更改String(byte [])的默认编码 [英] Changing the default encoding for String(byte[])
问题描述
在我自己的代码中,我使用String(byte [],String )来指定编码,但是我正在使用一个我无法更改的外部库。
String src =with incents:éà ;
byte [] bytes = src.getBytes(UTF-8);
System.out.println(UTF-8 decode:+ new String(bytes,UTF-8));
System.out.println(Default decoding:+ new String(bytes));
输出为:
UTF-8解码:带重音:éà
默认解码:带有重音:éÃ
我有尝试更改系统属性 file.encoding
,但它不起作用。
p>您需要在启动JVM之前更改区域设置;请参阅:
有些地方似乎意味着您可以通过在启动JVM时设置file.encoding变量来执行此操作,例如
java -Dfile.encoding = UTF-8 ...
...但我没有尝试过这个。最安全的方法是在操作系统中设置环境变量。
Is there a way to change the encoding used by the String(byte[]) constructor ?
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 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.
You need to change the locale before launching the JVM; see:
Some places seem to imply you can do this by setting the file.encoding variable when launching the JVM, such as
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.
这篇关于更改String(byte [])的默认编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!