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
In Java, we can append a string in an existing file using FileWriter which has an option to open a file in append mode. Java FileWriter class is used to write character-oriented data to a file. It is a character-oriented class that is used for file handling in Java.
When you create a Java FileWriter you can decide if you want to overwrite any existing file with the same name, or if you want to append to any existing file.
FileWriter fw = new FileWriter(String fileName); 7. FileWriter(String fileName, Boolean append): It constructs a FileWriter object given a file name with a Boolean indicating whether or not to append the data written. FileWriter fw = new FileWriter(String fileName, Boolean append);
To append new record on existing . csv file, you have to enable append mechanism of FileWritter class(here 'true' - second argument) which enables append feature of FileWritter class. Hope this may work for you. Save this answer.
Yeah. Use the constructor like this:
FileWriter writer = new FileWriter("myfile.csv",true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With