读取资产文件 [英] read file from assets

查看:113
本文介绍了读取资产文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Utils {

    public static List<Message> getMessages() {

    //File file = new File("file:///android_asset/helloworld.txt");

    AssetManager assetManager = getAssets();
    InputStream ims = assetManager.open("helloworld.txt");

     }
}

我用这code试图读取从资产的文件。我尝试两种方法可以做到这一点。首先,当使用文件我收到 FileNotFoundException异常,使用 AssetManager getAssets时()方法无法识别。 有没有什么解决办法吗?

I am using this code trying to read a file from assets. I tried two ways to do this. First, when use File I received FileNotFoundException, when using AssetManager getAssets() method isn't recognized. Is there any solution here?

推荐答案

下面是我做的对缓冲读书活动扩展/修改,以满足您的需求。

Here is what i do in an activity for buffered reading extend/modify to match your needs

BufferedReader reader = null;
try {
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt")));

    // do reading, usually loop until end of file reading  
    String mLine;
    while ((mLine = reader.readLine()) != null) {
       //process line
       ...
    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

编辑:我的回答可能是无用的,如果你的问题是如何活动之外做。如果你的问题仅仅是如何读取从资产的文件,那么答案是以上。

EDIT : My answer is perhaps useless if your question is on how to do it outside of an activity. If your question is simply how to read a file from asset then the answer is above.

更新

要打开一个文件中指定类型只需添加类型的InputStreamReader的呼叫如下。

To open a file specifying the type simply add the type in the InputStreamReader call as follow.

BufferedReader reader = null;
try {
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt"), "UTF-8")); 

    // do reading, usually loop until end of file reading 
    String mLine;
    while ((mLine = reader.readLine()) != null) {
       //process line
       ...
    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

修改

由于@Stan说,在评论中,code,我给不总结线。 MLINE 被更换一次传球。这就是为什么我写了 //生产线。我认为该文件包含了一些类型的数据(如联系人列表),并在每行应分开处理。

As @Stan says in the comment, the code I am giving is not summing up lines. mLine is replaced every pass. That's why I wrote //process line. I assume the file contains some sort of data (i.e a contact list) and each line should be processed separately.

如果你只是要加载的文件,没有任何形式的处理,你将不得不总结 MLINE 在使用每个通的StringBuilder() 及附加每一遍。

In case you simply want to load the file without any kind of processing you will have to sum up mLine at each pass using StringBuilder() and appending each pass.

另一个编辑

据@Vincent的我加了最后块。

According to the comment of @Vincent I added the finally block.

另外请注意,在Java 7中,上,你可以使用尝试与 - 资源使用 AutoCloseable 可关闭最新的Java功能。

Also note that in Java 7 and upper you can use try-with-resources to use the AutoCloseable and Closeable features of recent Java.

这篇关于读取资产文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆