简体   繁体   English

更改String的默认编码(byte [])

[英]Changing the default encoding for String(byte[])

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但它不起作用。

You need to change the locale before launching the JVM; 您需要在启动JVM之前更改语言环境; see: 看到:

Java, bug ID 4163515 Java,错误ID 4163515

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. 最安全的方法是在操作系统中设置环境变量。

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. 如果SO是ISO-8859-1,则字符“í”和“Í”不能正确转换。 Just with the JVM option on startup, I get it solved. 只需在启动时使用JVM选项,我就可以解决它。 Now just my Java Console in the NetBeans IDE is crashing charset when showing special chars. 现在,只有我在NetBeans IDE中的Java控制台在显示特殊字符时崩溃了charset。

Quoted from defaultCharset() 引自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. 在大多数操作系统中,您可以使用环境变量设置字符集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM