Appending Data to an Existing CSV File
Now that we've created a CSV file, let's modify our Java program to append additional data to the existing file rather than overwriting it.
- Create a new Java file named
CSVAppendExample.java
:
touch src/main/java/com/example/CSVAppendExample.java
- Open
CSVAppendExample.java
in the WebIDE editor and add the following code:
package com.example;
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CSVAppendExample {
public static void main(String[] args) {
try {
// Create a CSVWriter in append mode by setting the second parameter of FileWriter to true
CSVWriter writer = new CSVWriter(new FileWriter("students.csv", true));
// Write additional data to the existing file
String[] student3 = {"Robert Johnson", "24", "[email protected]"};
writer.writeNext(student3);
String[] student4 = {"Maria Garcia", "23", "[email protected]"};
writer.writeNext(student4);
// Close the writer
writer.close();
System.out.println("Data appended to CSV file successfully!");
} catch (IOException e) {
System.out.println("Error appending to CSV file: " + e.getMessage());
e.printStackTrace();
}
}
}
This code is similar to our previous example, but with two important differences:
- We create the
FileWriter
with a second parameter set to true
, which puts it in append mode
- We don't write a header row, since the file already has one
- Run the program to append data to our existing CSV file:
cd ~/project/csv-writer
mvn exec:java -Dexec.mainClass="com.example.CSVAppendExample"
- Let's check the content of our CSV file now:
cat students.csv
You should see the original data plus the newly appended records:
"Name","Age","Email"
"John Doe","25","[email protected]"
"Jane Smith","22","[email protected]"
"Robert Johnson","24","[email protected]"
"Maria Garcia","23","[email protected]"
As you can see, the new data was added to the end of the file without disturbing the existing content.
- Let's create one more example that demonstrates writing CSV files with custom separators:
touch src/main/java/com/example/CSVCustomExample.java
- Open
CSVCustomExample.java
in the WebIDE editor and add the following code:
package com.example;
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.io.IOException;
public class CSVCustomExample {
public static void main(String[] args) {
try {
// Create a CSVWriter with semicolon as separator instead of comma
CSVWriter writer = new CSVWriter(new FileWriter("students_semicolon.csv"),
';',
CSVWriter.DEFAULT_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);
// Write header line
String[] header = {"Name", "Age", "Email"};
writer.writeNext(header);
// Write data lines
String[] student1 = {"John Doe", "25", "[email protected]"};
writer.writeNext(student1);
String[] student2 = {"Jane Smith", "22", "[email protected]"};
writer.writeNext(student2);
// Close the writer
writer.close();
System.out.println("CSV file with semicolon separator created successfully!");
} catch (IOException e) {
System.out.println("Error writing to CSV file: " + e.getMessage());
e.printStackTrace();
}
}
}
This example creates a CSV file using semicolons as separators instead of commas, which is common in some European countries where commas are used as decimal separators.
- Run the custom separator example:
cd ~/project/csv-writer
mvn exec:java -Dexec.mainClass="com.example.CSVCustomExample"
- Check the content of the new CSV file:
cat students_semicolon.csv
You should see the CSV file with semicolons as separators:
"Name";"Age";"Email"
"John Doe";"25";"[email protected]"
"Jane Smith";"22";"[email protected]"