Java 更改 String(byte[]) 的默认编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/81323/
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
Changing the default encoding for String(byte[])
提问by Michel
Is there a way to change the encoding used by the String(byte[]) constructor ?
有没有办法改变 String(byte[]) 构造函数使用的编码?
In my own code I use String(byte[],String) to specify the encoding but I am using an external library that I cannot change.
在我自己的代码中,我使用 String(byte[],String) 来指定编码,但我使用的是无法更改的外部库。
String src = "with accents: é à";
byte[] bytes = src.getBytes("UTF-8");
System.out.println("UTF-8 decoded: "+new String(bytes,"UTF-8"));
System.out.println("Default decoded: "+new String(bytes));
The output for this is :
这个的输出是:
UTF-8 decoded: with accents: é à Default decoded: with accents: ?? ??
I have tried changing the system property file.encoding
but it does not work.
我曾尝试更改系统属性,file.encoding
但它不起作用。
采纳答案by Mat Mannion
You need to change the locale before launching the JVM; see:
您需要在启动 JVM 之前更改语言环境;看:
Some places seem to imply you can do this by setting the file.encoding variable when launching the JVM, such as
有些地方似乎暗示你可以通过在启动 JVM 时设置 file.encoding 变量来做到这一点,例如
java -Dfile.encoding=UTF-8 ...
...but I haven't tried this myself. The safest way is to set an environment variable in the operating system.
……不过我自己没试过。最安全的方法是在操作系统中设置环境变量。
回答by jrudolph
Quoted from defaultCharset()
The default charset is determined during virtual-machine startup and typically depends upon the locale and charset of the underlying operating system.
默认字符集是在虚拟机启动期间确定的,通常取决于底层操作系统的区域设置和字符集。
In most OSes you can set the charset using a environment variable.
在大多数操作系统中,您可以使用环境变量设置字符集。
回答by iileandro
I think you want this: System.setProperty("file.encoding", "UTF-8");
我想你想要这个: System.setProperty("file.encoding", "UTF-8");
It solved some problems, but I still have another ones. The chars "í" and "í" doesn't convert correctly if the SO is ISO-8859-1. Just with the JVM option on startup, I get it solved. Now just my Java Console in the NetBeans IDE is crashing charset when showing special chars.
它解决了一些问题,但我还有其他问题。如果 SO 是 ISO-8859-1,字符“í”和“í”不能正确转换。只需在启动时使用 JVM 选项,我就解决了。现在,只有 NetBeans IDE 中的 Java 控制台在显示特殊字符时会导致字符集崩溃。