开发者

special characters display proper in Java IDE, but not in program launched from jar file

开发者 https://www.devze.com 2023-03-10 07:25 出处:网络
I\'m t开发者_Go百科rying to build a Chinese flashcards program in Java to help myself learn Chinese. I\'m using intelliJ IDEA 10. The basic process is that my program will read a file saved on the loc

I'm t开发者_Go百科rying to build a Chinese flashcards program in Java to help myself learn Chinese. I'm using intelliJ IDEA 10. The basic process is that my program will read a file saved on the local machine to generate the flashcards. The file is written using the File class in java. When opened in notepad, it displays all characters properly.

When I run it in the IDE I am able to display Chinese characters as well as pinyin characters(basically vowels with accent marks over them). However, when I built a jar file and launch the program from there, it can no longer display special characters and ends up showing a bunch of weird symbols.

Any ideas on why this is and how to fix it?


IntelliJ doesn't use the platform default encoding, it autodetects it based on the encoding of the source files. When running the code outside IntelliJ, you need to ensure that you explicitly specify the proper encoding when reading/writing the file. You can do that by specifying it as 2nd constructor argument of InputStreamReader and OutputStreamWriter respectively.

File file = new File("/foo.txt");
Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
// ...

Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
// ...

Further you also need to ensure that the viewer supports the fonts as well. Windows Command Console for example, doesn't support Chinese. You'd need to create a Swing application to present the results instead.


Maybe it is an encoding issue?

The File class doesn't have any methods for writing data to files. For that you need a FileOutputStream or some Writer. You should post your code that saves the file. (Just edit the question and add your code there.)

My first guess is that you use a FileWriter. This sounds easy, but it is often wrong, because the FileWriter needs to convert characters into bytes, and it does that with the System's default encoding. You should always specify the encoding yourself, and probably UTF-8 is a good choice.

So instead of the FileWriter you should use the following code:

Charset utf8 = Charset.forName("UTF-8");
OutputStream os = new FileOutputStream(file);
try {
  Writer wr = new OutputStreamWriter(os, utf8);
  wr.write("whatever strings you want to write");
  wr.close();
} finally {
  os.close();
}
0

精彩评论

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