我试图获取打印输出并将其存储到字符串数组中,以便将值写入CSV文件中。出现的问题是,当运行代码时,从100行打印,只存储最后一行:
public void organize(String line) throws IOException {
CSVWriter writer = new CSVWriter(new FileWriter("out.csv"), '\t');
String[] token = line.split(",");
String[] DependencyItems = token[1].split(" ");
List<String> entriesList;
String[] entries;
String row = "";
for (int i = 0; i < DependencyItems.length; i++) {
row = token[0] + "," + DependencyItems[i] + "," + token[2] + "," + token[3] + "," + token[5]+ "," + token[6]
+ "," + token[7]+ "," + token[8]+ "," + token[9] + "," + token[10] + "," + token[11] + "," + token[12]
+ "," + token[13] + "," + token[14] + "," + token[15] + "," + token[16]+ "," + token[17]
+ "," + token[18]+ "," + token[19]+ "," + token[20]+ "," + token[21] + "," + token[22]
+ "," + token[23]+ "," + token[24]+ "," + token[25]+ "," + token[26]+ "," + token[27]
+ "," + token[28]+ "," + token[29];
}
System.out.println(row);
entriesList = Arrays.asList(row);
entries = entriesList.toArray(new String[0]);
writer.writeNext(entries);
writer.close();
}
我做错了什么?
答案 0 :(得分:0)
row = <big long string>
您总是重置row
,然后循环后的所有内容只是使用行的最后一个值。如果要使用所有值,可能循环后的代码应该在循环中?至少看起来这两行属于循环。
System.out.println(row);
entriesList = Arrays.asList(row);
请注意,如果row
的范围仅限于循环内部,那么这可能有助于识别此类问题,因为它在循环之外无效。