如何在Android中显示奇怪的字符

时间:2013-09-22 14:06:20

标签: java android utf-8 ascii

有人可以告诉我为什么奇怪的角色不会出现在Android listView中吗?

我有一个带有此

的文本文件

enter image description here

我得到了这个

enter image description here

这是我的代码

    ArrayList<String> list = new ArrayList<String>();
    InputStream inStream = getResources().openRawResource(R.raw.emoticons);
    InputStreamReader inputReader = null;


    if (inStream != null)
    {

        try {
            inputReader = new InputStreamReader(inStream, "UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }

        int c, i=0;
        //int i=0;
        char [] cb = new char[1];
        byte [] buf = new byte[100];
        String line = "";

        int ble = -1;

        try {
            ble = inputReader.read(cb, 0, 1);
        }catch (IOException e){
            e.printStackTrace();
        }

        while (ble > -1)
        {

            if(cb[0] == '\r' || cb[0] == '\n')
            {
                try{
                    line = new String(buf, 0, i, "UTF-8");
                }catch (UnsupportedEncodingException e){
                    e.printStackTrace();
                }

                i=0;
                list.add(line);
            }

            else
            {

                buf[i++] = (byte) cb[0];
            }

            try {
                ble = inputReader.read(cb, 0, 1);
            }catch (IOException e){
                e.printStackTrace();
            }

        }
    }



    String[] emoticons = list.toArray(new String[list.size()]);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, emoticons);
    listview.setAdapter(adapter);

1 个答案:

答案 0 :(得分:0)

看起来你正在尝试逐行读取文件。一种方法是使用BufferedReader类:

ArrayList<String> list = new ArrayList<String>();
InputStream inStream = getResources().openRawResource(R.raw.emoticons);
BufferedReader inputReader = null;

if (inStream != null)
{
    try {
        inputReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
        String line;
        while ((line = inputReader.readLine()) != null) {
            list.add(line);
        }
    // handle exceptions..
    } finally {
        if (inputReader != null) inputReader.close();
    }
}

String[] emoticons = list.toArray(new String[list.size()]);