horiuchi #4219(2007/11/19 04:59 GMT) [ Java ] Rating1/1=1.00
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 76 77 78 79
import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; public class Answer33 { public static void main(String[] args) { String str = "\"aaa\",\"b\nbb\",\"ccc\",zzz,\"y\"\"Y\"\"y\",xxx"; CSVDataReader reader = new CSVDataReader(new StringReader(str)); try { while (true) { int cell = reader.getCellNumber(); String s = reader.readCell(); if (s == null) break; System.out.println(cell + " => " + s); } } catch (IOException e) { e.printStackTrace(); } } } class CSVDataReader extends BufferedReader { private int cellCount_; public CSVDataReader(Reader reader) { super(reader); cellCount_ = 1; } public int getCellNumber() { return cellCount_; } public String readCell() throws IOException { int c = read(); if (c < 0) return null; cellCount_++; StringBuilder builder = new StringBuilder(); boolean quote = (c == '"'); if (!quote) { if (c == '\r' || c == '\n') return ""; builder.append((char) c); } OUTER: while ((c = read()) >= 0) { if (quote) { INNER: switch (c) { case '"': int next = read(); switch (next) { case '"': builder.append('"'); break INNER; case ',': break OUTER; default: throw new IllegalStateException(); } default: builder.append((char) c); } } else { switch (c) { case ',': case '\r': case '\n': break OUTER; case '"': throw new IllegalStateException(); default: builder.append((char) c); } } } return builder.toString(); } }
Rating1/1=1.00-0+
[ reply ]
horiuchi
#4219()
[
Java
]
Rating1/1=1.00
Rating1/1=1.00-0+
[ reply ]