I have written a Java program, to read from a txt file line by line, find a certain value in a line, edit it and write all lines to a new file. e.g:
Input:
4563,9876,abc545
Output:
4563,9876_1,abc545
I am running the program from my command prompt, and I am able to treat 1 Million records. But if I treat a bit more, I get the below error:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
I tried to sort it out, but without success. Below is my Java class, can I get some suggestions on how to ameliorate my code to treat more records?
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();
}
}