0

次のコードは、一連の .csv ファイルを読み取り、それらを 1 つの .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);
    } 
}
4

2 に答える 2

0

試す

boolean autoFlush = true; PrintWriter output = new PrintWriter(myFileName, autoFlush);

新しい行またはフォーマットがあるたびにコンテンツをフラッシュする PrintWriter インスタンスを作成します。

于 2016-02-02T00:33:52.563 に答える