Thursday, February 13, 2014

TextView display Chinese garbled


BufferedReader read by a string from a file, but the display of Chinese characters in TextView occurs when a garbled, String encoding has been changed to "GBK"<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Write encoding landlord, pay attention and read the file encoding to be consistent ah! Recommended use utf-8 encoding, write this code, also used to read this.
Reply:
cited a floor ncepu307 reply: write encoding
landlord, pay attention and read the file encoding to be consistent ah! Recommended use utf-8 encoding, write this code, also used to read this.
Hello, I am a novice android, read txt files are read directly from the file

 try {
InputStream in = getResources () getAssets () open ("data.txt");..
BufferedReader buffer = new BufferedReader (new InputStreamReader (in, "utf-8"));

String pureWord = new String ();
String explain = new String ();

while ((pureWord = buffer.readLine ())! = null) {
explain = buffer.readLine ();
words.add (new Word (pureWord, explain));
}
in.close ();
} Catch (IOException e) {
e.printStackTrace ();
}




This is wrong, why ah?
Reply:
reference to the second floor letingoo reply:
Quote: references to a floor ncepu307 reply:

Write encoding landlord, pay attention and read the file encoding to be consistent ah! Recommended use utf-8 encoding, write this code, also used to read this.
Hello, I am a novice android, read txt files are read directly from the file

 try {
InputStream in = getResources () getAssets () open ("data.txt");..
BufferedReader buffer = new BufferedReader (new InputStreamReader (in, "utf-8"));

String pureWord = new String ();
String explain = new String ();

while ((pureWord = buffer.readLine ())! = null) {
explain = buffer.readLine ();
words.add (new Word (pureWord, explain));
}
in.close ();
} Catch (IOException e) {
e.printStackTrace ();
}




This is wrong, why ah?


Are you sure data.txt is utf-8 encoding it? With notepad + + creates a Chinese in utf-8 encoded file, and then try to open?

Reply:
reference to the second floor letingoo reply:
Quote: references to a floor ncepu307 reply:

Write encoding landlord, pay attention and read the file encoding to be consistent ah! Recommended use utf-8 encoding, write this code, also used to read this.
Hello, I am a novice android, read txt files are read directly from the file

 try {
InputStream in = getResources () getAssets () open ("data.txt");..
BufferedReader buffer = new BufferedReader (new InputStreamReader (in, "utf-8"));

String pureWord = new String ();
String explain = new String ();

while ((pureWord = buffer.readLine ())! = null) {
explain = buffer.readLine ();
words.add (new Word (pureWord, explain));
}
in.close ();
} Catch (IOException e) {
e.printStackTrace ();
}




This is wrong, why ah?


You want to make sure your data.txt file is encoded with utf-8 written? You pull out of this file, open with notepad + +, utf-8 encoding to see is not garbled.
Or say you upstairs to create a new utf-8 encoded files, just write about the contents into it, and then read out the code to see.
Reply:
Encoding format should not change, check the source

No comments:

Post a Comment