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