阅读与UNI code字的原始文件 [英] Read raw file with unicode characters

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

问题描述

我在 / RES /原料(R.raw.test)文件夹包含以下内容的文件:

I have a file in the /res/raw (R.raw.test) folder with the following content:

这是一个Tésêt

我想把它读入一个字符串。我现在的code是:

I want to read it into a string. My current code is:

public static String readRawTextFile(Context ctx, int resId) {
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    InputStreamReader inputreader;
    try {
        inputreader = new InputStreamReader(inputStream, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
        return null;
    }
    BufferedReader buffreader = new BufferedReader(inputreader);
    String line;
    StringBuilder text = new StringBuilder();

    try {
        while ((line = buffreader.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
    } catch (IOException e) {
        return null;
    }
    return text.toString();
}

但返回的字符串是:

But the string returned is:

这是一个TST

我该如何解决呢?谢谢

推荐答案

您code似乎确定。返回的字符串是OK也只是你要查看它在浏览器不支持 UTF-8 。我从 groovyConsole中运行code这是UNI code意识到,它显示 UTF-8 字符串完美。

Your code seems OK. String returned is OK also except that you're trying to view it in viewer not supporting UTF-8. I've run your code from groovyConsole which is UNICODE aware and it displays UTF-8 string perfectly.

这篇关于阅读与UNI code字的原始文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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