本文介绍了需要AssetInputStream转换的FileInputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了这是工作在我的电脑上的数据结构,现在我想将它移植到我的Andr​​oid应用程序。我打开原始 .DAT 资源,并获得的InputStream ,但我需要一个的FileInputStream

I have implemented a data structure which is working on my computer and now I am trying to port it into my android application. I open a raw .dat resource and get a InputStream but I need to get a FileInputStream:

FileInputStream fip = (FileInputStream) context.getResources().openRawResource(fileID);
FileChannel fc = fip.getChannel();
long bytesSizeOfFileChannel = fc.size();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0L, bytesSizeOfFileChannel);
...

在code以上抛出以下异常,因为一个InputStream不能转换为一个FileInputStream,但是这正是我需要的:

The code above throws the following exception since an InputStream can not be cast to a FileInputStream but that's just what I need:

java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream cannot be cast to java.io.FileInputStream

我所有的code是建立在使用本 FileChannel 有一个FileInputStream,所以我想继续使用它。有没有办法从具有的InputStream context.getResources()。openRawResource(FILEID),然后转换为去它变成一个 FileChannel

All my code is build on using this FileChannel with a FileInputStream so I want to keep using it. Is there a way to go from having an InputStream from context.getResources().openRawResource(fileID) and then convert it into a FileChannel?

有点相关的帖子中,我无法找到我的情况该机器人工作的解决方案:

Converting的InputStream的FileInputStream?

推荐答案

一个资源不是一个文件。测功它不能被用来作为存储器映射文件。如果您有这么巨大的,他们需要的内存功放推动的资源,他们可能不应该都是资源。如果他们是小,内存映射没有带来好处。

A resource isn't a file. Ergo it can't be used as a memory-mapped file. If you have resources that are so enormous they need to be memory-amped, they probably shouldn't be resources at all. And if they are small, memory mapping brings no advantages.

这篇关于需要AssetInputStream转换的FileInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:50