在netbeans中运行时,特殊字符显示正确,但是在运行& quot; jar& quot;时,特殊字符显示正确.文件出现奇怪的字符 [英] Special characters when run in netbeans are showing correctly, but when running "jar" file strange characters appear
问题描述
似乎是一个简单的问题,但是即使在搜索论坛和网络后,我也找不到答案.
当我在netbeans中运行程序时,所有ä,ö,ü之类的特殊字符均正确显示.但是,当我运行同一项目的"jar"文件(我做了清理并重建)时,会出现一些奇怪的字符,例如 @A& $ 等,而不是正确的字符.任何帮助,将不胜感激.
//编辑22. 08.2012 00:46
我以为解决方案会更容易,所以我没有发布任何代码或细节.好的,然后:
Seems like a simple problem, but even after searching forum and web I could not find an answer.
When I run my program in netbeans all the special characters like ä, ö, ü are showing correctly. But when I run "jar" file of the same project (I did clean and rebuild) some strange characters as @A &$ and so on are appearing instead of correct character.
Any help would be appreciated.
//edited 22. 08. 2012 00:46
I thought the solution would be easier so I didn't post any code or details. Ok then:
//input file is in UTF-8
try {
BufferedReader in = new BufferedReader(new FileReader("fin.dir"));
String line;
while ((line = in.readLine()) != null) {
processLine(line, 0);
}
in.close();
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
我以这种方式显示字符:
I am displaying characters in this way:
JOptionPane.showMessageDialog(rootPane, "Correct!\n\n"
+ testingFin.getWord(), "Congrats", 1);`
推荐答案
来自 FileReader
的描述:
用于读取字符文件的便捷类.的构造函数此类假定默认的字符编码和默认字节缓冲区的大小是适当的.要自己指定这些值,在FileInputStream上构造一个InputStreamReader.
Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.
如果您使用的是Windows,则默认编码为ISO-8859-1,因此正如乔恩(Jon)所言,输入时会发生编码问题.试试这个:
If you're on Windows, the default encoding is ISO-8859-1, so as Jon commented, the encoding problem is occurring on input. Try this:
in = new BufferedReader(
new InputStreamReader(new FileInputStream("fin.dir"),"UTF-8"));
这篇关于在netbeans中运行时,特殊字符显示正确,但是在运行& quot; jar& quot;时,特殊字符显示正确.文件出现奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!