[英]Why does my text normalization behave differently in different environments?
I am normalizing some accented text using the following approach / code taken from this answer 我正在使用以下方法/从此答案中获取的代码来规范一些重音文字
Accent removal: 去除口音:
String accented = "árvíztűrő tükörfúrógép";
String normalized = Normalizer.normalize(accented, Normalizer.Form.NFD);
normalized = normalized.replaceAll("[^\\p{ASCII}]", "");
System.out.println(normalized);
When I run this from with IntelliJ (as part of a unit test), this gives the expected result: 当我使用IntelliJ进行运行时(作为单元测试的一部分),这给出了预期的结果:
arvizturo tukorfurogep
If I run this from the command line (via gradle), I get: 如果从命令行(通过gradle)运行此命令,则会得到:
ArvAztArA tAkArfArAgA
In both cases, I'm using the same PC and Java 1.8.0_151
. 在这两种情况下,我都使用同一台PC和Java 1.8.0_151
。
The relevant parts from build.gradle
: build.gradle
的相关部分:
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
What causes this different behaviour? 是什么导致这种不同的行为? And how do I ensure I get the expected result everywhere? 以及如何确保到处都能获得预期的结果?
Thanks to @eckes and others for the compile time suggestion. 感谢@eckes和其他人的编译时间建议。 By specifying an encoding at compile time, I was able to get the desired result. 通过在编译时指定编码,我可以获得所需的结果。
The setting I added to build.gradle
was: 我添加到build.gradle
的设置是:
compileTestJava.options.encoding = 'UTF-8'
This option only affects the test classes (which is where my issue was). 此选项仅影响测试类(这是我的问题所在)。 You can also use: 您还可以使用:
compileJava.options.encoding = 'UTF-8'
if you have text in your production code that needs to be encoded. 如果您的生产代码中有需要编码的文本。
An alternative solution I came across is: 我遇到的另一个解决方案是:
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
(Interestingly, none of the above solutions changed the value of the file.encoding
system property.) (有趣的是,以上解决方案均未更改file.encoding
系统属性的值。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.