从Android上r.raw阅读文本文件 [英] reading a textfile from r.raw on android

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

问题描述

我有一个JSON文件R.raw.test123,

I've got a json file in R.raw.test123,

我需要处理与GSON。

I need to process that with GSON.

步骤之一;阅读文字转换成字符串,我想这样做,使用;

Step one is; read the text into a string, I want to do that using;

BufferedReader r = new BufferedReader(new FileReader(file));

本的FileReader需要一个字符串作为文件名,但我该怎么办转R.raw.test123进我可以通到的FileReader的字符串。

The FileReader expects a string as filename, but how do I turn R.raw.test123 into a string which I can pass to the FileReader.

我GOOGLE了约4小时,在此,仍然无法找到它。而且我知道它probally一个小白的问题,但我是新来的机器人编程,我来自一个.NET的背景,所以这是所有非常新,我...

I've googled for about 4 hours on this, still can't find it. And I know its probally a noob question, but I'm new to droid programming, I come from a .net background, so this is all very new to me...

谢谢,

丹尼斯

推荐答案

作为回答的 hooked82 您可以使用与获得的InputStream

As answered by hooked82 you can use get the inputstream with:

InputStream stream = getResources().openRawResource(R.raw.test123);

和然后使用方法将其转换成字符串:

and then using method to convert it into string:

private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append((line + "\n"));
        }
    } catch (IOException e) {
        Log.w("LOG", e.getMessage());
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.w("LOG", e.getMessage());
        }
    }
    return sb.toString();
}

所以,你可以得到与字符串 convertStreamToString(流);

这篇关于从Android上r.raw阅读文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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