Android:能够在测试应用中阅读日语字符,但相同的代码在主应用中不起作用

时间:2016-02-22 19:52:35

标签: java android utf-8

这对我来说毫无意义,但那时我是个新手。当我在一个精简的测试应用程序中进行调试时,我可以清楚地看到代码正在读取日文文本,但是当我将代码放入我的主应用程序并进行调试时,我看到它正在读取每一行,但是将日语字符拼凑起来。知道可能会影响到什么吗?

String[] fileList = {"file1", "file2", ...
    try {
        for (int i = 0; i < fileList.length; i++) {
            int rawId = this.getResources().getIdentifier(fileList[i], "raw", this.getPackageName());
            FileOutputStream out = openFileOutput(fileList[i], Context.MODE_PRIVATE);
            InputStream read = this.getResources().openRawResource(rawId);
            InputStreamReader inputstreamReader = new InputStreamReader(read, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputstreamReader);
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                line = line + "\n"; 

               // Japanese characters in "line" appear correct here 
               // in test app but not in main app.

                out.write(line.getBytes());
            }
            inputstreamReader.close();
            bufferedReader.close();
            out.close();
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

1 个答案:

答案 0 :(得分:0)

line.getBytes()使用平台默认编码为您提供字符串中的字节,这在测试应用和主应用之间可能有所不同。使用out.write(line.getBytes("UTF-8"))确保您将字节写入UTF-8编码。