问题
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