Using the Javacsv API to manipulate CSV files

Source: Internet
Author: User
Javacsv is a foreign development of a relatively good operation of the CSV file API, here a brief talk about usage.
First download the Javacsv2.0.zip file, after decompression, add Javacsv.jar to the project.
Site Download Address:
Http://www.cnitblog.com/Files/rd416/javacsv2.0.zip
Official Download Address:
http://sourceforge.net/project/showfiles.php?group_id=33066

Simple Operation code:

 /** * Read CSV file/public void Readecsv () {try { arraylist<string[]> csvlist = new arraylist<string[]> ();
                    Used to save the data String Csvfilepath = "C:/test.csv";    Csvreader reader = new Csvreader (Csvfilepath, ', ', Charset.forname ("Sjis")); Generally read with this code can be reader.readheaders ();
                    
                    Skip the header If you need a table head, don't write this sentence.
                    while (Reader.readrecord ()) {//Read the Data Csvlist.add (Reader.getvalues ()) In addition to the header line by row;
                    
                    } reader.close (); for (int row=0;row<csvlist.size (); row++) {String cell = Csvlist.get (ro W) [0];
                        
                    Obtain the data System.out.println (cell) in column No. 0 of row rows;
                    
    }                
               }catch (Exception ex) {System.out.println (ex);
               }/** * Write CSV file/public void Writecsv () {
                    try {String Csvfilepath = "C:/test.csv";
                    Csvwriter WR =new Csvwriter (Csvfilepath, ', ', Charset.forname ("Sjis"));                    
                    String[] contents = {"AAAAA", "bbbbb", "CCCCCC", "ddddddddd"};
                    Wr.writerecord (contents);
                Wr.close ();
                catch (IOException e) {e.printstacktrace (); }
           }

For more functions, see the javacsv2.0/doc/index.html description. I think it's useful to see Javacsv2.0/src/alltests.java.

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.