【发布时间】:2021-08-19 21:37:58
【问题描述】:
我正在尝试接收byte[] 中的文件,我正在使用:
byte[] buffer = new byte[16384]; // How many bytes to read each run
InputStream in = socket.getInputStream(); // Get the data (bytes)
while((count = in.read(buffer)) > 0) { // While there is more data, keep running
fos.write(buffer); // Write the data to the file
times++; // Get the amount of times the loop ran
System.out.println("Times: " + times);
}
System.out.println("Loop ended");
循环在 1293 次后停止,然后停止打印时间。但是代码没有移动到System.out.println("Loop ended"); - 似乎循环正在等待什么......
为什么循环没有中断?
【问题讨论】:
-
in.read(buffer)可能会停止代码,试图无限期地读取数据。 -
也许吧。但我认为没有其他方法可以从输入流中读取数据
标签: java while-loop buffer