2015-06-20 34 views
2

我尝试读取从一个变音符号文件属性,这是我的build.gradle:摇篮使用错误的编码(拉丁语 - 1)属性文件

task utf8test << { 

    Properties props = new Properties() 
    def propFile = new File("my.property") 
    if (propFile.canRead()) { 
     props.load(new FileInputStream(propFile)) 
     for (Map.Entry property in props) { 
       println property.value 
     } 
    } 
} 

我的财产文件看起来像(UTF-8编码):

challenge: ö 

如果我执行任务:gradle utf8test 结果看起来像

:utf8test 
ö 

BUILD SUCCESSFUL 

Total time: 0.877 secs 

“ö”更改为“Ô,这很容易理解。 作为十六进制的“ö”是c3b6,latin-1中的c3是Ã,b6是¶,但这不是我所期望的。

问:如何配置gradle这个在性质为UTF-8读取编码

更多信息:

println propFile.text 

如果我在gradle这个与打印出propFiles内容

我收到“ö”作为输出,所以文件被正确读入并且输出由我的shell正确编码。

摇篮守护与运行:-Dfile.encoding = UTF-8

的gradle执行与-Dfile.encoding = UTF-8:gradle utf8test -Dfile.encoding=UTF-8没有帮助,也没有在bash所做export GRADLE_OPTS="-Dfile.encoding=UTF-8",也不会增加systemProp.file.encoding=utf-8到gradle.properties。

我无法找到gradle中Properties-Class的文档页面,有没有任何选项可以配置编码?

非常感谢迄今!

回答

5

这是预料之中的,与gradle没有多大关系。 documentation of java.util.Properties(与Gradle无关,但是是JDK的标准类)明确指定属性文件的标准编码是ISO-8859-1。如果你是唯一一个阅读该文件,并希望它包含UTF-8,那么明确地读它为UTF-8:

Properties props = new Properties() 
def propFile = new File("my.property") 
if (propFile.canRead()) { 
    props.load(new InputStreamReader(new FileInputStream(propFile), StandardCharsets.UTF_8)); 
    for (Map.Entry property in props) { 
      println property.value 
    } 
} 
+0

感谢您的完美答案!并将我链接到java文档! – Murmel