亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號