介绍读取大文本文件的两种方式,避免堆内存溢出。
1.生成测试文件
package com.example.demo;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;
/**
* @author Walker
* @since 1.0 2018-10-08
*/
public class RandomAccessFileTest {
public static void main(String[] args) throws IOException {
//写文件
RandomAccessFile randomAccessFile = new RandomAccessFile(new File("C:\\result.txt"), "rw");
randomAccessFile.seek(randomAccessFile.length());
long count = 0;
while (true) {
randomAccessFile.write(("测试数据" + System.getProperty("line.separator")).getBytes("utf-8"));
if (count >= Long.MAX_VALUE) {
break;
}
count++;
}
randomAccessFile.close();
//读文件
List<String> list = IOUtils.readLines(new FileInputStream("C:\\result.txt"), "utf-8");
System.out.println(list.size());
}
}
生成测试文件后,调用读取文件的方法将报错。
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3210)
at java.util.Arrays.copyOf(Arrays.java:3181)
at java.util.ArrayList.grow(ArrayList.java:265)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
at java.util.ArrayList.add(ArrayList.java:462)
at org.apache.commons.io.IOUtils.readLines(IOUtils.java:1461)
at org.apache.commons.io.IOUtils.readLines(IOUtils.java:1416)
at org.apache.commons.io.IOUtils.readLines(IOUtils.java:1440)
at com.example.demo.RandomAccessFileTest.main(RandomAccessFileTest.java:50)
2.使用Apache common-io中的FileUtils类读取文件
package com.example.demo;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author Walker
* @since 1.0 2018-10-08
*/
public class RandomAccessFileTest {
public static void main(String[] args) throws IOException {
LineIterator lineIterator = FileUtils.lineIterator(new File("C:\\result.txt"), "utf-8");
while (lineIterator.hasNext()) {
String result = lineIterator.next();
System.out.println(result);
}
}
}
3.使用Scanner读取
package com.example.demo;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
/**
* @author Walker
* @since 1.0 2018-10-08
*/
public class RandomAccessFileTest {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("C:\\result.txt");
Scanner scanner = new Scanner(fileInputStream, "utf-8");
while (scanner.hasNext()) {
String result = scanner.nextLine();
System.out.println(result);
}
fileInputStream.close();
scanner.close();
}
}