Java 如何强制使用 jar(或运行 jar 的 jvm)utf-8 而不是系统的默认编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4159551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force a jar to use(or the jvm that jar runs in) utf-8 instead of the system's default encoding
提问by Aloong
My Windows's default encoding is GBK, and my Eclipse is totally utf-8 encoded.
So an application which runs well in my Eclipse, crashes because the words become unreadable when exported as a jar file;
I have to write the following line in a .bat file to run the application
我的 Windows 的默认编码是 GBK,而我的 Eclipse 完全是 utf-8 编码。
所以一个在我的 Eclipse 中运行良好的应用程序崩溃了,因为当导出为 jar 文件时,这些单词变得不可读;
我必须在 .bat 文件中编写以下行才能运行应用程序
start java -Dfile.encoding=utf-8 -jar xxx.jar
Now my question is that can I write something in the source code to set the application uses(or the jvm runs in) utf-8 instead of the system's default encoding.
现在我的问题是我可以在源代码中写一些东西来设置应用程序使用(或 jvm 运行)utf-8 而不是系统的默认编码。
采纳答案by sleske
When you open a file for reading, you need to explicitly specify the encoding you want to use for reading the file:
当您打开文件进行读取时,您需要明确指定要用于读取文件的编码:
Reader r = new InputStreamReader(new FileInputStream("myfile"), StandardCharsets.UTF_8);
Then the value of the default platform encoding (which you can change using -Dfile.encoding
) no longer matters.
然后默认平台编码的值(您可以使用 更改-Dfile.encoding
)不再重要。
Note:
笔记:
I would normally recommend to alwaysspecify the encoding explicitly for any operation that depends on the standard locale, such as character I/O. Many Java API methods default to the platform encoding, which I consider a bad design, because often the platform encoding is not the right one, plus it may suddenly change (if the user e.g. switches OS locale), breaking your app.
我通常建议始终为依赖于标准语言环境的任何操作(例如字符 I/O)明确指定编码。许多 Java API 方法默认使用平台编码,我认为这是一种糟糕的设计,因为平台编码通常不是正确的,而且它可能会突然改变(如果用户切换操作系统区域设置),从而破坏您的应用程序。
So just always say which encoding you want.
所以总是说你想要哪种编码。
There are some cases where the platform encoding is the right one (such as when opening a file the user just created for you), but they are fairly rare.
在某些情况下,平台编码是正确的(例如打开用户刚刚为您创建的文件时),但这种情况很少见。
Note 2:
笔记2:
java.nio.charset.StandardCharsets
was introduced in Java 1.7. For older Java versions, you need to specify the input encoding as a String (ugh). The list of possible encodings depends on the JVM, but every JVM is guaranteed to at least have:
java.nio.charset.StandardCharsets
是在 Java 1.7 中引入的。对于较旧的 Java 版本,您需要将输入编码指定为字符串(呃)。可能的编码列表取决于 JVM,但保证每个 JVM 至少具有:
US-ASCII, ISO-8859-1,UTF-8,UTF-16BE,UTF-16LE,UTF-16.
US-ASCII、ISO-8859-1、UTF-8、UTF-16BE、UTF-16LE、UTF-16。
回答by Andrei
There's another way. If you are sure how you like to encode the input and output, you can save the settings before you compile your jar file.
还有另一种方式。如果您确定您喜欢如何编码输入和输出,您可以在编译 jar 文件之前保存设置。
Here is a example for NetBeans.
这是 NetBeans 的示例。
Go To Project >> Properties >> Run >> VM Options and type -Dfile. encoding=UTF-8
转到项目>>属性>>运行>>VM选项并键入 -Dfile. encoding=UTF-8
After that, everything is encoded in UTF-8
every time the Java VM is started.
之后,UTF-8
每次启动 Java VM 时都会对所有内容进行编码。
(I think Eclipse offers the same possibility. If not, just google to VM Options.)
(我认为 Eclipse 提供了同样的可能性。如果没有,只需谷歌到 VM 选项。)