Apache commons CSV library is a java library that can be used to read and write CSV files in a very simple and easy way. Another advantage is that this java library is open source and freely available to use.
Add the library to your project as :
Maven dependency :
You just need to add this dependency in your pom.xml file.
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.5</version> </dependency>
or, Gradle Dependency :
You can just add this to your dependencies within build.gradle file.
compile "org.apache.commons:commons-csv:1.5"
Let us start by generating a simple CSV file “student.csv” in the following program.
Program to generate a simple CSV file :
import java.io.IOException; import java.io.Writer; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; public class BasicCsvWriter { public static void main(String[] args) { try { //We have to create CSVPrinter class object Writer writer = Files.newBufferedWriter(Paths.get("student.csv")); CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader("Student Name", "Fees")); //Writing records in the generated CSV file csvPrinter.printRecord("Akshay Sharma", 1000); csvPrinter.printRecord("Rahul Gupta", 2000); csvPrinter.printRecord("Jay Karn", 3000); //writing record in the form of list csvPrinter.printRecord(Arrays.asList("Dev Bhatia", 4000)); csvPrinter.flush(); } catch (IOException e) { e.printStackTrace(); } } }
This program generates the “student.csv” file with the content as follows:
Student Name, Fees Akshay Sharma,1000 Rahul Gupta,2000 Jay Karn,3000 Dev Bhatia,4000
Let us now read the content of the generated CSV file using the following program.
Program to read the CSV file :
import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; public class BasicCsvReader { public static void main(String[] args) throws IOException { BufferedReader reader = Files.newBufferedReader(Paths.get("student.csv")); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader("Student Name", "Fees").withIgnoreHeaderCase().withTrim()); for (CSVRecord csvRecord: csvParser) { // Accessing Values by Column Index String name = csvRecord.get(0); //Accessing the values by column header name String fees = csvRecord.get("fees"); //Printing the record System.out.println("Record Number - " + csvRecord.getRecordNumber()); System.out.println("Name : " + name); System.out.println("Fees : " + fees); System.out.println("\n\n"); } } }
The output will be generated on console as follows :
Record Number - 1 Name : Akshay Sharma Fees : 1000 Record Number - 2 Name : Rahul Gupta Fees : 2000 Record Number - 3 Name : Jay Karn Fees : 3000 Record Number - 4 Name : Dev Bhatia Fees : 4000