我有一些给定的属性文件,如下所示:
firstvalue=1
secondvalue=hello
我使用此Ant脚本来更改值:
<propertyfile file="A.properties">
<entry key="firstvalue" value="2" />
</propertyfile>
执行脚本后,属性文件现在为:
#Wed, 20 May 2015 11:42:46 +0200
=
firstvalue=2
secondvalue=hello
我理解为什么Ant任务会添加日期注释,但为什么Ant也会在第二行添加此符号?
我认为这是由编码文件引起的,因为我无法使用我的所有属性文件重现问题,但只能使用其他人手动制作的文件。有没有办法在使用Ant?
中的文件之前避免这种行为或修复编码编辑: 这是我的属性文件,在脚本和在线hexdump工具之前看到: 文件名:A.properties 哑剧类型:
0000-0010: ef bb bf 0d-0a 66 69 72-73 74 76 61-6c 75 65 3d .....fir stvalue=
0000-0020: 31 0d 0a 73-65 63 6f 6e-64 76 61 6c-75 65 3d 68 1..secon dvalue=h
0000-0024: 65 6c 6c 6f
ello
以下相同的文件:
file name: A.properties
mime type:
0000-0010: 23 54 68 75-2c 20 32 31-20 4d 61 79-20 32 30 31 #Thu,.21 .May.201
0000-0020: 35 20 31 35-3a 32 31 3a-31 32 20 2b-30 32 30 30 5.15:21: 12.+0200
0000-0030: 0d 0a ef bb-bf 3d 0d 0a-66 69 72 73-74 76 61 6c .....=.. firstval
0000-0040: 75 65 3d 32-0d 0a 73 65-63 6f 6e 64-76 61 6c 75 ue=2..se condvalu
0000-0049: 65 3d 68 65-6c 6c 6f 0d-0a e=hello. .
答案 0 :(得分:1)
问题是文件前面的UTF-8编码的字节顺序标记(ef bb ff)。属性文件始终以ISO8859-1编码,而不是UTF-8,因此文件不是有效的属性文件。我强烈建议修复属性文件(并让其他人切换到不会重新引入问题的编辑器),但如果您需要短期解决方法,可以先删除BOM:
<copy file="A.properties" encoding="UTF-8" tofile="A.properties.tmp">
<filterchain>
<deletecharacters chars=""/>
</filterchain>
</copy>
<move file="A.properties.tmp" tofile="A.properties"/>
<propertyfile file="A.properties">
<entry key="firstvalue" value="2" />
</propertyfile>