views:

385

answers:

1

Hallo, i have a txt file which contains many chinese characters, and the txt file is in the directory 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?

+1  A: 

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.

Jin Kim
thank you, I could solve this problem with your answer
TianDong