我对Java非常陌生,我无法将内容写入文件,我的代码如下:
FileWriter fstream;
try {
fstream = new FileWriter(fileLocation);
BufferedWriter out = new BufferedWriter(fstream);
log.info("test was supposed to be written to the file");
out.write("test");
out.flush();
out.close();
} catch (IOException e) {
log.error("File not created ", e);
}
当我进入文件位置时,我看到我的文件,但是它是空的。我的日志显示“test应该被写入文件”。
我在这里可能做错了什么?
谢谢!
更新:我的FileLocation变量是一个字符串:
private String fileLocation="/Users/s/out.txt";
我正在使用Mac电脑
finally
中这样做。flush()
调用也是不必要的,因为close
已经隐式地执行了它。 - BalusC