Samuel Hornby Samuel Hornby - 2 months ago 14
Java Question

JAVA write new row to .CSV file

How can I write a new row of data to a .CSV file that already has data in it. So far my code just clears the file and doesn't actually write anything?

BufferedReader br = null;
BufferedWriter bw = null;
String fileString = "patients.csv";
String fileLine = "";

File file = new File(fileString);
br = new BufferedReader(new FileReader(file));
FileWriter fw = new FileWriter(fileString);
bw = new BufferedWriter(fw);

while((fileLine = br.readLine()) != null){

bw.write(fileLine);
}


br.close();
bw.close();

Answer

specify true in the constructor java FileWriter to know that the true and append be added to the end of the file if you place it does not overwrite information

FileWriter fw = new FileWriter(fileString,true);