java - Gradle 2.3 + JUnit 4.11 = 损坏的字符集

标签 java unicode junit character-encoding gradle

正常情况下可以将正确的字节写入文件,但结合 Gradle 2.3 + JUnit 4.11 会出现意想不到的结果。

import org.junit.Test;
import java.io.FileOutputStream;
import static org.junit.Assert.*;

public class TestUTF16Test
{
   @Test
   public void testWrite() throws Exception
   {
      try (FileOutputStream bin = new FileOutputStream("testUTF16junit.txt")) {
         bin.write("Русский".getBytes("UTF-16LE"));
      }
   }
}

构建.gradle

apply plugin: 'java'

sourceCompatibility = 1.7
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

$ gradle.bat:干净:测试

:clean
:compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning

:processResources UP-TO-DATE
:classes
:compileTestJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning

:processTestResources UP-TO-DATE
:testClasses
:test

BUILD SUCCESSFUL

Total time: 4.999 secs

结果:

20 04 A0 00 21 04 53 04 21 04 03 04 21 04 03 04... 28 bytes

但应该是:

20 04 43 04 41 04 41 04 3A 04 38 04 39 04 = 14 bytes

操作系统:Microsoft Windows 7 [版本 6.1.7601]

最佳答案

Gradle 修复

[ compileJava, compileTestJava ]*.options*.encoding = 'UTF-8'

2015年,javac无法识别BOM的Unicode!

>javac.exe src\test\java\TestUTF16Test.java
src\test\java\TestUTF16Test.java:1: error: illegal character: '\u00bb'
я╗┐import org.junit.Test;
 ^
1 error

>javac.exe -version
javac 1.8.0_25

现在我明白为什么IDE不添加BOM了...

关于java - Gradle 2.3 + JUnit 4.11 = 损坏的字符集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28788754/

相关文章:

java - 使用spring 3.1访问jsp中的数据库连接字符串

java - Hibernate - 在 SQLite 数据库中将 UUID 的默认类型从二进制更改为文本

java - Robotium 5.2.1 示例记事本

java - URLConnection 上的 JUnit 测试,使用 EasyMock?

java - 为什么这段代码会抛出 IOException?

java - 如何复制二维字符串数组?

javascript - 用于升序和降序的 unicode 图标

c - argv 的编码是什么?

c++ - C++ 中的波斯语字符 utf8

java - 为什么JUnit使用CountDownLatch来实现FailOnTimeout