Loading...

Parse, Read and Write CSV File Examples in java

View: 668    Dowload: 0   Comment: 0   Post by: hanhga   Category: Javascript   Fields: Other

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

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.

Posted on 17-08-2016 

Comment:

To comment you must be logged in members.

Files with category

  • Mini Youtube Using ReactJS

    Mini Youtube Using ReactJS

    View: 169    Download: 3   Comment: 0

    Category: Javascript     Fields: none

    This is one the best starter for ReactJS. MiniYoutube as the name suggests is a youtube like website developed using reactJS and youtube API. This project actually let's you search , play and list youtube videos. Do check it out and start learning...

  • AngularJS and REST API

    AngularJS and REST API

    View: 315    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    This is a tutorial for those interested in a quick introduction to AngularJS and REST API. We will build the familiar Periodic Table of the Elements found in every chemistry textbook, and allow the user to select a Chemical Element by clicking on...

  • Collective Intelligence, Recommending Items Based on Similar Users' Taste

    Collective Intelligence, Recommending Items Based on Similar Users' Taste

    View: 250    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Using Collaborative Filtering to find people who share tastes, and for making automatic recommendations based on things that other people like.

  • Think Like a Bird for Better Parallel Programming

    Think Like a Bird for Better Parallel Programming

    View: 226    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Coding an application to run in parallel is hard, right? I mean, it must be hard or we’d see parallel programs everywhere. All we'd see are slick parallel apps that use every available core effortlessly. Instead multi-threaded apps are the exception...

  • Getting Started with the Bing Search APIs

    Getting Started with the Bing Search APIs

    View: 249    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Bing Search API is a set of REST interfaces that find web pages, news, images, videos, entities, related searches, spelling corrections, and more in response to queries from any programming language that can generate a web request. Applications that...

  • Brief Introduction of SocketPro High Performance and Scalable Persistent Message Queue

    Brief Introduction of SocketPro High Performance and Scalable Persistent Message Queue

    View: 569    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Continuous in-line request/result batching, real-time stream sending/processing, asynchronous data transferring and parallel computation for best performance and scalability

  • Iteration Over Java Collections with High Performance

    Iteration Over Java Collections with High Performance

    View: 219    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Java developers usually deal with Collections such as ArrayList, HashSet, Java 8 come with lambda and streaming API helps us to easily work with Collections. In most cases, we work with few thousands of items and performance isn't a concern. But in...

  • SR2JLIB - A Symbolic Regression Library for Java

    SR2JLIB - A Symbolic Regression Library for Java

    View: 215    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Grammar-Guided Genetic Programming library featuring: multi-threading, just-in-time compilation of individuals, dynamic class loading, and JNI interfacing with C/C++ code

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd