我需要打开一个 12 MB 的文件,但实际上我正在创建一个 12834566 字节的缓冲区,因为文件的大小是 12MB,而且我正在为 Android 移动系统开发这个应用程序。
然后,我想我必须用 for 读取 1024 KB 的块而不是 12 MB 的块,但我不知道该怎么做,我需要一点帮助。
这是我的实际代码:
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 KB 的块而不是 12 MB 的块吗?
THanks!
尝试一次复制 1 KB。
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);
}
Android 是否像 UNIX 一样支持符号链接或手动链接?如果是的话,这会更快/更有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)