android randomaccessfile 优化,RandomAccessFile in Android raw resource file

It's simply not possible to seek forward and back in an input stream without buffering everything in between into memory. That can be extremely costly, and isn't a scalable solution for reading a (binary) file of some arbitrary size.

You're right: ideally, one would use a RandomAccessFile, but reading from the resources provides an input stream instead. The suggestion mentioned in the comments above is to use the input stream to write the file to the SD card, and randomly access the file from there. You could consider writing the file to a temporary directory, reading it, and deleting it after use:

String file = "your_binary_file.bin";

AssetFileDescriptor afd = null;

FileInputStream fis = null;

File tmpFile = null;

RandomAccessFile raf = null;

try {

afd = context.getAssets().openFd(file);

long len = afd.getLength();

fis = afd.createInputStream();

// We'll create a file in the application's cache directory

File dir = context.getCacheDir();

dir.mkdirs();

tmpFile = new File(dir, file);

if (tmpFile.exists()) {

// Delete the temporary file if it already exists

tmpFile.delete();

}

FileOutputStream fos = null;

try {

// Write the asset file to the temporary location

fos = new FileOutputStream(tmpFile);

byte[] buffer = new byte[1024];

int bufferLen;

while ((bufferLen = fis.read(buffer)) != -1) {

fos.write(buffer, 0, bufferLen);

}

} finally {

if (fos != null) {

try {

fos.close();

} catch (IOException e) {}

}

}

// Read the newly created file

raf = new RandomAccessFile(tmpFile, "r");

// Read your file here

} catch (IOException e) {

Log.e(TAG, "Failed reading asset", e);

} finally {

if (raf != null) {

try {

raf.close();

} catch (IOException e) {}

}

if (fis != null) {

try {

fis.close();

} catch (IOException e) {}

}

if (afd != null) {

try {

afd.close();

} catch (IOException e) {}

}

// Clean up

if (tmpFile != null) {

tmpFile.delete();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值