Writing to an already existing file using FileWriter Java

北城以北 提交于 2019-12-29 07:31:31

问题


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


回答1:


Yeah. Use the constructor like this:

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



回答2:


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. 


来源:https://stackoverflow.com/questions/3005124/writing-to-an-already-existing-file-using-filewriter-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!