读取Assets目录下的文件

来源:互联网 发布:hc05 编程 编辑:程序博客网 时间:2024/05/18 02:45

在ANDROID中,我们除了可以把资源文件放在res目录下之外,还可以把它们放到assets目录下。

那么,怎样去读取Assets目录下的文件呢?

 

首先,我们需要得到一个AssetManager的实例。

AssetManager assetManager = context.getAssets();


然后,通过这个assetManager,

InputStream inputStream = assetManager.open("dir/subdir/filename.txt");


得到一个InputStream。我们可以通过InputStream来读取文件。

 

最后,不要忘记关闭InputStream。

inputStream.close();


从上面的示例中,我们还可以看出,可以在assets目录下新建自定义的目录结构,只需要在打开时指定正确的路径即可。