The comma-separated values, or CSV, format is widely used for data files. A CSV file has data stored in rows and columns; each row is in a separate line in the file, and each column is separated by a comma from the following column in the same row. CSV files are portable across all operating systems. You can have your Java program generate a CSV file as part of its output.
- Skill level:
- Moderately Easy
Other People Are Reading
Include the following line at the beginning of your Java program:
Open the CSV file as the target of a FileWriter by including the following code:
FileWriter my Writer = new FileWriter("output.csv");
Replace "output.csv" by the name of the CSV file you want to create.
Write comma-separated values to the open CSV file by generating output in append mode, as in the following sample code:
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.
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:
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for