Hot File

Parse, Read and Write CSV File Examples in java

View: 417    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Javascript   Fields: Other

10 point/2 review File has been tested

Working with CSV files is not common, but it may be required at times while supporting legacy softwares. Java language does not provide any native support for effectively handling CSV files and without using 3rd party libraries, you will end up creating your own CSV parser. There is usually no advantage in re-inventing the wheel, So it is advisable to use such 3rd party tools for parsing the CSV files.

Introduction

Working with CSV files is not common, but it may be required at times while supporting legacy softwares. Java language does not provide any native support for effectively handling CSV files and without using 3rd party libraries, you will end up creating your own CSV parser. There is usually no advantage in re-inventing the wheel, So it is advisable to use such 3rd party tools for parsing the CSV files.

We have already learned about OpenCSV, let’s expand our knowledge by learning Super CSV – another powerful java library for parsing, reading and writing CSV files.

In this tutorial, you will be learning below listed things with examples.

Super CSV runtime dependencies

Let’s start by listing down dependencies needed to use Super CSV in your project.

Maven

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SuperCSVExamples</groupId>
  <artifactId>SuperCSVExamples</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
        <groupId>net.sf.supercsv</groupId>
        <artifactId>super-csv</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.4</version>
    </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Gradle.

'net.sf.supercsv:super-csv:2.4.0'
'org.slf4j:slf4j-api:1.7.4'

Some useful common classes

Let’s go through main classes you need to know about while working with Super CSV for reading or writing CSV files.

ICsvBeanReader and CsvBeanReader

ICsvBeanReader (interface) and CsvBeanReader (implementing class) are used to read CSV files. It reads a CSV file by instantiating a bean for every row and mapping each column to a field on the bean.

The bean to populate can be either a class or interface. If a class is used, it must be a valid Javabean, i.e. it must have a default no-argument constructor and getter/setter methods. An interface may also be used if it defines getters/setters – a proxy object will be created that implements the interface.

ICsvBeanWriter and CsvBeanWriter

ICsvBeanWriter (interface) and CsvBeanWriter (implementing class) are used to write CSV files. It writes a CSV file by mapping each field on the bean to a column in the CSV file (using the supplied name mapping).

CellProcessor

CellProcessor instances are used to read a value from CSV file and process it before setting it to java bean class/interface. e.g. You ay want to convert a value to Date object or even you may want to run some regex validation over values.

CsvPreference

Before reading or writing CSV files, you must supply the reader/writer with some preferences. Essentially it means that you are setting delimiter related configuration in CSV file. e.g. CsvPreference.STANDARD_PREFERENCE means :

And I will be populating the instances of Customer.java with values of above file.

package com.howtodoinjava.example;
 
public class Customer 
{
    private Integer CustomerId;
    private String CustomerName;
    private String Country;
    private Long PinCode;
    private String Email;
     
    public Customer(){
    }
     
    public Customer(Integer customerId, String customerName, String country, Long pinCode, String email) {
        super();
        this.CustomerId = customerId;
        this.CustomerName = customerName;
        this.Country = country;
        this.PinCode = pinCode;
        this.Email = email;
    }
    public Integer getCustomerId() {
        return CustomerId;
    }
    public void setCustomerId(Integer customerId) {
        CustomerId = customerId;
    }
    public String getCustomerName() {
        return CustomerName;
    }
    public void setCustomerName(String customerName) {
        CustomerName = customerName;
    }
    public String getCountry() {
        return Country;
    }
    public void setCountry(String country) {
        Country = country;
    }
    public Long getPinCode() {
        return PinCode;
    }
    public void setPinCode(Long pinCode) {
        PinCode = pinCode;
    }
    public String getEmail() {
        return Email;
    }
    public void setEmail(String email) {
        Email = email;
    }
    @Override
    public String toString() {
        return "Customer [CustomerId=" + CustomerId + ", CustomerName="
                + CustomerName + ", Country=" + Country + ", PinCode="
                + PinCode + ", Email=" + Email + "]";
    }
}

Now look at CSV file, first row is column names. They should match up exactly with the bean’s field names, and the bean has the appropriate setters defined for each field.

If your header doesn’t match (or there is no header), then you can simply define your own name mapping array. [I have commented out the line, but you may take the hint.]

package com.howtodoinjava.example;
 
import java.io.FileReader;
import java.io.IOException;
 
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.ParseLong;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.StrRegEx;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;
 
public class ReadCSVFileExample {
 
    static final String CSV_FILENAME = "data.csv";
 
    public static void main(String[] args) throws IOException 
    {
        try(ICsvBeanReader beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE))
        {
            // the header elements are used to map the values to the bean
            final String[] headers = beanReader.getHeader(true);
            //final String[] headers = new String[]{"CustomerId","CustomerName","Country","PinCode","Email"};
            final CellProcessor[] processors = getProcessors();
 
            Customer customer;
            while ((customer = beanReader.read(Customer.class, headers, processors)) != null) {
                System.out.println(customer);
            }
        } 
    }
 
    /**
     * Sets up the processors used for the examples.
     */
    private static CellProcessor[] getProcessors() {
        final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+";
        StrRegEx.registerMessage(emailRegex, "must be a valid email address");
 
        final CellProcessor[] processors = new CellProcessor[] {
                new NotNull(new ParseInt()), // CustomerId
                new NotNull(), // CustomerName
                new NotNull(), // Country
                new Optional(new ParseLong()), // PinCode
                new StrRegEx(emailRegex) // Email
        };
        return processors;
    }
}
 
Output:
 
Customer [CustomerId=10001, CustomerName=Lokesh, Country=India, PinCode=110001, Email=abc@gmail.com]
Customer [CustomerId=10002, CustomerName=John, Country=USA, PinCode=220002, Email=def@gmail.com]
Customer [CustomerId=10003, CustomerName=Blue, Country=France, PinCode=330003, Email=ghi@gmail.com]
//... So on

Partially reading CSV file

Partial reading allows you to ignore columns when reading CSV files by simply setting the appropriate header columns to null. For example, in below code I have decided NOT to read the PinCode column.

final String[] headers = new String[]{"CustomerId", "CustomerName", "Country", null, "Email"};

Complete Example.

package com.howtodoinjava.example;
 
import java.io.FileReader;
import java.io.IOException;
 
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.ParseLong;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.StrRegEx;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;
 
public class PartialReadCSVFileExample {
 
    static final String CSV_FILENAME = "data.csv";
 
    public static void main(String[] args) throws IOException 
    {
        final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+";
        StrRegEx.registerMessage(emailRegex, "must be a valid email address");
         
        try(ICsvBeanReader beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE))
        {
            //First column is header
            beanReader.getHeader(true);
            //Set null for columns you do not want to read
            //final String[] headers = beanReader.getHeader(true);
            final String[] headers = new String[]{"CustomerId","CustomerName","Country",null,"Email"};
            final CellProcessor[] processors = getProcessors();
 
            Customer customer;
            while ((customer = beanReader.read(Customer.class, headers, processors)) != null) {
                System.out.println(customer);
            }
        } 
    }
 
    /**
     * Sets up the processors used for the examples.
     */
    private static CellProcessor[] getProcessors() {
        final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+";
        StrRegEx.registerMessage(emailRegex, "must be a valid email address");
 
        final CellProcessor[] processors = new CellProcessor[] {
                new NotNull(new ParseInt()), // CustomerId
                new NotNull(), // CustomerName
                new NotNull(), // Country
                new Optional(new ParseLong()), // PinCode
                new StrRegEx(emailRegex) // Email
        };
        return processors;
    }
}
 
Output:
 
Customer [CustomerId=10001, CustomerName=Lokesh, Country=India, PinCode=null, Email=abc@gmail.com]
Customer [CustomerId=10002, CustomerName=John, Country=USA, PinCode=null, Email=def@gmail.com]
Customer [CustomerId=10003, CustomerName=Blue, Country=France, PinCode=null, Email=ghi@gmail.com]
//... So on

Reading CSV file in key-value pair

To read in key value pairs, we need to use CsvMapReader. It allows you to retrieve each column by name from the resulting Map, though you’ll have to cast each column to it’s appropriate type.

package com.howtodoinjava.example;
 
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
 
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.ParseLong;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.StrRegEx;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvMapReader;
import org.supercsv.io.ICsvMapReader;
import org.supercsv.prefs.CsvPreference;
 
public class ReadCSVFileInKeyValuePairs {
 
    static final String CSV_FILENAME = "data.csv";
 
    public static void main(String[] args) throws IOException 
    {
        try(ICsvMapReader listReader = new CsvMapReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE))
        {
            //First Column is header names
            final String[] headers = listReader.getHeader(true);
            final CellProcessor[] processors = getProcessors();
 
            Map<String, Object> fieldsInCurrentRow;
            while ((fieldsInCurrentRow = listReader.read(headers, processors)) != null) {
                System.out.println(fieldsInCurrentRow);
            }
        } 
    }
 
    /**
     * Sets up the processors used for the examples.
     */
    private static CellProcessor[] getProcessors() {
        final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+";
        StrRegEx.registerMessage(emailRegex, "must be a valid email address");
 
        final CellProcessor[] processors = new CellProcessor[] {
                new NotNull(new ParseInt()), // CustomerId
                new NotNull(), // CustomerName
                new NotNull(), // Country
                new Optional(new ParseLong()), // PinCode
                new StrRegEx(emailRegex) // Email
        };
        return processors;
    }
}
 
Output:
 
{Country=India, CustomerId=10001, CustomerName=Lokesh, Email=abc@gmail.com, PinCode=110001}
{Country=USA, CustomerId=10002, CustomerName=John, Email=def@gmail.com, PinCode=220002}
{Country=France, CustomerId=10003, CustomerName=Blue, Email=ghi@gmail.com, PinCode=330003}
//... So on

Reading CSV file with arbitrary number of columns

Some CSV files don’t conform to RFC4180 and have a different number of columns on each row. If you have got such a CSV file, then your will need to use CsvListReader, as it’s the only reader that supports it.

Read such files is tricky, as you do not know the number of columns in any row. So you read all columns in a row in aList and then based on size of list, you determine how you may want to handle the read values.

Let’s modify the data.csv and remove some data from it randomly.

Let’s read this file.

package com.howtodoinjava.example;
 
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
 
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.ParseLong;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.StrRegEx;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvListReader;
import org.supercsv.io.ICsvListReader;
import org.supercsv.prefs.CsvPreference;
 
public class ReadCSVFileWithArbitraryNumberOfColumns {
 
    static final String CSV_FILENAME = "data.csv";
 
    public static void main(String[] args) throws IOException 
    {
        try(ICsvListReader listReader = new CsvListReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE))
        {
            //First Column is header names- though we don't need it in runtime
            @SuppressWarnings("unused")
            final String[] headers = listReader.getHeader(true);
            CellProcessor[] processors = null;
 
            List<String> fieldsInCurrentRow;
            while ((fieldsInCurrentRow = listReader.read()) != null) {
                if(fieldsInCurrentRow.size() == 5){
                    processors = getFiveColumnProcessors();
                }else if(fieldsInCurrentRow.size() == 4) {
                    processors = getFourColumnProcessors();
                }else if(fieldsInCurrentRow.size() == 3) {
                    processors = getThreeColumnProcessors();
                }else{
                    //Create more processors
                }
                final List<Object> formattedFields = listReader.executeProcessors(processors);
                System.out.println(String.format("rowNo=%s, customerList=%s", listReader.getRowNumber(), formattedFields));
            }
        } 
    }
 
    private static CellProcessor[] getFiveColumnProcessors() {
        final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+";
        StrRegEx.registerMessage(emailRegex, "must be a valid email address");
 
        final CellProcessor[] processors = new CellProcessor[] {
                new NotNull(new ParseInt()), // CustomerId
                new NotNull(), // CustomerName
                new NotNull(), // Country
                new Optional(new ParseLong()), // PinCode
                new StrRegEx(emailRegex) // Email
        };
        return processors;
    }
    private static CellProcessor[] getFourColumnProcessors() {
        final CellProcessor[] processors = new CellProcessor[] {
                new NotNull(new ParseInt()), // CustomerId
                new NotNull(), // CustomerName
                new NotNull(), // Country
                new Optional(new ParseLong()) // PinCode
        };
        return processors;
    }
    private static CellProcessor[] getThreeColumnProcessors() {
        final CellProcessor[] processors = new CellProcessor[] {
                new NotNull(new ParseInt()), // CustomerId
                new NotNull(), // CustomerName
                new NotNull() //Country
        };
        return processors;
    }
}
 
Output:
 
rowNo=2, customerList=[10001, Lokesh, India, 110001, abc@gmail.com]
rowNo=3, customerList=[10002, John, USA]
rowNo=4, customerList=[10003, Blue, France, 330003]

Writing new CSV file

Writing a CSV file is as much simple as it was for reading the CSV file. Create CsvBeanWriter instance, define headers and processors and write the beans. It will generate the CSV file with data values populated from beans.

package com.howtodoinjava.example;
 
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.ParseLong;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.StrRegEx;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanWriter;
import org.supercsv.io.ICsvBeanWriter;
import org.supercsv.prefs.CsvPreference;
 
public class WriteCSVFileExample {
    //Watch out for Exception in thread "main" java.lang.ExceptionInInitializerError
    private static List<Customer> customers = new ArrayList<Customer>();
    static{
        customers.add(new Customer(1, "Lokesh", "India", 12345L, "howtodoinjava@gmail.com"));
        customers.add(new Customer(2, "Mukesh", "India", 34234L, "mukesh@gmail.com"));
        customers.add(new Customer(3, "Paul", "USA", 52345345L, "paul@gmail.com"));
    }
     
    private static CellProcessor[] getProcessors() {
        final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+";
        StrRegEx.registerMessage(emailRegex, "must be a valid email address");
 
        final CellProcessor[] processors = new CellProcessor[] {
                new NotNull(new ParseInt()), // CustomerId
                new NotNull(), // CustomerName
                new NotNull(), // Country
                new Optional(new ParseLong()), // PinCode
                new StrRegEx(emailRegex) // Email
        };
        return processors;
    }
     
    public static void main(String[] args) 
    {
         
        ICsvBeanWriter beanWriter = null;
        try {
            beanWriter = new CsvBeanWriter(new FileWriter("temp.csv"), CsvPreference.STANDARD_PREFERENCE);
            final String[] header = new String[] { "CustomerId", "CustomerName", "Country", "PinCode" ,"Email" };
            final CellProcessor[] processors = getProcessors();
            // write the header
            beanWriter.writeHeader(header);
            // write the beans data
            for (Customer c : customers) {
                beanWriter.write(c, header, processors);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }  finally {
            try {
                beanWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

Parse, Read and Write CSV File Examples in java

Parse, Read and Write CSV File Examples in java Posted on 17-08-2016  Working with CSV files is not common, but it may be required at times while supporting legacy softwares. Java language does not provide any native support for effectively handling CSV files and without using 3rd party libraries, you will end up creating your own CSV parser. There is usually no advantage in re-inventing the wheel, So it is advisable to use such 3rd party tools for parsing the CSV files. 5/10 417

Comment:

To comment you must be logged in members.

Files with category

  • JUnit 5 State Of The Union using java

    View: 552    Download: 0   Comment: 0   Author: none  

    JUnit 5 State Of The Union using java

    Category: Javascript
    Fields: Other

    2.25/2 review
    JUnit 5 has been under development for about 14 months now and the prototype is almost a year old. Time to summarize what happened so far, where the project stands, and where it’s going.

  • Getting Started with Dropwizard using java

    View: 618    Download: 0   Comment: 0   Author: none  

    Getting Started with Dropwizard using java

    Category: Javascript
    Fields: Other

    2.25/2 review
    Dropwizard is a framework for building RESTful web services in Java. In this tutorial we’re going to have a look at how to get started with developing a Dropwizard application by building a new service from scratch.

  • Build Query NULL Value in MySql

    View: 288    Download: 0   Comment: 0   Author: none  

    Build Query NULL Value in MySql

    Category: Javascript
    Fields: Other

    2.5/2 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • Manage Your JavaScript Application State with MobX

    View: 302    Download: 0   Comment: 0   Author: none  

    Manage Your JavaScript Application State with MobX

    Category: Javascript
    Fields: Other

    2.25/2 review
    This article was peer reviewed by Michel Weststrate and Aaron Boyer. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!

  • Build Bringing Pages to Life with the Web Animations API

    View: 314    Download: 0   Comment: 0   Author: none  

    Build Bringing Pages to Life with the Web Animations API

    Category: Javascript
    Fields: Other

    4.5/2 review
    This article is by guest author Dudley Storey. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • How to Style Google Custom Search Manually

    View: 291    Download: 0   Comment: 0   Author: none  

    How to Style Google Custom Search Manually

    Category: Javascript
    Fields: Other

    0/0 review
    Website owners very often decide on using Google’s Custom Search Engine (GCSE) for searching through their content instead of using built-in and/or custom search functionality. The reason is simple – it’s much less work, and most often it does the...

  • Test React Components Using Jest

    View: 4903    Download: 0   Comment: 0   Author: none  

    Test React Components Using Jest

    Category: Javascript
    Fields: Other

    4.5/1 review
    This article is by guest author Jack Franklin. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • Programming Question Reverse String without using String function

    View: 752    Download: 0   Comment: 0   Author: none  

    Programming Question Reverse String without using String function

    Category: Javascript
    Fields: Other

    0/0 review
    Write a program to reverse string without using string function. You don’t have to use any in-built string library function. This problem can be solved by multiple approaches. Let’s check it.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd