Writing to an already existing file using FileWrit

2019-01-26 07:40发布

Is there anyway I can write to an already existing file using 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

2条回答
Viruses.
2楼-- · 2019-01-26 08:26

Yeah. Use the constructor like this:

FileWriter writer = new FileWriter("myfile.csv",true);
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-26 08:35
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. 
查看更多
登录 后发表回答