开发者

Android: How to read a txt file which contains Chinese characters?

开发者 https://www.devze.com 2023-01-02 09:33 出处:网络
i have a txt file which contains many chinese characters, and the txt file is in the direct开发者_如何学JAVAory res/raw/test.txt. I want to read the file but somehow i can\'t make the chinese characte

i have a txt file which contains many chinese characters, and the txt file is in the direct开发者_如何学JAVAory res/raw/test.txt. I want to read the file but somehow i can't make the chinese characters display correctly. Here is my code:

try {
    InputStream inputstream = getResources().openRawResource(R.raw.test);
    BufferedReader bReader = new BufferedReader(
        new InputStreamReader(inputstream,Charset.forName("UTF-8")));
    String line = null;
    while ((line= bReader.readLine())!= null) {
        Log.i("lolo", line);
        System.out.println("here is some chinese character 这是一些中文字");
    }
} catch (IOException e) {
    e.printStackTrace();
}

Both Log.i("lolo", line); and System.out.println("here is some chinese character 这是一些中文字") don't show characters correctly, i can not even see the chinese characters in the println() method. What can i do to fix this problem? Can anybody help me?


In order to correctly handle non-ASCII characters such as UTF-8 multi-byte characters, it's important to understand how these characters are encoded and displayed.

Your console (output screen) may not support the display of non-ASCII characters. If that's the case, your UTF-8 characters will be displayed as garbage. Sometimes, you will be able to change the character encoding on the console. Sometimes not.

Even if the console correctly displayed UTF-8 characters, it's possible that your string does not correctly store the Chinese characters. You may think that it's correct because your editor displays them, but ensure that the character encoding of your editor also supports UTF-8.


I also was trying to figure out that. First you need to open the .txt file with the notepad and then click on File->Save as, there you will see a dropdown menu that says Enconding, so change it to UTF-8. After saving the file you should remove the .txt extension to the file and then add the file to the path res/raw and then you can refer to it from the code as R.raw.txtFileName.

That's all, i will put my code where I used the chinese characters and I could show them in the emulator.

If you have any other question, let me know because i am also developing something related with characters. Here is the code:

public List<String> getWords() {

    List<String> contents = new ArrayList<String>();
    try {
        InputStream inputStream = getResources().openRawResource(R.raw.chardb);
        BufferedReader input =  new BufferedReader(new InputStreamReader(inputStream,Charset.forName("UTF-8")));
        try {
            String line = null;
            while (( line = input.readLine()) != null){
                contents.add(line);
            }
          }
        finally {
        input.close();
        }
    }
    catch (IOException ex){
      ex.printStackTrace();
    }

    return contents;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消