Byte []和java.lang.OutOfMemoryError按位读取文件 [英] Byte[] and java.lang.OutOfMemoryError reading file by bits
问题描述
然而,然后我试图与500 MB的文件,但它甚至没有开始。因为这一行:
byte [] fileBits = new byte [len];
现在我正在搜索sulutions,找不到任何东西。
也许有人已经解决了它,可以分享一些代码,提示或想法。
$ b $ $ $ $ $ $ $ $ $ $ $ $ if(file.length() > Integer.MAX_VALUE){
throw new IllegalArgumentException(File is too large:+ file.length());
}
int len =(int)file.length();
FileInputStream inputStream = new FileInputStream(file);
尝试{
byte [] fileBits = new byte [len];
for(int pos = 0; pos< len;){
int n = inputStream.read(fileBits,pos,len - pos);
if(n <0){
throw new EOFException();
}
pos + = n;
}
inputStream.read(fileBits,0,inputStream.available());
inputStream.close();
FileChannel fc = new FileInputStream(file).getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY,0,(int)fc.size());
这将使整个文件几乎立即可用(大约10毫秒),并且使用旁边没有堆。 BTW该文件必须小于2 GB。
I am trying to write a reader which reads files by bits but I have a problem with large files. I tried to read file with 100 mb and it took over 3 minutes but it worked.
However, then I tried file with 500 mb but it didn't even started. Because of this line:
byte[] fileBits = new byte[len];
Now I am searching for sulutions and can't find any. Maybe someone had solved it and could share some code, tips or idea.
if (file.length() > Integer.MAX_VALUE) {
throw new IllegalArgumentException("File is too large: " + file.length());
}
int len = (int) file.length();
FileInputStream inputStream = new FileInputStream(file);
try {
byte[] fileBits = new byte[len];
for (int pos = 0; pos < len;) {
int n = inputStream.read(fileBits, pos, len - pos);
if (n < 0) {
throw new EOFException();
}
pos += n;
}
inputStream.read(fileBits, 0, inputStream.available());
inputStream.close();
I suggest you try memory mapping.
FileChannel fc = new FileInputStream(file).getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size());
This will make the whole file available almost immediately (about 10 ms) and uses next to no heap. BTW The file has to be less than 2 GB.
这篇关于Byte []和java.lang.OutOfMemoryError按位读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!