CSV is a comma-separated file Java

Source: Internet
Author: User

CSV is a file separated by commas (,). The results of this class are basically the same as those when CSV is opened in Excel.

The difference is that excel uses carriage return as the content of a project, which is separated by the unit of action.

AA, BB, CC, DD -----> | AA | BB | CC | dd is divided into four elements: AA, BB, CC, and DD.

"AA", "BB", "cc", "DD" -----> | AA | BB | CC | dd

"A" A "," a "B" --------> | A "A | a" "B

"A" B "," C "" D "E", F "" E -------> | AB "| C" de "| f" "E |

// Generated by fason

Package fason;

Import java. Io. bufferedreader;
Import java. Io. filereader;
Import java. util. arraylist;
Import java. util. iterator;

Public class csvparse {
Public static arraylist parsecsv (string linestr ){
Arraylist collist = new arraylist ();
Stringbuffer sb = new stringbuffer (linestr );
Int colflg = 0;
Boolean comaflg = false;
Stringbuffer colbuf = new stringbuffer ();
While (sb. Length ()> 0 ){
Char c = sb. charat (0 );
If (comaflg = false ){
If (C = ','){
Collist. Add (colbuf. tostring ());
Colbuf = new stringbuffer ();
} Else if (C = '\"'){
Colflg = 1;
Char tmpc = '\ 0 ';
If (sb. Length ()> 1 ){
Tmpc = sb. charat (1 );
}
If (tmpc = '\"'){
Colflg = 2;
SB. deletecharat (1 );
}
Comaflg = true;
} Else {
Colflg = 2;
Colbuf. append (C );
Comaflg = true;
}
} Else {
If (colflg = 2 & C = ','){
Collist. Add (colbuf. tostring ());
Colbuf = new stringbuffer ();
Comaflg = false;
} Else if (colflg = 1 & C = '\"'){
Char tmpc = '\ 0 ';
If (sb. Length ()> 1 ){
Tmpc = sb. charat (1 );
}
If (tmpc = '\"'){
Colbuf. append (tmpc );
SB. deletecharat (1 );
} Else {
Colflg = 2;
}
} Else {
Colbuf. append (C );
}
}
SB. deletecharat (0 );
}
Collist. Add (colbuf. tostring ());
Return collist;
}
Public static arraylist parsecsvfile (string filepath ){
Bufferedreader BR;
Try {
BR = new bufferedreader (New filereader (filepath ));
} Catch (exception e ){
System. Out. Print (E. getmessage ());
Return NULL;
}
Arraylist linelist = new arraylist ();
Try {
String S;
While (S = Br. Readline ())! = NULL ){
Linelist. Add (s );
}
} Catch (exception e ){
System. Out. Print (E. getmessage ());
}
Arraylist linecollist = new arraylist ();
Iterator ite = linelist. iterator ();
While (ITE. hasnext ()){
String linestr = (string) ite. Next ();
Linecollist. Add (parsecsv (linestr ));
}
Return linecollist;
}
}

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.