Example of using Opencsv to read and write CSV files in Java _java

Source: Internet
Author: User

Opencsv is a simple Java class library for parsing CSV files that encapsulates the output and reading of CSV format files, automates the processing of special characters in CSV format, and most importantly opencsv can be used for commercialization (commercial-friendly). Specific methods of use:

Read CSV file

1, the use of iterator way to read

Copy Code code as follows:

Csvreader reader = new Csvreader (New FileReader ("Yourfile.csv"));
String [] nextline;
while ((nextline = Reader.readnext ())!= null) {
Nextline[] is a array of values from the line
System.out.println (Nextline[0] + nextline[1] + "etc ...");
}

2. Use list
Copy Code code as follows:

Csvreader reader = new Csvreader (New FileReader ("Yourfile.csv"));
List myentries = Reader.readall ();

Write CSV file

1, similar to FileReader

Copy Code code as follows:

Csvwriter writer = new Csvwriter (New FileWriter ("Yourfile.csv"), ' t ');
Feed in your array (or convert your data to an array)
string[] entries = "First#second#third". Split ("#");
Writer.writenext (entries);
Writer.close ();

Custom Separator

1, custom separator, such as the use of tab separator

Copy Code code as follows:

Csvreader reader = new Csvreader (New FileReader ("Yourfile.csv"), ' t ');

2, also can use the escape character
Copy Code code as follows:

Csvreader reader = new Csvreader (New FileReader ("Yourfile.csv"), ' \ t ', ' \ ');

3, from the second (n) line to start parsing
Copy Code code as follows:

Csvreader reader = new Csvreader (New FileReader ("Yourfile.csv"), ' t ', ' \ ', 2;
Dump SQL Tables

Java.sql.ResultSet Myresultset = .....
Writer.writeall (Myresultset, includeheaders);


Generate JavaBeans
Copy Code code as follows:

Columnpositionmappingstrategy strat = new Columnpositionmappingstrategy ();
Strat.settype (Yourorderbean.class);
string[] columns = new string[] {"Name", "OrderNumber", "id"}; The fields to bind does in your JavaBean
strat.setcolumnmapping (columns);

Csvtobean csv = new Csvtobean ();
List List = Csv.parse (strat, Yourreader);

Finish

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.