Certain Chinese Characters not displayed properly.
We are retrieving Chinese data from a file and displaying it to our web page. However, for some reason, certain chinese characters are destroyed, thus resulting to 3 different characters instead of 1 character.
I found out that:
If we directly copy the bytes from the file into a new file, the chinese chars can be viewed properly in our web browser.
If we save the bytes into a new String, the chinese is displayed properly but 1 or 2 characters are destroyed (displaying 3 different garbage characters each).
Any idea on how to resolve this? Any help will be appreciated.
Thanks and Regards,
Kerwin
Re: Certain Chinese Characters not displayed properly.
Hello,
Make sure your file is stored using UTF-8 then open this file using the following snippet of code.
Code :
public String readUTF8FileAsString(final String filePath) {
try {
final StringBuilder stringBuilder = new StringBuilder();
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
bufferedReader.close();
return stringBuilder.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// Json