gpt4 book ai didi

Java堆空间程序性能

转载 作者:行者123 更新时间:2023-11-30 06:30:27 25 4
gpt4 key购买 nike

我编写了一个Java程序,从txt文件中逐行读取,找到一行中的某个值,对其进行编辑并将所有行写入一个新文件。例如:

Input:
4563,9876,abc545

Output:
4563,9876_1,abc545

我正在从命令提示符运行该程序,并且能够处理 100 万条记录。但如果我处理得更多一些,我会收到以下错误:

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

最佳答案

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

关于Java堆空间程序性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46266241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com