【发布时间】:2015-05-22 11:38:53
【问题描述】:
我正在尝试使用 FileInputStream 从我的资产中的原始文件夹中读取 File。
这就是我创建FileInputStream的方式:
AssetManager assetManager = getAssets();
AssetFileDescriptor fileDescriptor = assetManager.openFd(fileName);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
之后我尝试从File 读取数据,如下所示:
FileChannel fileChannel = inputStream.getChannel();
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
IntBuffer intBuffer = mappedByteBuffer.asIntBuffer();
int[] array = new int[intBuffer.limit()];
intBuffer.get(array);
inputStream.close();
fileChannel.close();
但这不起作用。出于某种原因,fileChannel.size() 返回了一个巨大的数字。我有一个正好 13 个字节长的测试文件,但 fileChannel.size() 返回 1126498!此外,如果我忽略大小并开始读取返回的字节,则根本不匹配我的测试文件!
那么这里发生了什么?有没有办法解决这个问题?
【问题讨论】:
-
你为什么要费尽心思去读 13 个字节?为什么不直接使用 FileInputStream?源代码和字节码比文件中的字节多。
-
@EJP 我需要从应用程序中的文件中读取 80K 整数,这就是我需要 nio 的原因
标签: java android assets fileinputstream