在 Java 中确定平台的默认字符集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2677419/
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
Determining default character set of platform in Java
提问by Anand Sunderraman
I am programming in Java
我正在用 Java 编程
I have the code as:
我的代码如下:
byte[] b = test.getBytes();
In the api it is specified that if we do not specify character encoding it takes the default platform character encoding.
在 api 中指定,如果我们不指定字符编码,它将采用默认的平台字符编码。
What is meant by "default platform character encoding" ?
“默认平台字符编码”是什么意思?
Does it mean the Java encoding or the OS encoding ?
它是指 Java 编码还是 OS 编码?
If it means OS encoding the how can i check the default character encoding of Windows and Linux ? Is there anyway we can get the default character encoding using command line ?
如果这意味着操作系统编码,我如何检查 Windows 和 Linux 的默认字符编码?无论如何我们可以使用命令行获得默认字符编码吗?
采纳答案by Jon
It means the default character encoding of the JVM that you're running on,
这意味着您正在运行的 JVM 的默认字符编码,
To check the default encoding you can do the following:
要检查默认编码,您可以执行以下操作:
System.getProperty("file.encoding");
that will return the default encoding (and the one used by getBytes() above).
这将返回默认编码(以及上面 getBytes() 使用的编码)。
回答by BalusC
The system property file.encodingis JVM vendor specific. In this specific case it's only applicable on the Sun JVM and it may not work on JVM's from other vendors than Sun.
系统属性file.encoding是特定于 JVM 供应商的。在这种特定情况下,它仅适用于 Sun JVM,可能不适用于 Sun 以外的其他供应商的 JVM。
Rather use Java SE API provided Charset#defaultCharset().
而是使用提供的 Java SE API Charset#defaultCharset()。
Charset defaultCharset = Charset.defaultCharset();

