1. java 读取大文件的困难
java 读取文件的一般操作是将文件数据全部读取到内存中,然后再对数据进行操作。例如
Path path = Paths.get("file path");
byte[] data = Files.readAllBytes(path);
这对于小文件是没有问题的,但是对于稍大一些的文件就会抛出异常
Exception in thread "main" java.lang.OutOfMemoryError: Required array size too large
at java.nio.file.Files.readAllBytes(Files.java:3156)
从错误定位看出,Files.readAllBytes
方法最大支持 Integer.MAX_VALUE - 8
大小的文件,也即最大2GB的文件。一旦超过了这个限度,java 原生的方法就不能直接使用了。
2. 分次读取大文件
既然不能直接全部读取大文件到内存中,那么就应该把文件分成多个子区域分多次读取。这就会有多种方法可以使用。
(1) 文件字节流
对文件建立 java.io.BufferedInputStream
,每次调用 read()
方法时会接连取出文件中长度为 arraySize
的数据到 array
中。这种方法可行但是效率不高。
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
* Created by
*/
public class StreamFileReader {
private BufferedInputStream fileIn;
private long fileLength;
private int arraySize;
private byte[] array;
public StreamFileReader(String fileName, int arraySize) throws IOException {
this.fileIn = new BufferedInputStream(new FileInputStream(fileName), arraySize);
this.fileLength = fileIn.available();
this.arraySize = arraySize;
}
public int read() throws IOException {
byte[] tmpArray = new byte[arraySize];
int bytes = fileIn.read(tmpArray);// 暂存到字节数组中
if (bytes != -1) {
array = new byte[bytes];// 字节数组长度为已读取长度
System.arraycopy(tmpArray, 0, array, 0, bytes);// 复制已读取数据
return bytes;
}
return -1;
}
public void close() throws IOException