How to Export Data From CSV Files in Java
- 1). Include the following line at the beginning of your Java program:
import java.io.FileWriter; - 2). Open the CSV file as the target of a FileWriter by including the following code:
FileWriter myWriter = new FileWriter("output.csv");
Replace "output.csv" by the name of the CSV file you want to create. - 3). Write comma-separated values to the open CSV file by generating output in append mode, as in the following sample code:
myWriter.append("EmployeeNumber");
myWriter.append(',');
myWriter.append("Name");
myWriter.append('\n');
myWriter.append("92");
myWriter.append(',');
myWriter.append("Elton Fanculo");
myWriter.append('\n');
The first group of invocations to the FileWriter.append method writes the column names at the beginning of the CSV file; the second group writes a sample record. - 4). Commit all updates made on the CSV file to secondary storage by including the following code in your program after all of the contents of the CSV file have been written out:
myWriter.flush();
myWriter.close();
Source...