如何在Java中为Ant Task参数指定字符编码 [英] How to specify character encoding for Ant Task parameters in Java

查看:22
本文介绍了如何在Java中为Ant Task参数指定字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 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
}

读取参数值.

推荐答案

这里有几个转码操作:

  1. 通过编辑器将 XML 保存为 UTF-8
    • 使用十六进制编辑器检查字符编码是否正确
  • 这里不太可能出现故障
  • 检查使用的编码是否支持字符
  • 检查控制台使用的编码是否与PrintStream

编码为 UTF-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屋!

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