Java源文件和失败测试的字符集 [英] Charset of Java source file and failing test

查看:154
本文介绍了Java源文件和失败测试的字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说我花了很多时间搜索解释/解决方案。我发现了这个问题的提示,但没有办法解决我的特定问题。因此,至少在某些情况下,一个主题的帖子似乎已被殴打死亡。

First, I'd like to say that I've spent a lot of time searching for an explanation/solution. I've found hints of the problem, but no way to resolve my particular issue. Hence the post on a topic that seems to have been beaten to death in at least some cases.

我有一个Java测试类,测试正确的编码/解码Mime实用程序。用于测试的字符串在源文件中声明,我们使用assertEquals()在处理输入字符串后测试等式。这里是一个例子:

I have a Java test class that tests for proper encoding/decoding by a Mime utility. The strings used for testing are declared in the source file and we use assertEquals() to test equality after processing the input string. Here's an example:

String test = "S2, =?iso-8859-1?Q?F=E4ltstr=F6m?= =?iso-8859-1?Q?,_Patrik?= S3";
String expected = "S2, Fältström, PatrikS3";



在我的编辑器(以及其他外部编辑器,如Notepad ++和UltraEdit)中,输入字符串正确显示如果我选择读它为windows-1252或ISO-8859-1编码; UTF-8显示期望的字符串为F ltstr m。

In my editor (and other external editors such as Notepad++ and UltraEdit), the input strings are properly displayed if I choose to read it as windows-1252 or ISO-8859-1 encoding; UTF-8 displays the expected string as "F�ltstr�m".

在Windows 7机器上编译并运行时,我得到以下输出:

When compiled and run on a Windows 7 machine, I get the following output:

预期:S2,F ltstr m,PatrikS3

Expected :S2, F�ltstr�m, PatrikS3

实际:S2,Fältström,PatrikS3

Actual :S2, Fältström, PatrikS3

我在命令shell以及我的代码编辑器中得到了这个行为。奇怪的是,它在Windows XP机器上工作。但是我在命令shell中使用chcp检查了代码页,在这两种情况下,我得到相同的输出。我得到这个工作的唯一方法是使用-encoding windows-1252编译类,我不想因为各种原因。

I get this behaviour in a command shell as well as in my code editor. Bizarrely, it works on a Windows XP machine. Yet I checked the codepage using chcp in a command shell and I get the same output in both cases. The only way I got this to work is to compile the class using "-encoding windows-1252", which I don't want to be doing for a variety of reasons.

所以问题是:
1)XP和Windows 7之间有什么不同,使这失败?是否更改了默认平台编码?
2)我如何修复,以便它可以在Windows 7机器和Linux机器上工作?

So the questions are: 1) what's different between XP and Windows 7 that makes this fail? Has the default platform encoding changed? 2) how can I fix so that it will work both on a Windows 7 machine and a Linux machine?

非常感谢任何洞察力! p>

Thanks a lot for any insight!

推荐答案

看起来你的Windows 7机器上使用的默认编码是UTF-8,而在Windows XP是Windows-1252。所以:在编译时始终是明确的,编译时使用,不依赖平台默认。

It looks like the default encoding used on your Windows 7 machine is UTF-8, while on Windows XP it is Windows-1252. So: always be explicit in the encoding your files use when compiling, don't depend on the platform default.

BTW:据我所知,在我的Windows 7机器仍然使用Windows-1252作为默认值。

BTW: As far as I know java on my Windows 7 machine still uses Windows-1252 as the default.

这篇关于Java源文件和失败测试的字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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