1. Overview
This tutorial shows you how to read large files efficiently in Java. This article is part of the "java--Back to Basics" series of tutorials on Baeldung (http://www.baeldung.com/).
2. Read in memory
The standard way to read a file line is to read in memory, and both guava and Apache Commons io provide a quick way to read a file line as follows:
Files.readlines (new File (path), charsets.utf_8); Fileutils.readlines (The new File (path));
The problem with this approach is that all the rows of the file are stored in memory and will soon cause the program to throw a OutOfMemoryError exception when the file is large enough.
For example: read a file that is about 1G:
@Testpublic void Givenusingguava_wheniteratingafile_thenworks () throws IOException {String Path = ... Files.readlines (new File (path), charsets.utf_8);}
This approach starts with a small amount of memory: (approximately 0Mb of memory is consumed)
[Main] Info org.baeldung.java.corejavaiounittest-total memory:128 Mb[main] Info org.baeldung.java.corejavaiounittest-free memory:116 Mb
However, when the files are all read into memory, we can finally see (consumes about 2GB of memory):
[Main] Info org.baeldung.java.corejavaiounittest-total memory:2666 Mb[main] Info org.baeldung.java.corejavaiounittest-free memory:490 Mb
This means that this process consumes about 2.1GB of memory--for simple reasons: all the rows of the file are now stored in memory.
Putting all of the contents of a file in memory quickly runs out of available memory--no matter how large the actual available memory is, this is obvious.
In addition, we don't usually need to put all the lines of a file into memory at once--instead, we just need to iterate through each line of the file, then do the appropriate processing, and throw it out after processing. So that's exactly what we're going to do--by iterating through the lines, rather than putting all the rows in memory.
3. File stream
Now let's take a look at this solution-we'll use the Java.util.Scanner class to scan the contents of a file, one line at a time to read it continuously:
fileinputstream inputstream = null; Scanner sc = null;try { inputstream = new fileinputstream (path); sc = new scanner (inputstream, "UTF-8"); while (Sc.hasNextLine ( )) { string line = sc.nextline (); // System.out.println (line); } // note that scanner suppresses exceptions if (Sc.ioexception () != null) { throw Sc.ioexception (); }} finally { if (inputstream != null) { inputstream.close (); } if (sc != null) { sc.close (); }}
This scenario will traverse all the rows in the file-allowing each row to be processed without maintaining a reference to it. In short, they are not stored in memory: (about 150MB of memory consumption)
[Main] Info org.baeldung.java.corejavaiounittest-total memory:763 Mb[main] Info org.baeldung.java.corejavaiounittest-free memory:605 Mb
4. Apache Commons IO Stream
You can also use the Commons IO Library implementation to take advantage of the custom lineiterator that the library provides:
Lineiterator it = Fileutils.lineiterator (thefile, "UTF-8"); try {while (It.hasnext ()) {String line = It.nextlin E (); Do something with line}} finally {lineiterator.closequietly (it);}
Because the entire file is not all in memory, it also leads to quite conservative memory consumption: (approximately 150MB of memory is consumed)
[Main] Info o.b.java.corejavaiointegrationtest-total memory:752 Mb[main] Info o.b.java.corejavaiointegrationtest-free Memo ry:564 Mb
5. Conclusion
This short article describes how to handle large files without repeated reads and runs out of memory-this provides a useful solution for processing large files.
All of these examples are implemented and snippets can be obtained on my GitHub project-this is an Eclipse-based project, so it should be easy to import and run.
Java reads large files efficiently