We often save the files in the Excel format as the CSV format for easy uploading and modification, but when the data contains a comma and double quotation marks, Excel will block the field with a double quotes and put "change to" in the data. " It has brought difficult to the resolution. I wrote the following functions to parse such a string:
Testsplitcsv.java:
import java.util.Vector; class testSplitCSV {/ ** * Split one line of csv file * @return a String array results * / public static String [] splitCSV (String src) throws Exception {if (src == null || Src.equals (")) Return New String [0]; StringBuffer ST = New StringBuffer (); Vector Result = New Vector (); boolean beginwith quote = false; for (int i = 0; i = src.length ()) {Result.AddeElement (ST. Totring ()); ST = new stringbuffer (); beginwith quote = false;} else {ch = src.charat (i); if (ch == '/ ") {st.append (ch);} else IF CH == ') {Result.addelement (st.Tostring ()); ST = new stringbuffer (); beginwith quote = false;} else {throw new exception ("Single Double-Quote Char Mustn't Exist In File) (Result.Size () 1) "While it is beginning with quote / nchar at: i);}}} else}}} else}}} else}}} == 0)}}} == 0) {beginwith quote = true Throw New Exception ("Quote Cannot EXIST I Filed Which Doesn't Begin with Quote! / Nfield:" (RESUELD: ");}} Else IF (CH == ',') {IF Beginwith quote) {st.append (ch); else {result.addelement (st.toString ()); ST = new stringbuffer (); beginwith quote = false;}} else {st.append (ch);}} St.LENGTH ()! = 0) {if (beginwith quote) {throw new exception ("Last Field Is Begin with But Not End With Double Quote");} else {result.addelement (st.toString ());
}} String RS [] = new string [result.size ()]; for (int i = 0; i