我正在尝试将内容写入文件,但是一旦 while 循环打印了所有行,它就会卡住并且不会转到下一行。while 循环之后的行没有被执行。我的第一个猜测是 fstream 可能仍在等待另一行,但是 while 循环确实检查是否没有更多数据然后它应该出来。
请帮帮我!谢谢。
//代码
File file;
DataInputStream inputData = new DataInputStream(newPeerConnection.getInputStream());
file = new File(
"F:\\Workspace\\PeerToPeer\\src\\ncsu\\csc\\socketClasses\\out\\out.txt");
@SuppressWarnings("resource")
PrintWriter fstream = new PrintWriter(new FileWriter(
file, true));
if (!file.exists()) {
file.createNewFile();
}
System.out.println("Writing to file: ");
while ((strval = inputData.readUTF()) != null) {
System.out.println(strval);
fstream.println(strval);
//fstream.flush();
//fstream.close();
}
//These lines are not executed
System.out.println("Do you want to download another file (Y/N)?");
wantToContinue = scannerObj.next();
out.writeUTF(wantToContinue);
out.flush();}