It describes, parsing a CSV file without using StringTokenizer in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import java.io.*; public class Test { public static void main(String args[]) throws IOException { String thisLine; String[] fullText = new String[ 100 ]; //number of possible lines in your file int counter = 0 ; String fName = "C:/Users/Desktop/345_APPLE.txt" ; FileInputStream fis = new FileInputStream(fName); DataInputStream myInput = new DataInputStream(fis); while ((thisLine = myInput.readLine()) != null ) { System.out.println( "---------------------------------\nBegin Parsing" ); int sPos = 0 ; int ePos = 1 ; int tDelim = 0 ; String d = "" ; int colCount = 0 ; int collCount = 0 ; boolean fMe = false ; boolean finalCol = false ; while ( ePos < thisLine.length()) { sPos = thisLine.indexOf( "," , ePos); tDelim = thisLine.indexOf( ",\"" , ePos); if (tDelim == ePos) { fMe = true ; collCount = ePos + 1 ; while (collCount <= thisLine.indexOf( "\"," )) { collCount += 1 ; } colCount += 1 ; sPos = collCount; } if (colCount == 47 ) { //Get Max count of columns finalCol = true ; collCount = ePos + 1 ; while (collCount <= thisLine.lastIndexOf( "\"" )) { collCount += 1 ; } colCount += 1 ; sPos = collCount; } if (sPos == - 1 ) { d = thisLine.substring(ePos,thisLine.length()); System.out.println(d); break ; } else if (ePos == 1 ) { d = thisLine.substring(ePos- 1 ,sPos); System.out.println(d); } else { if (fMe) { d = thisLine.substring(ePos + 1 , sPos); fMe = false ; } else if (finalCol) { d = thisLine.substring(ePos, sPos); finalCol = false ; } else { d = thisLine.substring(ePos, sPos); } System.out.println(d); } colCount += 1 ; ePos = sPos + 1 ; } counter++; fullText[counter] = thisLine; System.out.println( "\nEnd Parsing\n---------------------------------\n\n" ); } } } |