为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕尼黑5688855 2022-06-08 16:40:59
我有下面的 Java 代码和 TestData.csv(输入文件),我的预期输出如下所示。但它显示了我尝试了很多的实际计数。任何人对此都有任何想法。任何帮助都是有价值的。根据列数据,我想要特定值的计数。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));
查看完整描述

1 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

您可以使用 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) {

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

}


查看完整回答
反对 回复 2022-06-08
  • 1 回答
  • 0 关注
  • 199 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号