1 18 package com.Ostermiller.util; 19 20 import java.io.*; 21 22 30 class CSVTest { 31 public static void main (String [] args) throws IOException{ 32 File f = new File("CSVTest.txt"); 33 FileOutputStream out = new FileOutputStream(f); 34 CSVPrinter csvOut = new CSVPrinter(out, '#'); 35 36 csvOut.printlnComment("Comma Separated Value Test"); 37 csvOut.println(); 38 csvOut.printlnComment("Five Cities"); 39 csvOut.println(new String [] { 40 "Boston", 41 "San Francisco", 42 "New York", 43 "Chicago", 44 "Houston", 45 }); 46 csvOut.println(); 47 csvOut.println(""); csvOut.println(new String [] { 49 "Two\nTokens", 50 "On the\nSame Line" 51 }); 52 csvOut.printlnComment("A two line comment\njust to see that it works"); 53 54 CSVParser shredder = new CSVParser(new StraightStreamReader(new FileInputStream(f))); 55 shredder.setCommentStart("#;!"); 56 shredder.setEscapes("nrtf", "\n\r\t\f"); 57 String t; 58 while ((t = shredder.nextValue()) != null) { 59 if (t.length() == 1){ 60 System.out.println("" + shredder.lastLineNumber() + " " + (int)(t.charAt(0))); 61 } else { 62 System.out.println("" + shredder.lastLineNumber() + " " + t); 63 } 64 } 65 66 try { 67 String normalInput = ",\"a\",\",\t'\\\"\""; 68 String [][] normalOutput = new String [][]{{"", "a", ",\t'\""}}; 69 shredder = new CSVParser(new StringReader(normalInput)); 70 compare("normal", normalOutput, shredder.getAllValues()); 71 72 String tabInput = "\t\"a\"\t\",\t'\\\"\""; 73 shredder = new CSVParser(new StringReader(tabInput)); 74 shredder.changeDelimiter('\t'); 75 compare("tabs", normalOutput, shredder.getAllValues()); 76 77 String aposInput = ",'a',',\t\\'\"'"; 78 shredder = new CSVParser(new StringReader(aposInput)); 79 shredder.changeQuote('\''); 80 compare("apostrophes", normalOutput, shredder.getAllValues()); 81 82 String swappedInput = "\",a,\",\\,\t'\\\","; 83 shredder = new CSVParser(new StringReader(swappedInput)); 84 shredder.changeDelimiter('\t'); 85 shredder.changeQuote(','); 86 shredder.changeDelimiter('"'); 87 compare("commas and quotes swapped", normalOutput, shredder.getAllValues()); 88 89 normalInput = "\"test\\\\\",test"; 90 normalOutput = new String [][]{{"test\\", "test"}}; 91 shredder = new CSVParser(new StringReader(normalInput)); 92 compare("backslash at end of quoted", normalOutput, shredder.getAllValues()); 93 } catch (Exception x){ 94 System.err.println(x.getMessage()); 95 System.exit(1); 96 } 97 98 } 99 100 private static void compare(String testName, String [][] a, String [][] b){ 101 if (a.length != b.length) { 102 System.err.println(testName + ": unexpected number of lines"); 103 System.exit(1); 104 } 105 for(int i=0; i<a.length; i++){ 106 if (a[i].length != b[i].length) { 107 System.err.println(testName + ": unexpected number of values in line: " + b[i].length); 108 System.exit(1); 109 } 110 for (int j=0; j<a[i].length; j++){ 111 if (!a[i][j].equals(b[i][j])) { 112 System.err.println(a[i][j]); 113 System.err.println(b[i][j]); 114 System.err.println(testName + ": values do not match."); 115 System.exit(1); 116 } 117 } 118 } 119 120 } 121 } 122 | Popular Tags |