阅读资产文件作为字符串 [英] Read Assets file as string

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

问题描述

我想读一个资产文件作为字符串,我试着像20个答案在这里,但他们不为我工作。

I'm trying to read a assets file as string, I tried like 20 answers here but they don't work for me.

我在我的资产文件夹中的文件:data.opml,我必须把内容串。我把它像:

I have a file in my assets folder: data.opml, and I have to put the content in a string. I send it like:

 OPML.importFromFile(string, MainTabActivity.this);

和接受它喜欢的:

 importFromFile(String filename, Context context); 

东西没有工作(但它不是一个资产文件):

Something that did work (but it's not a assets file):

 OPML.importFromFile(new StringBuilder(Environment.getExternalStorageDirectory().toString()).append(File.separator).append(fileNames[which]).toString(),MainTabActivity.this);

我已经试过:

I've tried:

 AssetFileDescriptor descriptor = getAssets().openFd("data.opml");
 FileReader reader = new FileReader(descriptor.getFileDescriptor());
 And also:
 InputStream input = getAssets().open("data.opml");
 Reader reader = new InputStreamReader(input, "UTF-8");

莫比我做得不对,但它是行不通的,因为它的项目为错误(分别为:OPML不能够对参数的FileReader和读写器),如果有人知道如何做到这一点,那将是非常AP preciated!

Maby I'm doing something wrong, but it just won't work because it the project gives errors (respectively: OPML is not capable for the arguments filereader and reader) , If somebody knows how to do this, it would be very appreciated!

推荐答案

getAssets()。打开()将返回的InputStream 。阅读从使用标准的Java I / O:

getAssets().open() will return an InputStream. Read from that using standard Java I/O:

    StringBuilder buf=new StringBuilder();
    InputStream json=getAssets().open("book/contents.json");
    BufferedReader in=
        new BufferedReader(new InputStreamReader(json, "UTF-8"));
    String str;

    while ((str=in.readLine()) != null) {
      buf.append(str);
    }

    in.close();

和,在未来,请解释完全和precisely 你所说的不工作,因为这解释是不是非常有帮助。意思

And, in the future, please explain completely and precisely what you mean by "don't work", as that explanation is not very helpful.

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

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