I want to print each line from a huge text file (over 600,000 MB).
But when I try to execute the code below, I get "... OutOfMemoryError: Java heap space" right before I reach the line number of 1,000,000.
Is there a better way to handle input than FileReader and LineNumberReader?
FileReader fReader = new FileReader(new File("C:/huge_file.txt")); LineNumberReader lnReader = new LineNumberReader(fReader); String line = ""; while ((line = lnReader.readLine()) != null) { System.out.println(lnReader.getLineNumber() + ": " + line); } fReader.close(); lnReader.close();
Thanks in advance!
Thank you all for your answers!
I finally found a memory leak, an unused instance of the Java class that duplicated it for each iteration of the lines. In other words, it had nothing to do with the file upload part.
source share