我正在编写一个 Java 应用程序,它读取逗号分隔的文本文件,对数据执行一些计算并将更新的数据写入新文件。输入文件包含大约 5 亿行,所以我试图尽可能地缩放下面的内容,以便在运行它时不会出现内存不足的异常。关于如何改进以下内容的任何想法?
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CsvTest {
public void readFile() {
BufferedReader br = null;
BufferedWriter out = null;
try {
br = new BufferedReader(new FileReader("C:\\input.txt"));
FileWriter fstream = new FileWriter("C:\\output.txt");
out = new BufferedWriter(fstream);
String line = null;
while ((line = br.readLine()) != null) {
out.write(line + "\r\n");
}
}
catch (FileNotFoundException ex) {
System.err.println("Error: " + ex.getMessage());
}
catch (IOException ex) {
System.err.println("Error: " + ex.getMessage());
}
finally {
try {
if (br != null) {
br.close();
}
if(out != null){
out.close();
}
}
catch (IOException ex) {
System.err.println("Error: " + ex.getMessage());
}
}
}
public static void main(String[] args) {
CsvTest test = new CsvTest();
test.readFile();
}
}