To create csv file in java

Apache Commons CSV to Read and Write CSV files in Java

Let’s take a look at reading and writing CSV files in java using Apache Commons CSV with a few examples. First, you might ask why we need a library when we can write CSV files using pure java. To some extent you are right. However, you would need a library as your custom written reader/writer might not cover all edge cases.

What is apache-commons CSV?

Apache Commons CSV is a java library that reads and writes files in Comma Separated Value (CSV) format. This library can write in all supported variations of CSV files like MSExcel, Informix, Oracle, MySQL and TSV(Tab-separated values). You can also create custom CSV formats using its fluent API. Let’s look at this library in action with some examples.

Maven Dependency

The easiest way to set up apache-commons-csv is to add its appropriate maven dependency. At the time of writing, the current version is 1.8. Add the following snippet to your section and you are done.

dependency> groupId>org.apache.commons groupId> artifactId>commons-csv artifactId> version>1.8 version> dependency>Code language: HTML, XML (xml)

Writing CSV files

Here is a simple java example of writing data into a file using Apache Commons CSV.

//data String[][] employees = < "Man", "Sparkes", "[email protected]", "Engineering">, "Dulcinea", "Terzi", "[email protected]", "Engineering">, "Tamar", "Bedder", "[email protected]", "Legal">, "Vance", "Scouller", "[email protected]", "Sales">, "Gran", "Jagoe", "[email protected]", "Business Development"> >; //create a CSV printer CSVPrinter printer = new CSVPrinter(new FileWriter("employees.csv"), CSVFormat.DEFAULT); //create header row printer.printRecord("FirstName", "LastName", "Email", "Department"); // create data rows for (String[] employee : employees) < printer.printRecord(employee); >//close the printer after the file is complete printer.flush(); printer.close();Code language: Java (java)

Here, we have the employee data inside an array of arrays. To write this data into file, you just need to create CSVPrinter. And then you can simply start writing records(rows).

Читайте также:  Python переименовать все файлы

If you want header row, then make sure you print them above all the data records.

Read data from CSV files using Apache Commons CSV

Reading data using this library is as easy as writing. Also, apache-commons CSV gives you a lot of options to read these files. Let’s take a look at few java examples with apache commons CSV.

The library comes with a CSVParser class that can read and convert each row into a list of CSVRecords. From this record object, we can gather data by column index.

CSVParser csvParser = new CSVParser(new FileReader("employees.csv"), CSVFormat.DEFAULT); for (CSVRecord record : csvParser) < System.out.println(record.get(0) + "," + record.get(1) + "," + record.get(2) + "," + record.get(3)); >Code language: Java (java)

You could also gather record values by assigning each index a header string.

CSVParser csvRecordsWithHeader = CSVFormat.DEFAULT.withHeader("FirstName","LastName","Email") .parse(new FileReader("csvwithoutheader.csv")); for (CSVRecord record : csvRecordsWithHeader) < System.out.println(record.get("FirstName") + "," + record.get("LastName") + "," + record.get("Email")); >Code language: Java (java)

The first column will be assigned the “FirstName” and second column will be assigned to “LastName” and the third column can be referred to using “Email”. If you already have a header row, then you can simply use the “withFirstLineAsHeader()” method.

CSVParser csvRecordsWithFirstLineHeader = CSVFormat.DEFAULT.withFirstRecordAsHeader() .parse(new FileReader("csvwithheader.csv")); for (CSVRecord record : csvRecordsWithHeader) < System.out.println(record.get("FirstName") + "," + record.get("LastName") + "," + record.get("Email")); >Code language: Java (java)

If you have a java Enum containing all the header names, then you can even use that to get the values from records. This approach will avoid having to hardcode column names as string everywhere.

java enum as header values in Apache Common CSV

CSVParser csvRecordsWithEnum = CSVFormat.DEFAULT.withHeader(Headers.class) .parse(new FileReader("employees.csv")); for (CSVRecord record : csvRecordsWithEnum) < System.out.println(record.get(Headers.FirstName) + "," + record.get(Headers.Email)); >Code language: Java (java)

Here you need to note a couple of things. The apache-commons CSV library still allows you to access each column using their respective indexes. The header Enums are evaluated in the order of Enum.values() method.

Also, All these examples use the DEFAULT CSV format. You can take a look at the CSVFormat class to know more about other formats. They all have the same behaviour but differs slightly like if they accept line break, empty lines etc.

You can find all of these examples in our GitHub repository.

Источник

Using Java to write Data into CSV files

Creating and writing CSV files is easier than you think with Java. CSV stands for Comma Separated Values. As the name suggests, We just need to write values separated by a comma on each line to create such a file.

Identify the data you want to write as CSV

For example, You have an Array of Array that contains information about employees like this.

String[][] employees = < "Man", "Sparkes", "[email protected]", "Engineering">, "Dulcinea", "Terzi", "[email protected]", "Engineering">, "Tamar", "Bedder", "[email protected]", "Legal">, "Vance", "Scouller", "[email protected]", "Sales">, "Gran", "Jagoe", "[email protected]", "Business Development"> >; Code language: Java (java)

In java, you can loop through these data and write the CSV file manually.

Create the CSV file using java.io.File class

In this step, you need to set up where the CSV file will go. For this, You need to define a File object and set up a write mechanism. For this example, we are going to use a file writer.

File csvFile = new File("employees.csv"); FileWriter fileWriter = new FileWriter(csvFile);Code language: Java (java)

The constructor for FileWriter will throw a java.io.IOException. You can either handle it using try-catch or add the exception to the method signature.

Java code to Write Data to CSV File

The next step is to add each record as comma-separated values in each line. For this, we can use a simple loop over java arrays. For example, we are using StringBuilder to create each line. And also, We are making sure to add commas and line breaks when necessary.

for (String[] data : employees) < StringBuilder line = new StringBuilder(); for (int i = 0; i < data.length; i++) < line.append(data[i]); if (i != data.length - 1) < line.append(','); > > line.append("\n"); fileWriter.write(line.toString()); > fileWriter.close();Code language: Java (java)

You have almost completed your work to write data into CSV files using pure java. You can run this java program and see that it creates employees.csv file.

Problems with this approach

Even though this approach works for the data we created, there are certain rules you must follow to handle special characters in a CSV file.

  1. You should wrap a value with double quotes if it contains a comma(,). So it is usually better to wrap all values in the double quote(“).
  2. A string with a line break also needs to be wrapped in double-quotes. But some CSV readers may not recognize line breaks and will cause problems.
  3. In the case of wrapping with double quotes, you will need to escape double quotes within the string you are trying to quote. The escaping is done by adding another double quote.
    1. For example, Some Guy with a “Masters” Degree becomes “Some Guy with a “”Masters”” Degree“.

    So let’s fix these problems in our program.

    Final Java Program that writes CSV

    Here is the final version of our CSV writer with all necessary fixes.

    public class CSVWriter < public static void main(String[] args) throws IOException < String[][] employees = < "Man", "Sparkes", "[email protected]", "Engineering">, "Dulcinea", "Terzi", "[email protected]", "Engineering">, "Tamar", "Bedder", "[email protected]", "Legal">, "Vance", "Scouller", "[email protected]", "Sales">, "Gran", "Jagoe", "[email protected]", "Business Development"> >; File csvFile = new File("employees.csv"); FileWriter fileWriter = new FileWriter(csvFile); //write header line here if you need. for (String[] data : employees) < StringBuilder line = new StringBuilder(); for (int i = 0; i < data.length; i++) < line.append("\""); line.append(data[i].replaceAll("\"","\"\"")); line.append("\""); if (i != data.length - 1) < line.append(','); > > line.append("\n"); fileWriter.write(line.toString()); > fileWriter.close(); > > Code language: Java (java)

    Once we run this program, we can see that our employees.csv can be opened in applications like excel or google sheets.

    CSV file created from Java program

    Note that this program is just an example to demonstrate how we can create a simple CSV file using plain Java. Even though this approach is small and simple, I would advise you to use java libraries like apache commons-CSV library to write CSV files. You can find all these examples in our GitHub repository.

    Источник

Оцените статью