使用FileWriter Java写入现有文件

时间:2010-06-09 10:57:06

标签: java io filewriter

无论如何,我可以使用Filewriter

写入已存在的文件

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

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());

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

2 个答案:

答案 0 :(得分:22)

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

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

答案 1 :(得分:7)

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.