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

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

http://img1.mukewang.com/62a060b80001808706040186.jpg

http://img3.mukewang.com/62a060be000173b704860290.jpg

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));




慕尼黑5688855
浏览 200回答 1
1回答

紫衣仙女

您可以使用 aMap将流量级别存储为键,并将 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) {&nbsp; &nbsp; // create a Path object from the path to your file&nbsp; &nbsp; Path csvFilePath = Paths.get("Y:\\our\\path\\to\\file.csv");&nbsp; &nbsp; // create a data structure that stores data rows per traffic level&nbsp; &nbsp; Map<Integer, List<DataRow>> dataRowsPerTrafficLevel = new TreeMap<Integer, List<DataRow>>();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; // read all the lines of the file&nbsp; &nbsp; &nbsp; &nbsp; List<String> lines = Files.readAllLines(csvFilePath);&nbsp; &nbsp; &nbsp; &nbsp; // iterate all the lines, skipping the header line&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < lines.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // split the lines by the separator (WHICH MAY DIFFER FROM THE ONE USED HERE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] lineValues = lines.get(i).split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // store the value from column 6 (index 5) as the traffic level&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int trafficLevel = Integer.valueOf(lineValues[5]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if the map already contains this key, just add the next data row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dataRowsPerTrafficLevel.containsKey(trafficLevel)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataRow dataRow = new DataRow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataRow.subId = lineValues[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataRow.countryId = lineValues[2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataRow.type = lineValues[3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataRowsPerTrafficLevel.get(trafficLevel).add(dataRow);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* otherwise create a list, then a data row, add it to the list and put it in&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* the map along with the new key&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<DataRow> dataRows = new ArrayList<DataRow>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataRow dataRow = new DataRow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataRow.subId = lineValues[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataRow.countryId = lineValues[2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataRow.type = lineValues[3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataRows.add(dataRow);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataRowsPerTrafficLevel.put(trafficLevel, dataRows);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // print the result&nbsp; &nbsp; &nbsp; &nbsp; dataRowsPerTrafficLevel.forEach((trafficLevel, dataRows) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("For TrafficLevel " + trafficLevel + " there are " + dataRows.size()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " data rows in the csv file");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}/*&nbsp;* small holder class that just holds the values of columns 3, 4 and 5.&nbsp;* If you want to have distinct values, make this one a full POJO implementing Comparable&nbsp;*/static class DataRow {&nbsp; &nbsp; String subId;&nbsp; &nbsp; String countryId;&nbsp; &nbsp; String type;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java