2011-04-24 195 views
4

我遇到了问题 - 我需要从Android APK(例如key.keystore)读取SecretKey,因为我的应用程序必须将其读取为FileInputStream,但是从资产我只能获取inputStream。如何将inputStream转换为FileInputStream?或者还有其他的方式,如何从例如文件访问文件。资源作为fileInputStream?将inputStream转换为FileInputStream?

感谢

回答

7

为什么你需要专门的FileInputStream?通常,您不必关心InputStream的底层实现,您只需从中读取即可。也许你的实现应该是InputStream不可知的。

+0

你是对的,16小时连续编程让我生气:-) – Waypoint 2011-04-24 18:39:57

2

我不认为这是可能的,因为一个FileInputStream简直就是文件的InputStream。它可以为你定义的文件获取InputStream,然后你使用普通的InputStream。

Android已经为你做了这份工作。那么为什么你需要一个FileInputStream?

+0

你可能想要一个FileInputStream,因为你想知道文件的长度,除非有一种方法我还没找到。 – fangster 2014-12-22 14:10:05

+1

我没有在FileInputStream上看到任何'getLength()' - 方法,所以不知道你在做什么。另外,如果它是*您*放入APK的资产,则知道它是文件大小。 FileInputStream只是从“物理”文件获取InputStream(读取文件内容)的一种便捷方式。 – 2014-12-22 14:45:15

+0

对不起@ Lukas-Knuth,我迷惑自己!不过,我正在使用未压缩资源的FileInputStream以便完整读入其数据: AssetFileDescriptor afd = context.getResources()。openRawResourceFd(R.raw.myresource); FileInputStream fis = afd.createInputStream(); data = new byte [(int)afd.getLength()]; DataInputStream dis = new DataInputStream(fis); dis.readFully(data); dis.close(); – fangster 2015-01-05 15:22:15

4

我想你指的是AssetManager.open(),它返回一个InputStream。无需将它“转换”为FileInputStream,只需获取对InputStream的引用并使用它(如果需要,可将其包装在BufferedInputStream中)。

相关问题