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

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

问题描述

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");    
     }
}

我正在使用此代码尝试从资产中读取文件.我尝试了两种方法来做到这一点.首先,当使用 File 时,我收到 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 在评论中所说,我给出的代码不是总结行.mLine 每次通过都会被替换.这就是我写//process line的原因.我假设该文件包含某种数据(即联系人列表),每一行都应单独处理.

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.

如果您只想加载文件而不进行任何类型的处理,您必须使用 StringBuilder() 在每次传递时总结 mLine 并附加每个传递.

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 的评论,我添加了 finally 块.

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

另请注意,在 Java 7 及更高版本中,您可以使用 try-with-resources 来使用最新 Java 的 AutoCloseableCloseable 功能.

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

上下文

@LunarWatcher 在评论中指出 getAssets()context 中的 class.因此,如果您在 activity 之外调用它,您需要引用它并将上下文实例传递给活动.

In a comment @LunarWatcher points out that getAssets() is a class in context. So, if you call it outside of an activity you need to refer to it and pass the context instance to the activity.

ContextInstance.getAssets();

@Maneesh 的回答对此进行了解释.因此,如果这对您有用,请为他的回答点赞,因为正是他指出了这一点.

This is explained in the answer of @Maneesh. So if this is useful to you upvote his answer because that's him who pointed that out.

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

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