JAVA:使用流api过滤csv中的特定列

时间:2019-05-23 17:25:32

标签: java

我已经从ftp服务器读取了CSV文件,该文件作为单元格值,格式为

"Col 1","Col 2","Col 3",”Col 4”,”Col 5”,”Col 6”,……”Col 80” 
"","","Yes",”A,B,C”,"12345","0",….. “XYZ 20190523 10:46:00 PM” 
"","","Yes",”D,E,F”,"12345","1",….. “XYZ 20190523 10:46:00 PM” 
"","","Yes",”X,Y”,"6789","0",….. “XYZ 20190523 10:46:00 PM”

需要应用拆分,过滤第6列的列并打印忽略第6列中0记录的数据

我尝试了以下方法:

       BufferedReader br = new BufferedReader(new 
       InputStreamReader(stream));
       String line = null;
       while ((line = br.readLine()) != null) {
       String[] arr = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
          for (int i=0; i<arr.length;i++) {
              if(arr[i].equals("\"Col 6\"")) {
                  index=i;                         
                }
              System.out.println("CSV Data is :"+ arr[i]+"");
           }                
       }
   }      

需要使用以下任一项: -地图 -哈希图 -过滤器(lambda函数)-将应用col 6> 0这样的条件并相应地打印数据 -getter和setter(如果有的话)

建议使用其他方法或示例代码

1 个答案:

答案 0 :(得分:0)

但是需要修复CSV:

"Col 1","Col 2","Col 3","Col 4","Col 5","Col 6","Col 80"
"","","Yes","A,B,C","12345","0","XYZ 20190523 10:46:00 PM"
"","","Yes","D,E,F","12345","1","XYZ 20190523 10:46:00 PM"
"","","Yes","X,Y","6789","0","XYZ 20190523 10:46:00 PM"

以下示例:

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class SampleCSVParser {
    public List<Map<String, String>> parse(Path csvPath) throws IOException {
        Reader in = new FileReader(csvPath.toFile());

        Iterable<CSVRecord> records = CSVFormat.DEFAULT
                .withFirstRecordAsHeader()
                .withIgnoreEmptyLines(true)
                .withDelimiter(',')
                .withTrim()
                .parse(in);


        return StreamSupport
                        .stream(records.spliterator(), false)
                        .filter(csvRecord -> Integer.parseInt(csvRecord.get("Col 6"))>0)
                        .map(csvRecord -> csvRecord.toMap().entrySet().stream()
                                .collect(Collectors.toMap(
                                        e -> e.getKey(),
                                        e -> e.getValue()
                                )))
                        .collect(Collectors.toList());
    }
}