读取12 MB大文件时出现问题(java.lang.OutOfMemoryError) [英] Problems reading a huge file of 12 MB (java.lang.OutOfMemoryError)

查看:224
本文介绍了读取12 MB大文件时出现问题(java.lang.OutOfMemoryError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打开一个12兆字节的文件,但实际上我正在创建一个12834566字节的缓冲区,因为文件的大小是12MB,我正在为Android移动系统开发这个应用程序。 >

然后,我必须阅读与1024千字节的块,而不是一块12兆字节,与一个for,但我不知道该怎么做,我需要

这是我的实际代码:

  File f = new File(getCacheDir()+/ berlin.mp3); 
if(!f.exists())try {
InputStream is = getAssets()。open(berlin.mp3);
int size = is.available();
byte [] buffer = new byte [size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch(Exception e){throw new RuntimeException(e);请问,有人可以告诉我,在这段代码中,如何读取1024千字节的数据块而不是一块12兆字节?

THANKs!

解决方案

尝试一次复制1 KB。
$ b $ pre $ 文件f =新文件(getCacheDir()+/ berlin.mp3);
if(!f.exists())try {
byte [] buffer = new byte [1024];
InputStream is = getAssets()。open(berlin.mp3);
FileOutputStream fos = new FileOutputStream(f);
int len; ((len = is.read(buffer))> 0)
fos.write(buffer,0,len);
} catch(Exception e){
throw new RuntimeException(e);
} finally {
IOUtils.close(is); //实用程序正确关闭流。
IOUtils.close(fos);
}

Android是否支持像UNIX这样的符号链接或手形链接?如果是这样,这将更快/更有效。


i need to open a file of 12 Megabytes, but actually i'm doing it creating a buffer of 12834566-byte, because the size of the file is 12MB and i am developing this app for Android mobile systems.

Then, i supose i have to read with blocks of 1024 Kbytes instead of one block of 12 Mbytes, with a for, but i don't know how to do it, i need a little help with it.

This is my actual code:

File f = new File(getCacheDir()+"/berlin.mp3");
        if (!f.exists()) try {
          InputStream is = getAssets().open("berlin.mp3");
          int size = is.available();
          byte[] buffer = new byte[size];
          is.read(buffer);
          is.close();
          FileOutputStream fos = new FileOutputStream(f);
          fos.write(buffer);
          fos.close();
        } catch (Exception e) { throw new RuntimeException(e); }

Please, can someone tell me what i have to changue in this code to read blocks of 1024 Kbytes instead of one block of 12 Mbytes?

THanks!

解决方案

Try copying 1 KB at a time.

File f = new File(getCacheDir()+"/berlin.mp3");
if (!f.exists()) try {
     byte[] buffer = new byte[1024];
     InputStream is = getAssets().open("berlin.mp3");
     FileOutputStream fos = new FileOutputStream(f);
     int len;
     while((len = is.read(buffer)) > 0) 
        fos.write(buffer, 0, len);
} catch (Exception e) { 
     throw new RuntimeException(e); 
} finally {
     IOUtils.close(is); // utility to close the stream properly.
     IOUtils.close(fos);
}

Does Android support symbolic or hand links like UNIX? If it does, this would be faster/more efficient.

这篇关于读取12 MB大文件时出现问题(java.lang.OutOfMemoryError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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