Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to an already existing file using FileWriter Java

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

like image 446
delo Avatar asked Jun 09 '10 10:06

delo


People also ask

How do I add text to an existing file in Java?

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.

Does FileWriter overwrite existing file?

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.

How do you write to a file using FileWriter class?

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

How do I add data to an existing csv file in Java?

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.


1 Answers

Yeah. Use the constructor like this:

FileWriter writer = new FileWriter("myfile.csv",true);
like image 144
Geo Avatar answered Nov 07 '22 23:11

Geo