I have to read two seperate txt files into memory. I will read each line from one txt file and will look for some contents from one txt file in the the other txt file several times.
I tried to read the txt files into two seperate ArrayLists but when I tried to check the size after loading, it threwjava.lang.OutOfMemoryError: Java heap space exception.
This is my code block:
BufferedReader mtc_txt = new BufferedReader(new FileReader(mtc_path));
BufferedReader mfc_txt = new BufferedReader(new FileReader(mfc_path));
String line;
ArrayList<String> mtc_txt_list = new ArrayList<String>();
ArrayList<String> mfc_txt_list = new ArrayList<String>();
System.out.println("Loading..");
while ((line = mtc_txt.readLine()) != null) {
mtc_txt_list.add(line);
}
while ((line = mfc_txt.readLine()) != null) {
mfc_txt_list.add(line);
}
System.out.println("Loaded..");
System.out.println("MTC num of lines: "+mtc_txt_list.size());
System.out.println("MFC num of lines: "+mfc_txt_list.size());
This is the output:
Loading..
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Unknown Source)
at java.lang.String.<init>(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Main.replaceSelected(Main.java:39)
at Main.main(Main.java:108)
one txt file contains about 90000 lines, and the other contains 524000 lines.
I want to keep them in the memory. What is the best way to do this in Java?
Thanks.