Java parsing CSV

Source: Internet
Author: User
Tags dateformat sublime text
Opencsv

Java read CSV Class library There are two main, opencsv and javacsv, the study found that javacsv last update is 2014-12-10, a long time does not maintain. Opencsv is an Apache project and is still being maintained, so decided to use Opencsv. CSV

CSV file, full name comma separated values, comma separated by default, is a plain text file. Although it was formatted with Excel, it was handled by Excel. Open with Notepad or sublime text to see the most original texts.
For a follow-up example, a Test.csv was edited here

Header1,header2,header3
1,a,10
2,b,20
3,c,30
4,d,40
5,e,50
Reading ModeIn the most original way, read-by-line, and then operate
Csvreader reader = new Csvreader (New InputStreamReader ("Test.csv"), "GBK");
       * * Line read
      /string[] Strarr = null;
      while ((Strarr = Reader.readnext ())!=null) {
          System.out.println (strarr[0]+ "---" +strarr[1]+ "----" +strarr[2]);

      Reader.close ();
bind csv file into Bean

Line-by-row reads are the most primitive way to operate, and Opencsv provides a "policy" based mapping to bind a CSV to a bean. Introduction to Policies

Look at the inheritance hierarchy of the policy
Interface Mappingstrategy
Top-level interface for mappings Headercolumnnamemappingstrategy
A column name Mapping policy that reads the first line of the CSV file as headers, such as Header1,header2,header3, and then calls the Bean's SetHeader1 method, SetHeader2 method, and SetHeader3 method to set the value separately, So this policy requires that the column name is exactly the same as the property name in the Bean, and if it is inconsistent, the value is empty and there is no error. When using annotations, the annotation name must be the same as the column name in the CSV. Columnpositionmappingstrategy
Column position mapping policy, he does not have the concept of header, so will output all rows. Specify the properties of the bean in the ColumnMapping array, the first value corresponds to the first column of CSV, and the second value to the second class of CSV ... Headercolumnnametranslatemappingstrategy
column Header name translation mapping policy, the Bean's property name can be different from the CSV column header compared to Headercolumnnamemappintstrategy. Mapped by specifying a map. Specific mapping usage Java Pojo class

public class Simplebeaninfo {

    private String header1;


    Private String Header2;

    Private String Header3;

    Public String GetHeader1 () {return
        header1;
    }

    @Override public
    String toString () {return
        "Simplebeaninfo [header1= + Header1 +", header2= "+ header2
                +", Header3= "+ Header3 +"] ";
    }

    public void SetHeader1 (String header1) {
        this.header1 = header1;
    }

    Public String GetHeader2 () {return
        header2;
    }

    public void SetHeader2 (String header2) {
        this.header2 = Header2;
    }

    Public String GetHeader3 () {return
        header3;
    }

    public void SetHeader3 (String header3) {
        this.header3 = Header3;
    }
mapping based on the column index

Popular point is the column position mapping, where the column position in the CSV file corresponds to the column in the Bean

Non-annotation method

Csvreader reader = new Csvreader (New InputStreamReader ("Test.csv"), "GBK");
       * * Based on the column position, mapping into the class/
       //csv file in the first column of the corresponding class header, the second column corresponding to the class Header2, the third column corresponding class Header3
      string[] columnmapping={ "Header1", "Header2", "Header3"};
      Columnpositionmappingstrategy<simplebeaninfo> mapper = new Columnpositionmappingstrategy<simplebeaninfo > ();
      Mapper.setcolumnmapping (columnmapping);
      Mapper.settype (simplebeaninfo.class);
      /* 
      *  /csvtobean<simplebeaninfo> Csvtobean = new csvtobean<simplebeaninfo> ();

      list<simplebeaninfo> list = Csvtobean.parse (mapper, reader);

      for (Simplebeaninfo e:list) {
          System.out.println (e);
      }

    }
Annotation mode
public class Simplebeaninfo {

    @CsvBindByPosition (position=0)
    private String header1;


    @CsvBindByPosition (position=1)
    private String header2;

    @CsvBindByPosition (position=2)
    private String header3;
}
Csvreader reader = new Csvreader (New InputStreamReader ("Test.csv"), "GBK");
 Columnpositionmappingstrategy<simplebeaninfo> mapper = new Columnpositionmappingstrategy<simplebeaninfo > ();
      Mapper.settype (simplebeaninfo.class);
      csvtobean<simplebeaninfo>  Csvtobean = new csvtobean<simplebeaninfo> ();

      list<simplebeaninfo> list = Csvtobean.parse (mapper, reader);

      for (Simplebeaninfo e:list) {
          System.out.println (e);
      }
a mapping based on column namesNon-annotation method
Csvreader reader = new Csvreader (New InputStreamReader ("Test.csv"), "GBK");

      /* */ 

      headercolumnnamemappingstrategy<simplebeaninfo> mapper = new
              headercolumnnamemappingstrategy <SimpleBeanInfo> ();
      Mapper.settype (simplebeaninfo.class);
      csvtobean<simplebeaninfo>  Csvtobean = new csvtobean<simplebeaninfo> ();

      list<simplebeaninfo> list = Csvtobean.parse (mapper, reader);

      for (Simplebeaninfo e:list) {
          System.out.println (e);
      }
Annotation mode
public class Simplebeaninfo {

    @CsvBindByName (column= "header1")
    private String header1;


    @CsvBindByName (column= "Header2")
    private String header2;

    @CsvBindByName (column= "Header3")
    private String header3;
}
Csvreader reader = new Csvreader (New InputStreamReader ("Test.csv"), "GBK");

      Headercolumnnamemappingstrategy<simplebeaninfo> mapper = new
              headercolumnnamemappingstrategy< Simplebeaninfo> ();
      Mapper.settype (simplebeaninfo.class);
      csvtobean<simplebeaninfo>  Csvtobean = new csvtobean<simplebeaninfo> ();

      list<simplebeaninfo> list = Csvtobean.parse (mapper, reader);

      for (Simplebeaninfo e:list) {
          System.out.println (e);
      }
transform mappings based on column names
Csvreader reader = new Csvreader (New InputStreamReader ("Test.csv"), "GBK");
       * * * based on column name conversion, mapping into class
      * *
      headercolumnnametranslatemappingstrategy<simplebeaninfo> mapper = 
              New Headercolumnnametranslatemappingstrategy<simplebeaninfo> ();
      Mapper.settype (simplebeaninfo.class);

      map<string,string> columnmapping = new hashmap<string,string> ();
      Columnmapping.put ("Header1", "header1"), header1 in//csv corresponding bean header1
      columnmapping.put ("Header2", "Header2") ;
      Columnmapping.put ("Header3", "Header3");
      Mapper.setcolumnmapping (columnmapping);

      csvtobean<simplebeaninfo>  Csvtobean = new csvtobean<simplebeaninfo> ();

      list<simplebeaninfo> list = Csvtobean.parse (mapper, reader);

      for (Simplebeaninfo e:list) {
          System.out.println (e);
      }
Filtering Device

Opencsv provides filters that filter certain rows, such as page headers, page footer, and all filters that must implement the Csvtobeanfilter interface

public class Mycsvtobeanfilter implements Csvtobeanfilter {public

        Boolean allowline (string[] line) {
            // Filter the rows with the first column value equal to 1
            if ("1". Equals (Line[0])) {return
                false;
            }
            return true;
        }

Mycsvtobeanfilter filter = new Mycsvtobeanfilter ();
 list<simplebeaninfo> list = Csvtobean.parse (mapper, Reader,filter);
Conversion Device

The properties in a class are not necessarily strings, such as numbers, dates, and so on, but we get a string from a CSV, and we should use the converter.
Here defines a simplebeanconverter, inherits Abstractbeanfield

public class Simplebeanfieldconverter extends abstractbeanfield<simplebeaninfo> {

    @Override
    protected Object CONVERT (String value) throws Csvdatatypemismatchexception,
            csvrequiredfieldemptyexception, csvconstraintviolationexception {
        Field f = GetField ();
        if ("Date". Equals (F.getname ()) {
            try {return
                new SimpleDateFormat ("Yyyy-mm-dd"). Parse (value);
            } catch ( ParseException e) {
                e.printstacktrace ();
            }
        }
        return null;
    }
}

Test.csv Add a column header4

Header1,header2,header3,header4
1,a,10,2016-05-01
2,b,20,2016-05-02
3,c,30,2016-05-03
4,d, 40,2016-05-04
5,e,50,2016-05-05
6,f,60,2016-05-06

Simplebeaninfo Add Properties

@CsvCustomBindByPosition (Position=3,converter=simplebeanfieldconverter.class)
private date date;

Output
Because Columnpositionmappingstrategy will also parse the header row, the first line prints exception information. We see that the Header4 column has been converted to a date. What to do if more than one column needs to be converted. Add a note (@CsvCustomBindByPosition or @csvcustombindbyname) to the corresponding property, and then expand it in convert (Object value)

Java.text.ParseException:Unparseable Date: "Header4" at Java.text.DateFormat.parse (dateformat.java:357) at Test_m Aven. Simplebeanfieldconverter.convert (simplebeanfieldconverter.java:24) at Com.opencsv.bean.AbstractBeanField.setFieldValue (abstractbeanfield.java:70) at Com.opencsv.bean.CsvToBean.processField (csvtobean.java:245) at Com.opencsv.bean.CsvToBean.processLine ( csvtobean.java:220) at Com.opencsv.bean.CsvToBean.processLine (csvtobean.java:189) at COM.OPENCSV.BEAN.CSVTOBEAN.PA RSE (csvtobean.java:166) at the Com.opencsv.bean.CsvToBean.parse (csvtobean.java:133) at Test_maven.
Testcsv.main (testcsv.java:46) simplebeaninfo [Header1=header1, Header2=header2, Header3=header3, Date=null]  Simplebeaninfo [header1=2, Header2=b, header3=20, Date=mon May 00:00:00 CST 2016] simplebeaninfo [Header1=3, Header2=c,  header3=30, Date=tue 00:00:00 CST 2016] simplebeaninfo [header1=4, Header2=d, header3=40, date=wed May 04 00:00:00 CST 2016] simplebeaninfo [header1=5, Header2=e, header3=50, Date=thu May 00:00:00 CST 2016] simplebeaninfo [Header1=6, Header2=f, header3=60, Date=Fri M AY-00:00:00 CST 2016]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.