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 approximately 2.1GB of memory-the reason is simple: 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.nextline (); 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 memory:564 Mb
5. Conclusion
This short article describes how to handle large files without having to read and run 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.
High-efficiency implementation of Java reading large files