Read and write CSV files with opencsv files

Source: Internet
Author: User

First understand what the CSV file looks for:

    • Open in Excel becomes a table, you can not see the details
    • It is recommended to use other simple and crude editors, such as notepad++,

The contents of the CSV file are as follows:

The CSV file is separated by commas by default.

Get a basic understanding of the topic, read and write CSV files with Opencsv

    • READ: Csvreader
    • Written by: Csvwriter

Here's a look at the methods Opencsv provides for us (here are just a few of the most common):

READ: Csvreader

There are three parameters involved in a constructor:

    • Reader: Is the stream object that reads the file, commonly has bufferedreader,inputstreamreader and so on
    • Separator: Used to define the delimiter mentioned earlier, the default is comma "csvwriter. Default_separator "for separating columns;
    • QuoteChar: Used to define the quotation marks for each column, sometimes the CSV file will enclose a column in quotation marks or other symbols, such as a row may be: "1", "2", "3", if the character you want to read does not include quotation marks, you can set the parameter to: "csvwriter. No_quote_character "
A csvreader that defines a comma as a delimiter and ignores quotes when Read is:

Csvreader reader = new Csvreader (new InputStreamReader (New FileInputStream (CSVFile), "GBK"), Csvwriter.default_ SEPARATOR, Csvwriter.no_quote_character);

The second method in the table above is relatively inferior to the other three, and close () does not say much,

    • ReadAll (): Read all
    • Readnext (): reads a row
Note: If the first Readnext, and then Readall,readall also from the Readnext after the line began, that is, Readnext read it will not read again.

Written by: Csvwriter

Constructors and methods are relatively easy to understand, and reader can correspond to understand, there is not much to say.

Here's a simple example:

 Public classOpencsv { Public Static voidMain (string[] args)throwsException {opencsv oc=Newopencsv (); Oc.        Csvreadall (); Oc.    Csvwriter (); }     PublicString dir = system.getproperty ("User.dir") + "/testmaven";  Public voidCsvreadall ()throwsException {File csv=NewFile (dir + "/file", "Readertest.csv"); Csvreader Reader=NewCsvreader (NewInputStreamReader (NewFileInputStream (CSV), "GBK"), ', '); String [] Header= Reader.readnext ();//after reading with Readnext, it doesn't exist in the stream.         for(String s:header) {System.out.print (s+ ","); } System.out.println (""); List<String[]> list = Reader.readall ();//Now the second line is read .System.out.println (list.get (0) [0]); System.out.println (""); }     Public voidCsvwriter ()throwsexception{File csv=NewFile (dir + "/file", "Writertest.csv"); if(!csv.exists ()) csv.createnewfile (); List<String[]> list =NewArraylist<>(); String S1= "123";  for(inti = 0; I < 22; i++) {string[] SS={string.valueof (i), string.valueof (i), string.valueof (i), string.valueof (i)};        List.add (ss); } csvwriter writer=NewCsvwriter (NewOutputStreamWriter (NewFileOutputStream (CSV), "GBK"), Csvwriter.default_separator, Csvwriter.no_quote_character);        Writer.writenext (S1, S1, S1, S1); Writer.writenext ("#","#","#","#");        Writer.writeall (list);        Writer.flush ();    Writer.close (); }}

Read and write CSV files with opencsv files

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.