简体   繁体   English

如何使用PrintWriter克服内存不足异常?

[英]How to overcome out of memory exception with PrintWriter?

The following code reads a bunch of .csv files and then combines them into one .csv file. 下面的代码读取一堆.csv文件,然后将它们组合成一个.csv文件。 I tried to system.out.println ... all datapoints are correct, however when i try to use the PrintWriter I get: 我尝试了system.out.println ...所有数据点都是正确的,但是当我尝试使用PrintWriter我得到了:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space . Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I tried to use FileWriter but got the same error. 我尝试使用FileWriter但遇到了相同的错误。 How should I correct my code? 我应该如何更正我的代码?

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);
    } 
}

I can only think of two scenarios in which that code could result in an OOME: 我只能想到两种可能导致该代码导致OOME的情况:

  • If the file directory has a very large number of elements, then file.listFiles() could create a very large array of File objects. 如果file目录中包含大量元素,则file.listFiles()可以创建大量File对象数组。

  • If one of the input files includes a line that is very long, then CSVFile.readLine() could use a lot of memory in the process of reading it. 如果输入文件之一包含很长的一行,则CSVFile.readLine()在读取过程中可能会占用大量内存。 (Up to 6 times the number of bytes in the line.) (最多为该行中字节数的6倍。)

The simplest approach to solving both of these issues is to increase the Java heap size using the -Xmx JVM option. 解决这两个问题的最简单方法是使用-Xmx JVM选项增加Java堆大小。


I can see no reason why your use of a PrintWriter would be the cause of the problem. 我看不出有任何原因导致您使用PrintWriter

Try 尝试

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

It creates a PrintWriter instance which flushes content everytime when there is a new line or format. 它创建一个PrintWriter实例,每次有新行或新格式时都会刷新内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM