我有以下csv文件:
No_Reduction No_Reduction No_Reduction No_Reduction
1 15 1 9
0 7 0 0
3 1 5 1
我想读取文件并计算每行的总和。第一行是我在项目中使用的一些技术(忘记重复)。为此,我做了以下工作:
BufferedReader readerBuffer = new BufferedReader(new FileReader("location"));
BufferedWriter bw = new BufferedWriter(new FileWriter("location",true));
while ((line = readerBuffer.readLine()) != null) {
if(line.contains("Reduction")) {
bw.write(convertCSVtoArrayList(line).get(0)); //I want to write the technique name in the new file
bw.write("\n");
}else if(!line.contains("NA")) {
ArrayList<String> data_per_class = convertCSVtoArrayList(line);
List<Double> doubleList = data_per_class.stream().map(Double::valueOf).collect(Collectors.toList()); //this is line 46
double sum = this.calculateSum(doubleList);
bw.write(sum);
bw.write("\n");
}
}
bw.close();
转换列表方法:
public static ArrayList<String> convertCSVtoArrayList(String pathCSV) {
ArrayList<String> result = new ArrayList<String>();
if (pathCSV != null) {
String[] splitData = pathCSV.split("\\s*,\\s*");
for (int i = 0; i < splitData.length; i++) {
if (!(splitData[i] == null) || !(splitData[i].length() == 0)) {
result.add(splitData[i].trim());
}
}
}
return result;
}
请注意,如果我删除了第一行(包含的行),则错误将消失!No_Reduction
我该如何解决这个问题?
繁花不似锦
天涯尽头无女友
守着一只汪
相关分类