这篇文章将描述怎样在JAVA中高效的读取一个大文件html
咱们通常经常使用的方法是在内存中读取文件。—包括 Guava 和 Apache Commons IO 都提供了一个简便的方法来操做:java
Files.readLines(new File(path), Charsets.UTF_8); FileUtils.readLines(new File(path));
这是会发生问题的,咱们将全部的文件都保存在了内存中。要是文件过大,就会出现 OutOfMemoryError ;ui
例如,读取1个1GB的文件code
@Test public void givenUsingGuava_whenIteratingAFile_thenWorks() throws IOException { String path = ... Files.readLines(new File(path), Charsets.UTF_8); }
内存会被耗尽htm
[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Total Memory: 128 Mb
[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Free Memory: 116 Mbip
在咱们读取完文件操做后。咱们能够看到内存:内存
[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Total Memory: 2666 Mb
[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Free Memory: 490 Mb开发
这意味着。有2.1GB的内存被使用,缘由很简单,就是把全部文件都装入了内存input
但一般。咱们并不须要一次性加载内存中的全部文件。咱们仅仅须要遍历文件。作一些操做。而后丢弃。因此,咱们如今要作的就是不把文件彻底加入到内存中依次遍历文件。it
咱们来看看这个方法。咱们将经过 java.util.Scanner 来依次遍历。
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(); } }
这个方法,依次遍历文件,对每行文件均可以处理。而且不须要所有加载在内存中
[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Total Memory: 763 Mb
[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Free Memory: 605 Mb
上面的那个操做也能够经过 Commons IO 来实现每一行的遍历
LineIterator it = FileUtils.lineIterator(theFile, "UTF-8"); try { while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { LineIterator.closeQuietly(it); }
一样,也是不在内存中加载所有文件。少许的内存消耗:
[main] INFO o.b.java.CoreJavaIoIntegrationTest - Total Memory: 752 Mb
[main] INFO o.b.java.CoreJavaIoIntegrationTest - Free Memory: 564 Mb
好了,文章到此结束,在咱们平常的开发中,在遇到读取大文件就能够参照以上的方法了。