多值行的java csv值比较

时间:2015-03-17 10:01:31

标签: java csv

我有一个CSV文件,其中包含以下字段。所以在这里我只删除了必需的字段。

"id.attr.id","id.attr.id1","id.attr.id2","id.attr.id7","id.attr.arry","id.attr.arry1","id.attr.arry2","id.attr.arry5","id.attr.arry4"

"0",,,"8",,,"8",,"0"
"3",,,"7",,,"7",,"3"
"4",,,"8",,,"8",,"4"
"5",,"7","5",,,"7",,"5"
"4",,,"8",,,"4",,"8"
"3",,,"7",,,"7",,"3"

现在来定义问题。所以对于" id.attr.id"对于这些字段("id.attr.id","id.attr.id1","id.attr.id2","id.attr.id7") & "id.attr.arry"分组的组意味着("id.attr.arry","id.attr.arry1","id.attr.arry2","id.attr.arry5","id.attr.arry4")我需要返回所有多字段值。

所以我所做的就是这个。

//to map the header

private void parseUsingOpenCSV(String filename) {
    try {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
        String line;
        StringBuilder builder = new StringBuilder();
        int count = 0;
        boolean processHeader = false;
        Map<Integer, String> columnIndexMap = new HashMap<Integer, String>();
        while ((line = bufferedReader.readLine()) != null) {
            //here we process only header line and prepare columnIndexMap
            if (!processHeader) {
                processHeader = true;
                CSVReader reader = new CSVReader(new StringReader(line));
                String[] row = reader.readNext();
                for (int i = 0; i < row.length; i++) {
                    String columnName = row[i];
                    if (columnName.matches("id.attr.id(.*)")) {
                        columnIndexMap.put(i, columnName);
                    }
                }
            } else {
                //here we process al data lines and maintain a buffer
                count++;
                builder.append(line).append("\n");
                if (count % 500 == 0) {
                    process(builder.toString(), columnIndexMap);
                    builder = new StringBuilder();
                }
            }
        }
        if (builder.length() > 0) {
            process(builder.toString(), columnIndexMap);
        }

    } catch (FileNotFoundException e) {
        System.err.println(e.getMessage());
    } catch (IOException e) {
        System.err.println(e.getMessage());
    }
}

private void process(String records, Map<Integer, String> columnIndexMap) throws IOException {
    CSVReader reader = new CSVReader(new StringReader(records));
    Map<String, String> columnValueMap;
    for (String[] row : reader.readAll()) {
        columnValueMap = new HashMap<String, String>();
        for (int i = 0; i < row.length; i++) {
            if (columnIndexMap.containsKey(i)) {
                String value = row[i];
                if (value.trim().length() > 0) {
                    columnValueMap.put(columnIndexMap.get(i), value);
                }
            }
        }
    //    if (columnValueMap >i) {

            System.out.println("columnValueMap = " + columnValueMap);


   //     }
    }
}
}

所以这个程序返回&#34; id.attr.id&#34;中的所有值。组。     现在columnValueMap返回的是所有映射列的值。  如何获得多值结果

预期结果如下:

所以程序应该从上面给出的csv。

返回
4,8
5,7,51
4,8
3,7

您也可以向我提供可能需要做的更改,以便检查来自&#34; id.attr.id&#34;组与&#34; id.attr.arry&#34;匹配如果是,那么我们也需要将它们归还

0
3
4,8
5,7
4,8
3,7

0 个答案:

没有答案