我正在获取java.lang.OutOfMemoryError:Java堆空间。我不确定自己在做什么错。这是我的代码:

StringBuffer finalString = new StringBuffer();

    try {
        BufferedReader br = new BufferedReader(new FileReader("collegeData 2.txt"));
        StringBuffer returnFile = new StringBuffer();
        returnFile.append(br.readLine() + br.readLine());
        ArrayList<String> allData = new ArrayList<String>();

        boolean completedFirstSection = false;
        int count = 2;
        String addString = "";

        String nextLine;

        while ((nextLine=br.readLine())!=null) {
            if(count < 990) {
                addString += nextLine;
                count++;
            } else {
                String sub = nextLine.substring(16);
                sub = sub.substring(0, sub.length());

                addString = sub + ":{" + nextLine + "," + addString.substring(0, addString.length()-2) + br.readLine();
                br.readLine();
                allData.add(addString);
                count = 2;
            }
        }
        allData.add("}}]");
        System.out.println("here");
        System.out.println(returnFile.toString());
        finalString = returnFile;
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }


collegeData 2.txt是44.2 mb,它是一个json文件。这是我的错误信息。

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.base/jdk.internal.misc.Unsafe.allocateUninitializedArray(Unsafe.java:1250)
at java.base/java.lang.invoke.StringConcatFactory$MethodHandleInlineCopyStrategy.newArray(StringConcatFactory.java:1605)
at java.base/java.lang.invoke.DirectMethodHandle$Holder.invokeStatic(DirectMethodHandle$Holder)
at java.base/java.lang.invoke.LambdaForm$BMH/754666084.reinvoke(LambdaForm$BMH)
at java.base/java.lang.invoke.LambdaForm$MH/801197928.linkToTargetMethod(LambdaForm$MH)
at RunnerCombined.main(RunnerCombined.java:31)


RunnerCombined.java:31是上面的“ addString + = nextLine”。

预先感谢您的帮助!

最佳答案

谢谢twain249!在将代码更改为此之后,我的程序可以快速运行并且非常完美!

StringBuffer finalString = new StringBuffer();

    try {
        BufferedReader br = new BufferedReader(new FileReader("collegeData 2.txt"));
        StringBuffer returnFile = new StringBuffer();
        returnFile.append(br.readLine() + "\n" + br.readLine() + "\n");

        boolean completedFirstSection = false;
        int count = 2;
        StringBuffer addString = new StringBuffer();

        String nextLine;

        while ((nextLine=br.readLine())!=null) {
            if(count < 990) {
                addString.append(nextLine + "\n");
                count++;
            } else {
                String sub = nextLine.substring(16);
                sub = sub.substring(0, sub.length());

                // addString = sub + ":{" + nextLine + "," + addString.substring(0, addString.length()-2) + br.readLine();
                returnFile.append(sub + ":{" + nextLine + "," + "\n");
                returnFile.append(addString.substring(0, addString.length()-2) + "\n");
                returnFile.append(br.readLine() + "\n");
                addString.setLength(0);
                count = 2;
                br.readLine();
            }
        }
        returnFile.append("}}]");
        finalString = returnFile;
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

08-04 21:17