使用 FileWriter Java 写入已经存在的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3005124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:30:46  来源:igfitidea点击:

Writing to an already existing file using FileWriter Java

javaiofilewriter

提问by delo

Is there anyway I can write to an already existing file using Filewriter

无论如何我可以使用 Filewriter 写入已经存在的文件

For example when the user clicks a submit button:

例如,当用户单击提交按钮时:

FileWriter writer = new FileWriter("myfile.csv");
writer.append("LastName");
writer.append(',');
writer.append("FirstName");
writer.append('/n');

writer.append(LastNameTextField.getText());
writer.append(',');
writer.append(FirstNameTextField.getText());

I want to be able to write new data into the already existing myfile.csv without having to recreate a brand new one every time

我希望能够将新数据写入已经存在的 myfile.csv 中,而不必每次都重新创建一个全新的数据

回答by Geo

Yeah. Use the constructor like this:

是的。像这样使用构造函数:

FileWriter writer = new FileWriter("myfile.csv",true);

回答by Inv3r53

FileWriter

public FileWriter(File file,
                  boolean append)
           throws IOException

Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.