如何在Java中为Ant Task参数指定字符编码 [英] How to specify character encoding for Ant Task parameters in Java
问题描述
我在Java中写一个ANT任务。
I'm writing an ANT task in Java.
在我的build.xml中,我指定了参数,它应该从我的java类中读取。当我在这些参数中使用特殊字符,如德语变音符号(Ö,Ä,Ü)时会出现问题。在我的java任务中,它们显示为?字符(使用eclipse中的System.out.print)。
In my build.xml I specify parameters, which should be read from my java class. Problems occur, when I use special characters, like german umlauts (Ö,Ä,Ü) in these parameters. In my java task they appear as ?-characters (using System.out.print from within eclipse).
我的所有文件都编码为UTF-8。和我的build.xml有相应的声明:
All my files are encoded as UTF-8. and my build.xml has the corresponding declaration:
<?xml version="1.0" encoding="UTF-8" ?>
有关编写任务的详细信息:我根据 http://ant.apache.org/manual/develop.html (特别是第5点嵌套的元素)。
我在我的任务中有嵌套的元素:
For the details of writing the task: I do it according to http://ant.apache.org/manual/develop.html (especially Point 5 nested elements). I have nested elements in my task like:
<parameter name="test" value="ÖÄÜtest"/>
和一个java方法:
public void addConfiguredParameter(Parameter prop) {
System.out.println(prop.getValue());
//prints ???test
}
读取参数值
推荐答案
这里有几个转码操作:
- 您的编辑器将XML保存为UTF-8
- 使用十六进制编辑器检查字符是否正确编码
- 这是一个错误。
- 检查所使用的编码是否支持字符
- 检查控制台使用的编码是否与
PrintStream
- Check that the encoding used by the console matches that of the
PrintStream
-8,您会在XML文件中看到以下编码值:
Encoded as UTF-8, you would expect the following encoded values in your XML file:
Grapheme UTF-8 encoded bytes
Ö c3 96
Ä c3 84
Ü c3 9c
这篇关于如何在Java中为Ant Task参数指定字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!