設置默認的Java字符編碼

本文翻譯自: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()應該被棄用,因爲它在我見過的許多情況下都會造成嚴重問題,開發人員沒有考慮可能更改的默認字符集。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章