admin
2019-10-10 e19ce4be094d93f68bdb6ee1c28e9caa502bf2c4
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.yeshi.fanli.util;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * CsvParser
 */
public class CsvParser {
    // Saved input CSV file pathname
    private String inputCsvFile;
 
    // Space mark , ; : etc.
    private String spaceMark = ",";
 
    /**
     * Contructor
     * 
     * @param inputCsvFile
     */
    public CsvParser(String inputCsvFile, String spaceMark) {
        this.inputCsvFile = inputCsvFile;
        this.spaceMark = spaceMark;
    }
 
    /**
     * Contructor
     * 
     * @param inputCsvFile
     */
    public CsvParser(String inputCsvFile) {
        this.inputCsvFile = inputCsvFile;
        this.spaceMark = ",";
    }
 
    /**
     * Get parsed array from CSV file
     * 
     * @return
     */
    public Object[] getParsedArray() throws Exception {
        List<List<String>> retval = new ArrayList<List<String>>();
 
        String regExp = getRegExp();
        BufferedReader in = new BufferedReader(new FileReader(this.inputCsvFile));
        String strLine;
        String str = "";
 
        while ((strLine = in.readLine()) != null) {
            Pattern pattern = Pattern.compile(regExp);
            Matcher matcher = pattern.matcher(strLine);
            List<String> listTemp = new ArrayList<String>();
            while (matcher.find()) {
                str = matcher.group();
                str = str.trim();
 
                if (str.endsWith(spaceMark)) {
                    str = str.substring(0, str.length() - 1);
                    str = str.trim();
                }
 
                if (str.startsWith("\"") && str.endsWith("\"")) {
                    str = str.substring(1, str.length() - 1);
                    if (CsvParser.isExisted("\"\"", str)) {
                        str = str.replaceAll("\"\"", "\"");
                    }
                }
 
                if (!"".equals(str)) {
                    listTemp.add(str);
                }
            }
 
            // Add to retval
            retval.add(listTemp);
        }
        in.close();
 
        return retval.toArray();
    }
 
    /**
     * Regular Expression for CSV parse
     * 
     * @return
     */
    private String getRegExp() {
        // TODO 未完
 
        StringBuffer strRegExps = new StringBuffer();
 
        return strRegExps.toString();
    }
 
    /**
     * If argChar is exist in argStr
     * 
     * @param argChar
     * @param argStr
     * @return
     */
    private static boolean isExisted(String argChar, String argStr) {
 
        boolean blnReturnValue = false;
        if ((argStr.indexOf(argChar) >= 0) && (argStr.indexOf(argChar) <= argStr.length())) {
            blnReturnValue = true;
        }
        return blnReturnValue;
    }
 
    /**
     * Test
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        CsvParser parser = new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test CSV Files\\dummydata_not quoted_1.csv");
        // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test
        // CSV Files\\dummydata_not quoted_2.csv");
        // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test
        // CSV Files\\dummydata_quoted.csv");
        // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test
        // CSV Files\\dummydata_quoted_2.csv");
 
        // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test
        // CSV Files\\dummydata_1.csv",";");
        // CsvParser parser=new CsvParser("C:\\Users\\IBM_ADMIN\\Desktop\\Test
        // CSV Files\\dummydata_2.csv",":");
 
        Object[] arr = parser.getParsedArray();
        // System.out.println(arr);
 
        for (Object obj : arr) {
            System.out.print("[");
 
            List<String> ls = (List<String>) obj;
 
            for (String item : ls) {
                System.out.println(item + ",");
            }
 
            System.out.println("],");
        }
    }
}