我正在尝试使用 print writer 创建并写入 java 文件。
我看了How do I create a file and write to it in Java?
我无法写入该文件。文件已创建,但没有文本
有人可以告诉我我错过了什么吗?
谢谢
static void createFile() throws FileNotFoundException{
String filename = "nothign.txt";
FileOutputStream connection1;
connection1 = new FileOutputStream( filename );
PrintWriter printnothing = null;
printnothing = new PrintWriter(connection1);
printnothing.printf("/nnewline writesomething/n exo");
printnothing.println("trying to write something");
}// createFile Method
最佳答案
我相信您需要关闭 PrintWriter 才能将内容刷新到文件中。尝试将其添加到代码末尾:
printnothing.close();
关于java - 用java写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18284291/