Java堆空间程序性能

时间:2017-09-17 16:20:27

标签: java

我编写了一个Java程序,逐行读取txt文件,在一行中找到某个值,编辑它并将所有行写入新文件。 e.g:

Input:
4563,9876,abc545

Output:
4563,9876_1,abc545

我从命令提示符运行程序,我能够处理1百万条记录。但是,如果我再多一点,我会得到以下错误:

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

我试图解决它,但没有成功。 下面是我的Java类,我可以获得一些关于如何改进我的代码来处理更多记录的建议吗?

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

 class RecordTreatment {
    public static void main(String args[]) throws IOException
    {
                // Open the file
                File file = new File("C:\\Users\\tolen\\Desktop\\test.txt"); 
                FileInputStream fstream = new FileInputStream(file);
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                StringBuilder fileContent = new StringBuilder();
                String strLine;
                int counter=1;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null)   {
                    String tokens[] = strLine.split(",");
                    if (tokens.length > 0) {
                            String tokens1[] = tokens[16].split("\"");
                            tokens[16] ="\""+tokens1[1] + "_"+counter+++"\"";
                            for (int i = 0; i < tokens.length; i++) {
                                if ( tokens[i].equals(tokens[tokens.length-1])) {
                                    fileContent.append(tokens[i]);
                                }else{
                                    fileContent.append(tokens[i]+",");
                                  }
                                }
                            fileContent.append("\n");
                    }
                }
                FileWriter fstreamWrite = new FileWriter("C:\\Users\\tolen\\Desktop\\test1.txt");
                BufferedWriter out = new BufferedWriter(fstreamWrite);
                out.write(fileContent.toString());
                out.close();
                //Close the input stream
                br.close();
    }
}

3 个答案:

答案 0 :(得分:1)

class RecordTreatment {
    public static void main(String args[]) throws IOException
    {
        // Open the file
        File file = new File("C:\\Users\\tolen\\Desktop\\test.txt"); 
        FileInputStream fstream = new FileInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        FileWriter fstreamWrite = new FileWriter("C:\\Users\\tolen\\Desktop\\test1.txt");
        BufferedWriter out = new BufferedWriter(fstreamWrite);

        String strLine;
        int counter=1;
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
            String tokens[] = strLine.split(",");
            if (tokens.length > 0) {
                String sub_tokens[] = tokens[16].split("\"");
                tokens[16] = String.format("\"{}_{}",sub_tokens[1],counter);
                out.write(String.join(",",tokens));
                out.write("\n");
            }
        }

        out.close();
        br.close();
    }
}

答案 1 :(得分:0)

由于您正在将整个文件一次性读入StringBuilder,因此内存不足。相反,您是否可以尝试附加到输出文件,因为您正在读取while循环中输入文件的行?

答案 2 :(得分:0)

StringBuilder循环内创建while并在每个循环中将其写入文件,并在BufferedWriter out循环后关闭与while的连接。

这种情况正在发生,因为您拥有的StringBuilder对象在循环时将所有数据存储在文件中。 Here你回答了类似的问题。看看它。