以下代码读取一堆 .csv 文件,然后将它们组合成一个 .csv 文件。我试图system.out.println
......所有数据点都是正确的,但是当我尝试使用时,PrintWriter
我得到:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
.
我尝试使用FileWriter
但得到了同样的错误。我应该如何更正我的代码?
public class CombineCsv {
public static void main(String[] args) throws IOException {
PrintWriter output = new PrintWriter("C:\\User\\result.csv");
final File file = new File("C:\\Users\\is");
int i = 0;
for (final File child: file.listFiles()) {
BufferedReader CSVFile = new BufferedReader( new FileReader( "C:\\Users\\is\\"+child.getName()));
String dataRow = CSVFile.readLine();
while (dataRow != null) {
String[] dataArray = dataRow.split(",");
for (String item:dataArray) {
System.out.println(item + "\t");
output.append(item+","+child.getName().replaceAll(".csv", "")+",");
i++;
}
dataRow = CSVFile.readLine(); // Read next line of data.
} // Close the file once all data has been read.
CSVFile.close();
}
output.close();
System.out.println(i);
}
}