Here's the problem: I got some Chinese in my code, and I'm writing it into sqlite.
When I run my program in eclipse and read the string out of sqlite, it just works fine. But when I packaged the project to a jar and run it in command line. The Chinese string which is read out of sqlite is unreadable.
After some trials, I know that the problem lies in the file.encoding system property. When I run the jar using command: java -Dfile.encoding=UTF-8 -jar TK.jar, it works fine with Chinese word, But if I set the system property in code like: System.setProperty("file.encoding", "UTF-8");, it won't work.
So, What is the difference between setting the system property from command line and code? And could anyone tell me how to set file.encoding system property in code?
Thanks a lot!
To summary:
Remember to add charset when using String.getBytes() as well as new String() in order to avoid unreadable output from Chinese or Japanese when running program in different environment.
Somewhere in your code, you are probably relying on the default character set being UTF-8. For example, when you call String.getBytes() without specifying a character set, Java will use the default character set. If you always want UTF-8, then specify this when calling String.getBytes():
byte[] utf8bytes = text.getBytes("UTF-8");
Also, if you want to read a file with a specific character encoding, then specify the character encoding rather than relying on the default setting. For example:
InputStream is = new FileInputStream("utf8file.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// Read text, file will be read as UTF-8
String line = in.readLine();
in.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With