Java JVM 的默认编码是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1006276/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:11:06  来源:igfitidea点击:

What is the default encoding of the JVM?

javaencodingcharacter-encodingjvm

提问by user67722

Is UTF-8 the default encoding in Java?
If not, how can I know which encoding is used by default?

UTF-8 是 Java 中的默认编码吗?
如果没有,我怎么知道默认使用哪种编码?

回答by skaffman

It's going to be locale-dependent. Different locale, different default encoding.

这将取决于语言环境。不同的语言环境,不同的默认编码。

回答by Andrzej Doyle

The default character set of the JVM is that of the system it's running on. There's no specific value for this and you shouldn't generally depend on the default encoding being any particular value.

JVM 的默认字符集是运行它的系统的字符集。对此没有特定的值,您通常不应依赖于任何特定值的默认编码。

It can be accessed at runtime via Charset.defaultCharset(), if that's any use to you, though really you should make a point of always specifying encoding explicitlywhen you can do so.

它可以在运行时通过 访问Charset.defaultCharset(),如果这对您有任何用处,但实际上您应该指出始终在可以这样做时明确指定编码

回答by Brian Agnew

Note that you can change the default encoding of the JVM using the confusingly-named property file.encoding.

请注意,您可以使用名称混淆的属性来更改 JVM 的默认编码file.encoding

If your application is particularly sensitive to encodings (perhaps through usage of APIs implying default encodings), then you should explicitly set this on JVM startup to a consistent (known) value.

如果您的应用程序对编码特别敏感(可能是通过使用隐含默认编码的 API),那么您应该在 JVM 启动时将其显式设置为一致(已知)值。

回答by mrclrchtr

There are three "default" encodings:

共有三种“默认”编码:

  • file.encoding:
    System.getProperty("file.encoding")

  • java.nio.Charset:
    Charset.defaultCharset()

  • And the encoding of the InputStreamReader:
    InputStreamReader.getEncoding()

  • 文件编码:
    System.getProperty("file.encoding")

  • java.nio.Charset:
    Charset.defaultCharset()

  • 以及 InputStreamReader 的编码:
    InputStreamReader.getEncoding()

You can read more about it on this page.

您可以在此页面上阅读更多相关信息。

回答by mike32b

I am sure that this is JVM implemenation specific, but I was able to "influence" my JVM's default file.encoding by executing:

我确信这是特定于 JVM 的实现,但我能够通过执行来“影响”我的 JVM 的默认 file.encoding:

export LC_ALL=en_US.UTF-8

(running java version 1.7.0_80 on Ubuntu 12.04)

(在 Ubuntu 12.04 上运行 Java 版本 1.7.0_80)

Also, if you type "locale" from your unix console, you should see more info there.

此外,如果您从 unix 控制台输入“locale”,您应该会在那里看到更多信息。

All the credit goes to http://www.philvarner.com/2009/10/24/unicode-in-java-default-charset-part-4/

所有功劳都归于http://www.philvarner.com/2009/10/24/unicode-in-java-default-charset-part-4/

回答by sdc

You can use this to print out the JVM defaults

您可以使用它来打印出 JVM 默认值

import java.nio.charset.Charset;
import java.io.InputStreamReader;
import java.io.FileInputStream;

public class PrintCharSets {
        public static void main(String[] args) throws Exception {
                System.out.println("file.encoding=" + System.getProperty("file.encoding"));
                System.out.println("Charset.defaultCharset=" + Charset.defaultCharset());
                System.out.println("InputStreamReader.getEncoding=" + new InputStreamReader(new FileInputStream("./PrintCharSets.java")).getEncoding());
        }
}

Compile and Run

编译运行

javac PrintCharSets.java && java PrintCharSets

回答by Juwit

To get default java settings just use :

要获得默认的 Java 设置,只需使用:

java -XshowSettings