Use the Javacsv API to read and write CSV files

Source: Internet
Author: User

http://blog.csdn.net/loongshawn/article/details/53423121

http://javacsv.sourceforge.net/

Reprint please indicate source-author @loongshawn:http://blog.csdn.net/loongshawn/article/details/53423121

1 background

There are many ways to read and write CSV files, and here is a way to read and write CSV files using a third-party jar package.

In the daily work, if there are ready-made third-party toolkit, we'd better use off-the-shelf, to improve efficiency and reduce the abnormal rate, mature three-party toolkit more reliable.

2 Javacsv API

Maven Dependency

<!-- https://mvnrepository.com/artifact/net.sourceforge.javacsv/javacsv --><dependency>    <groupId>net.sourceforge.javacsv</groupId>    <artifactId>javacsv</artifactId>    <version>2.1</version></dependency>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

API Description Document: http://javacsv.sourceforge.net/

Javacsv Official website: https://sourceforge.net/projects/javacsv/

Javacsv-2.1.jar Click to download

3 Code Implementation 3.1 read operation
public  static void read () {String FilePath =  "xxx.csv"; try {//create CSV Read object Csvreader Csvreader = new Csvreader (FilePath); //read the table header csvreader.readheaders (); while (Csvreader.readrecord ()) {//read a whole line System.out.println ( Csvreader.getrawrecord ()); //read a column of this row System.out.println (Csvreader.get (catch (IOException e) {e.printstacktrace ();}}       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st

3.2 Write operations
PublicStaticvoid write () {String FilePath =  "/Use Rs/dddd/test.csv "; try {//Create a CSV write object csvwriter csvwriter = new csvwriter (Filepath, ', ', Charset.forname (" GBK ")); //csvwriter csvwriter = new Csvwriter (FilePath); //write header string[] headers = { "numbering",  "name" ,  "Age"}; string[] content = { "12365",  "Zhang Shan", catch (IOException e) {e.printstacktrace ();}}       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

Top
8
Step
2

Use the Javacsv API to read and write CSV 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.