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?

有帮助吗?

解决方案

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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top