Parsing del (CSV-like) format files using Apache Commons CSV

Source: Internet
Author: User

Del format is similar to CSV, my test file here Test.del is a set of data from the DB2 database.
The MAVEN coordinates of the Apache Commons CSV are:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv --><dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-csv</artifactId>    <version>1.5</version></dependency>

Test procedure:

Package test;import Java.io.File;import Java.io.FileInputStream;import java.io.IOException;import Java.io.InputStream;import Java.io.InputStreamReader;import Java.io.Reader;import java.util.ArrayList;import java.util.List;import Org.apache.commons.csv.CSVFormat;import Org.apache.commons.csv.CSVParser;import Org.apache.commons.csv.CSVRecord; Public classDelfileparser { Public Staticlist< list<string> >Parsefile(File file) {List reslist =Newarraylist< list<string> > ();Try{InputStream is =NewFileInputStream (file); InputStreamReader reader =NewInputStreamReader (IS,"GB2312"); Csvparser parser =New Csvparser(Reader, Csvformat.)DEFAULT); for(Csvrecord Record:parser) {List<string> tmplist =NewArraylist<string> (); for(String S:record) {if(s! =NULL) s = S.Trim(); Tmplist.Add(s); } reslist.Add(tmplist); } is.Close(); Reader.Close();returnReslist; }Catch(IOException e) {e.Printstacktrace();return NULL; }    } Public Static void Main(string[] args) {list< list<string> > reslist =Parsefile(NewFile ("D:\\Test.del "));//Test         for(list<string> tmplist:reslist) { for(String s:tmplist) {System. out.Print(S +","); } System. out.println(); }    }}

Using the default format does not use the first row as the header.

For more information see: http://commons.apache.org/proper/commons-csv/user-guide.html

Parsing del (CSV-like) format files using Apache Commons CSV

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.