java - PrintWriter 与 FileWriter 和 BufferedWriter
我已经无计可施了。
我正在尝试使用以下命令在文件中打印几千行:
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true));
PrintWriter pw = new PrintWriter(bw, true);
该文件已经包含文本,因此我在 FileWriter 中追加,因此是真正的参数。
现在,过去两个小时让我感到困惑的是,大约 85-90% 的行被写入文件,而前 10-15% 则没有。
代码在逻辑上没有问题,因为如果我在控制台中打印它,所有行都会被打印。
我在这里错过了什么吗?
我只在打印所有输出后执行 pw.close() 。
I'm at my wits end here.
I'm trying to print a few thousands of lines in a file, using the following:
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true));
PrintWriter pw = new PrintWriter(bw, true);
The file already consists of text so I'm appending, hence the true argument, in FileWriter.
Now what seems to be puzzling me for the last two hours, is that around 85-90% of the lines get written into the file, while the FIRST 10-15% are not.
There's nothing wrong with the code in terms of logic, because if i print it in the console, all lines are printed.
Am I missing something here?
I only do pw.close() after all output is printed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次在代码中写入文件后,您可能需要调用手动 .flush() 命令,以确保正确写入。
这非常令人费解,如果问题仍然存在,请回信。
希望有帮助!
干杯,
弗恩
You might want to invoke a manual .flush() command after each time you write to your file in your code just to be very sure that you are writing out correctly.
This is pretty puzzling, do write back if the problem persists.
Hope it helps!
Cheers,
Vern
在 pw.close() 之前,也许您应该调用 flush() 确保所有流都被写出。
before pw.close(), perhaps you should call flush() to ensure all the stream is written out.