设置默认的Java字符编码

本文探讨了如何在Java 1.5.x中以编程方式设置默认字符编码。讨论了各种方法,包括设置系统属性、解决locale问题以及避免依赖JVM默认编码。建议在代码中显式指定如UTF-8的编码,以确保跨系统和配置的兼容性。
摘要由CSDN通过智能技术生成

本文翻译自:Setting the default Java character encoding

How do I properly set the default character encoding used by the JVM (1.5.x) programmatically? 如何以编程方式正确设置JVM(1.5.x)使用​​的默认字符编码?

I have read that -Dfile.encoding=whatever used to be the way to go for older JVMs... I don't have that luxury for reasons I wont get into. 我已经读过-Dfile.encoding=whatever以前用于旧JVM的方式...我没有那么奢侈,因为我不会进入。

I have tried: 我试过了:

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

And the property gets set, but it doesn't seem to cause the final getBytes call below to use UTF8: 并且属性已设置,但它似乎不会导致下面的最终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());

#1楼

参考:https://stackoom.com/question/1WAJ/设置默认的Java字符编码


#2楼

I have a hacky way that definitely works!! 我有一种绝对有效的hacky方式!

System.setProperty("file.encoding","UTF-8");
Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);

This way you are going to trick JVM which would think that charset is not set and make it to set it again to UTF-8, on runtime! 这样你就会欺骗JVM,它会认为charset没有设置,并让它在运行时再次设置为UTF-8!


#3楼

We were having the same issues. 我们遇到了同样的问题。 We methodically tried several suggestions from this article (and others) to no avail. 我们有条不紊地尝试了本文(和其他人)的一些建议但无济于事。 We also tried adding the -Dfile.encoding=UTF8 and nothing seemed to be working. 我们还尝试添加-Dfile.encoding=UTF8 ,似乎没有任何工作。

For people that are having this issue, the following article finally helped us track down describes how the locale setting can break unicode/UTF-8 in Java/Tomcat 对于遇到此问题的人,以下文章最终帮助我们跟踪描述了区域设置如何在Java/Tomcat打破unicode/UTF-8

http://www.jvmhost.com/articles/locale-breaks-unicode-utf-8-java-tomcat http://www.jvmhost.com/articles/locale-breaks-unicode-utf-8-java-tomcat

Setting the locale correctly in the ~/.bashrc file worked for us. ~/.bashrc文件中正确设置语言环境对我们~/.bashrc


#4楼

I have tried a lot of things, but the sample code here works perfect. 我尝试过很多东西,但这里的示例代码非常完美。 Link 链接

The crux of the code is: 代码的关键是:

String s = "एक गाव में एक किसान";
String out = new String(s.getBytes("UTF-8"), "ISO-8859-1");

#5楼

I can't answer your original question but I would like to offer you some advice -- don't depend on the JVM's default encoding. 我无法回答你原来的问题,但我想提供一些建议 - 不要依赖于JVM的默认编码。 It's always best to explicitly specify the desired encoding (ie "UTF-8") in your code. 最好在代码中明确指定所需的编码(即“UTF-8”)。 That way, you know it will work even across different systems and JVM configurations. 这样,您就知道它甚至可以跨不同的系统和JVM配置工作。


#6楼

I think a better approach than setting the platform's default character set, especially as you seem to have restrictions on affecting the application deployment, let alone the platform, is to call the much safer String.getBytes("charsetName") . 我认为比设置平台的默认字符集更好的方法,特别是因为你似乎对影响应用程序部署有限制,更不用说平台了,就是调用更安全的String.getBytes("charsetName") That way your application is not dependent on things beyond its control. 这样你的应用程序就不依赖于它无法控制的东西。

I personally feel that String.getBytes() should be deprecated, as it has caused serious problems in a number of cases I have seen, where the developer did not account for the default charset possibly changing. 我个人认为String.getBytes()应该被弃用,因为它在我见过的许多情况下都会造成严重问题,开发人员没有考虑可能更改的默认字符集。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值