Java提取基于csv文件中列数据的计数

时间:2019-02-06 12:05:04

标签: java csv

我有下面的Java代码和TestData.csv(输入文件) 我的预期输出如下所示。但它显示实际数量 我尝试了很多。任何人对此都有任何想法。任何帮助都是有价值的。基于列数据,我希望对特定值进行计数。

enter image description here enter image description here

package com;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import com.opencsv.CSVWriter;
import com.opencsv.CSVReader;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;

public class TestDataProcess {
    public static void main(String args[]) throws IOException {
        processData();
    }

    public static void processData() {
        String[] trafficDetails;
        int locColumnPosition, subCcolumnPosition, j, i, msgTypePosition, k, m, trafficLevelPosition;
        String masterCSVFile, dayFolderPath;
        String[] countryID = { "LOC1" };
        String[] subID = { "S1" };
        String[] mType = { "MSG1" };
        String[] trafficLevel = { "1", "2", "3" };
        String columnNameLocation = "CountryID";
        String columnNameSubsystem = "SubID";
        String columnNameMsgType = "Type";
        String columnNameAlrmLevel = "TrafficLevel";
        masterCSVFile = "D:\\TestData.csv";
        dayFolderPath = "D:\\output\\";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd_MM_yyyy");
        LocalDateTime now = LocalDateTime.now();
        System.out.println(dtf.format(now));
        int count = 0;
        for (i = 0; i < countryID.length; i++) {
            count = 0;
            for (j = 0; j < subID.length; j++) {
                count = 0;
                String locaIdSubsysId = dtf.format(now) + "_" + countryID[i] + "_" + subID[j] + ".csv";
                try (CSVWriter csvWriter = new CSVWriter(new FileWriter(dayFolderPath + locaIdSubsysId, true));
                        CSVReader csvReader = new CSVReader(new FileReader(masterCSVFile));) {
                    trafficDetails = csvReader.readNext();
                    csvWriter.writeNext(trafficDetails);
                    locColumnPosition = getHeaderLocation(trafficDetails, columnNameLocation);
                    subCcolumnPosition = getHeaderLocation(trafficDetails, columnNameSubsystem);
                    msgTypePosition = getHeaderLocation(trafficDetails, columnNameMsgType);
                    trafficLevelPosition = getHeaderLocation(trafficDetails, columnNameAlrmLevel);
                    while ((trafficDetails = csvReader.readNext()) != null && locColumnPosition > -1
                            && subCcolumnPosition > -1) {
                        for (k = 0; k < mType.length; k++) {
                            for (m = 0; m < trafficLevel.length; m++) {
                                if (trafficDetails[locColumnPosition].matches(countryID[i])
                                        & trafficDetails[subCcolumnPosition].matches(subID[j])
                                        & trafficDetails[trafficLevelPosition].matches(trafficLevel[m])
                                        & trafficDetails[msgTypePosition].matches(mType[k]))

                                {
                                    count = count + 1;
                                    csvWriter.writeNext(trafficDetails);

                                }
                            }
                        }
                    }
                } catch (Exception ee) {

                    ee.printStackTrace();
                }
            }
        }
    }

    public static int getHeaderLocation(String[] headers, String columnName) {
        return Arrays.asList(headers).indexOf(columnName);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用Map将流量级别作为键存储,并将csv文件中的所有行存储在List中作为其值。然后只需打印List的大小即可。

请参见以下示例,并查看代码注释:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class ExampleMain {

public static void main(String[] args) {
    // create a Path object from the path to your file
    Path csvFilePath = Paths.get("Y:\\our\\path\\to\\file.csv");
    // create a data structure that stores data rows per traffic level
    Map<Integer, List<DataRow>> dataRowsPerTrafficLevel = new TreeMap<Integer, List<DataRow>>();

    try {
        // read all the lines of the file
        List<String> lines = Files.readAllLines(csvFilePath);

        // iterate all the lines, skipping the header line
        for (int i = 1; i < lines.size(); i++) {
            // split the lines by the separator (WHICH MAY DIFFER FROM THE ONE USED HERE)
            String[] lineValues = lines.get(i).split(",");
            // store the value from column 6 (index 5) as the traffic level
            int trafficLevel = Integer.valueOf(lineValues[5]);
            // if the map already contains this key, just add the next data row
            if (dataRowsPerTrafficLevel.containsKey(trafficLevel)) {
                DataRow dataRow = new DataRow();
                dataRow.subId = lineValues[1];
                dataRow.countryId = lineValues[2];
                dataRow.type = lineValues[3];
                dataRowsPerTrafficLevel.get(trafficLevel).add(dataRow);
            } else {
                /* otherwise create a list, then a data row, add it to the list and put it in
                 * the map along with the new key
                 */
                List<DataRow> dataRows = new ArrayList<DataRow>();
                DataRow dataRow = new DataRow();
                dataRow.subId = lineValues[1];
                dataRow.countryId = lineValues[2];
                dataRow.type = lineValues[3];
                dataRows.add(dataRow);
                dataRowsPerTrafficLevel.put(trafficLevel, dataRows);
            }
        }

        // print the result
        dataRowsPerTrafficLevel.forEach((trafficLevel, dataRows) -> {
            System.out.println("For TrafficLevel " + trafficLevel + " there are " + dataRows.size()
                    + " data rows in the csv file");
        });

    } catch (IOException e) {
        e.printStackTrace();
    }
}

/*
 * small holder class that just holds the values of columns 3, 4 and 5.
 * If you want to have distinct values, make this one a full POJO implementing Comparable
 */
static class DataRow {
    String subId;
    String countryId;
    String type;
}