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>
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