Byte []和java.lang.OutOfMemoryError按位读取文件 [英] Byte[] and java.lang.OutOfMemoryError reading file by bits

查看:172
本文介绍了Byte []和java.lang.OutOfMemoryError按位读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个阅读器读取文件的位,但我有一个大文件的问题。我尝试读取100 MB的文件,它花了3分钟,但它的工作。

然而,然后我试图与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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆