Charset.defaultCharset()在JDK1.7和JDK 1.6下获得不同的结果

编程入门 行业动态 更新时间:2024-10-24 09:28:36
本文介绍了Charset.defaultCharset()在JDK1.7和JDK 1.6下获得不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在测试我的应用程序的i18n兼容性。 我有一个英文版的Windows 7,这意味着系统的显示语言是英语。

I am testing my application's i18n compatibility. I have a English version of Windows 7 which mean the system's display language is English. And I set the system locale as Chinese for non-unicode application.

我的应用程序在jdk1.6下导出带汉字的Html文件时遇到问题,但运行时运行正常下面的jdk1.7。

My application encountered problems when exporting Html files with Chinese character under jdk1.6, but works fine when running under jdk1.7.

我调试它,发现直接的原因是 Charset.defaultCharset()返回不同值。

I debugged it and found the direct reason was that Charset.defaultCharset() returned different values.

在jdk1.7 Charset.defaultCharset()下返回 GBK

Under jdk1.7 Charset.defaultCharset() returned GBK which is the charset for chinese.

在jdk1.6下 Charset.defaultCharset()返回 window_1252 这是拉丁语的字符集。

Under jdk1.6 Charset.defaultCharset() returned window_1252 which is charset for Latin language.

我知道问题可以通过指定charset解决,说

I know the problem can be solved by designate charset,say utf-8, in code.

但我想知道为什么 Charset.defaultCharset()在JDK1.7和JDK 1.6下返回不同的值。

But I want to know why Charset.defaultCharset() return different values under JDK1.7 and JDK 1.6 .

推荐答案

Charset.defaultCharset )给出JVM运行的字符集,因此它并不总是相同的值。例如,如果您使用Netbeans运行程序,则它将始终返回UTF-8,因为这是Netbeans中Java项目的默认编码。

Charset.defaultCharset() gives the charset of JVM running, so it is not always the same value. For example if you are running your programs with Netbeans, it will always return UTF-8, since that's the default encoding for Java Projects in Netbeans.

我有一个类似于您的设置。我的Windows是英语(菜单,对话框是英语),我使用土耳其语为非Unicode应用程序。当我启动没有任何标志或系统参数的JVM时,当调用 Charset.defaultCharset()时,Java 7和Java 6运行时都会提供CP1254。 System.getProperty(file.encoding)和默认IO编码也相同。 (系统的区域在这两个Java版本中是不同的,但是这是另一个故事。)

I have a setup similar to yours. My Windows is English (menus, dialogs are English) and I'm using Turkish for non-Unicode applications. When I start JVM without any flag or system parameter, both Java 7 and Java 6 runtimes give "CP1254" when Charset.defaultCharset() is called. System.getProperty("file.encoding") and default IO encoding are also the same. ( The locale of the system is different in these two Java versions, however that's another story. )

所以我想你的问题是关于如何启动JVM,关于JVM如何决定应该使用的默认编码。如果您确定问题不是前一个(您运行JVM没有任何编码参数,并且不尝试更改程序中任何位置的默认字符集),那么JVM不正确地获取默认编码,很可能是异常行为。

So I guess your problem is either about how you start your JVM, or about how JVM decides to default encoding it should use. If you are sure that the problem is not the former one (you run JVM without any encoding parameter and you do not attempt to change the default charset anywhere in your program), then JVM fetches the default encoding incorrectly and most probably that's abnormal behaviour.

更多推荐

Charset.defaultCharset()在JDK1.7和JDK 1.6下获得不同的结果

本文发布于:2023-10-31 06:31:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:defaultCharset   Charset   JDK

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!