如何正确地设置JVM (1.5.x)使用的默认字符编码?

我读过-Dfile。encoding=任何以前的jvm使用的方式。我没有那种奢侈,原因我就不多说了。

我试过:

System.setProperty("file.encoding", "UTF-8");

并且属性被设置了,但是它似乎没有导致下面最后的getBytes调用使用UTF8:

System.setProperty("file.encoding", "UTF-8");

byte inbytes[] = new byte[1024];

FileInputStream fis = new FileInputStream("response.txt");
fis.read(inbytes);
FileOutputStream fos = new FileOutputStream("response-2.txt");
String in = new String(inbytes, "UTF8");
fos.write(in.getBytes());

当前回答

按照@Caspar对已接受答案的评论,根据Sun的说法,解决这个问题的首选方法是:

"在启动Java程序之前,更改底层平台的语言环境。"

http://bugs.java.com/view_bug.do?bug_id=4163515

docker参见:

http://jaredmarkell.com/docker-and-locales/

其他回答

在启动应用程序时设置jvm参数帮助我解决了这个问题。java -Dfile。编码= utf - 8 -Dsun.jnu.encoding = utf - 8。

文件。encoding=UTF-8 -这有助于在文件中使用Unicode字符。

sun.jnu。encoding=UTF-8 -这有助于在文件系统中使用Unicode字符作为文件名。

最近,我偶然接触了一家本地公司的Notes 6.5系统,发现在非中文本地Windows安装系统上,web邮件会显示无法识别的字符。我在网上查了几周,几分钟前才发现:

在Java属性中,将以下字符串添加到Runtime Parameters

-Dfile.encoding=MS950 -Duser.language=zh -Duser.country=TW -Dsun.jnu.encoding=MS950

在这种情况下,UTF-8设置将不起作用。

我的团队在装有Windows的机器上遇到了同样的问题。然后用两种方法解决了这个问题:

a)设置环境变量(即使在Windows系统首选项中)

JAVA_TOOL_OPTIONS -Dfile.encoding = UTF8

b)在pom.xml中引入以下片段:

 -Dfile.encoding=UTF-8 

 <jvmArguments>
 -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8001
 -Dfile.encoding=UTF-8
 </jvmArguments>

试试这个:

    new OutputStreamWriter( new FileOutputStream("Your_file_fullpath" ),Charset.forName("UTF8"))

我们把这两个系统属性放在一起,它让系统把所有东西都变成utf8

file.encoding=UTF8
client.encoding.override=UTF-8