更改String(byte [])的默认编码 [英] Changing the default encoding for String(byte[])

查看:111
本文介绍了更改String(byte [])的默认编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变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之前更改区域设置;请参阅:



Java,错误ID 4163515



有些地方似乎意味着您可以通过在启动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:

Java, bug ID 4163515

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆