Split CSV files using Java

xiaoxiao2021-03-06  40

CSV is divided into a comma-divided file, the result of this class is basically the same as Excel opens the CSV.

Different is that Excel is also the content of a project for the carriage return, but here is divided in behavioral units.

AA, BB, CC, DD -----> | AA | BB | CC | DD is divided into four elements of AA, BB, CC, 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.Itrator;

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);} = f (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);

转载请注明原文地址:https://www.9cbs.com/read-52022.html

New Post(0)