简体   繁体   English

根据某些条件写入 java 中的 csv 文件

[英]writing to a csv file in java based on some condition

I am fetching the data by reading from a csv file and storing it as List<List<String<>> "data".我通过从 csv 文件中读取数据并将其存储为 List<List<String<>>“数据”来获取数据。 While trying to write that data to another csv file I need to Check a condition whether id in this "rowdata" matches with id column of another csv file or not.在尝试将该数据写入另一个 csv 文件时,我需要检查此“行数据”中的 id 是否与另一个 csv 文件的 id 列匹配的条件。 Please help in how to check this condition.请帮助如何检查这种情况。

The condition is data.id=value.id;// value is data from another csv in the form List<List<String<>> value条件是 data.id=value.id;// 值是来自另一个 csv 的数据,形式为 List<List<String<>> value

public void writeRecords(List<List<String>> data) throws IOException {
    
    FileWriter csvWriter1 = new FileWriter(OUTPUT_PATH);
    Cable c1=new Cable();
    List<List<String>> value=c1.readRecords();

    for (List<String> rowData : data) {
                csvWriter1.append(String.join(",", rowData));
                csvWriter1.append("\n");
    }
    csvWriter1.flush();
    csvWriter1.close();
    
}

Would not suggest you to operate with data structure like that List<List<String<>> "data" .However, assumed all your lists are sorted and you don't care about code complexity level - in your situation you can do smth like that to compare data from your two lists:不建议您使用List<List<String<>> "data"类的数据结构。但是,假设您的所有列表都已排序并且您不关心代码复杂程度 - 在您的情况下,您可以这样做用于比较两个列表中的数据:

for (int i =0; i<value.size(); i++){
        for (int j = 0; j<value.get(i).size(); j++) {
            if(value.get(i).get(j)==data.get(i).get(j));
            //write data to another csv
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM