BufferedWriter not writing to text file

帅比萌擦擦* 提交于 2019-12-31 04:13:27

问题


So I'm using a BufferedWriter and would like to write some text to a text file.

try {
   BufferedWriter b = new BufferedWriter(new FileWriter ("/home/usr/Desktop/logger/logs.txt"));
   b.write("hello");
} catch (Exception e1) { e1.printStackTrace(); }

For some reason the text document is being created but nothing is being written to it, why is this?


回答1:


You need to close BufferedWriter, or use try-with-resource

BufferedWriter b = new BufferedWriter(
new FileWriter ("/home/usr/Desktop/logger/logs.txt"));
b.write("hello");

//After writing close the resource
b.close();


来源:https://stackoverflow.com/questions/20509114/bufferedwriter-not-writing-to-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!