问题描述
我有一个 Java 项目,它读取 UTF-8 编码的 .txt 文件以构建显示的字符串.在 Eclipse 中运行,文件按预期读取和显示,但在我的 ant 构建之后,字符没有按预期显示.
I have a Java project that reads UTF-8 encoded .txt files in order to construct strings that are displayed. Running in Eclipse the files are read and displayed as expected but after my ant build the characters are not coming out as they should.
这是我的 build.xml 文件
Here is my build.xml file
<?xml version="1.0"?>
<project name="Rutherford" default="jar">
<property name="libsSrc" value="libs"/>
<property name="build" value="build"/>
<property name="classes" value="build/classes"/>
<property name="jar" value="build/jar"/>
<property name="libs" value="build/libs"/>
<path id="classpath">
<fileset dir="${libsSrc}" includes="*.jar"/>
</path>
<pathconvert property="mf.classpath" pathsep=" ">
<path refid="classpath"/>
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="lib/*.jar"/>
</chainedmapper>
</mapper>
</pathconvert>
<target name="clean" description="remove intermediate files">
<delete dir="build"/>
</target>
<target name="compile" description="compile the Java source code to class files">
<mkdir dir="${classes}"/>
<javac srcdir="." destdir="${classes}" classpathref="classpath">
<compilerarg line="-encoding utf-8"/>
</javac>
</target>
<target name="jar" depends="compile" description="create a Jar file for the application">
<mkdir dir="${jar}"/>
<jar destfile="${jar}/App.jar">
<zipgroupfileset dir="${libsSrc}" includes="*.jar"/>
<fileset dir="${classes}" includes="**/*.class"/>
<manifest>
<attribute name="Main-Class" value="nat.rutherford.DesktopStarter"/>
<attribute name="Class-Path" value="${mf.classpath}"/>
</manifest>
</jar>
</target>
</project>
我尝试使用 UTF-8 字符编码进行编译
I tried to compile with UTF-8 character encoding using
<compilerarg line="-encoding utf-8"/>
但显然这还不够,我需要修改什么才能使其正常工作?
but obviously this is not enough, what do I need to modify to get this to work?
谢谢
编辑 1
我阅读了 .txt 文件.
I read the .txt files in with.
public String fileToString(String file) {
InputStream in = new BufferedInputStream(Gdx.files.internal(file).read());
return new Scanner(in).useDelimiter("\\A").next();
}
将字符串返回给 String 变量,然后我只需将这个字符串传递给 对象
which returns a string to a String variable, then I simply take this string and pass it to an object
在eclipse中编译运行时可以正常工作.
It works ok when compiling and running within eclipse.
编辑 2
这是修复
public String fileToString(String file) {
InputStream in = new BufferedInputStream(Gdx.files.internal(file).read());
return new Scanner(in, "UTF-8").useDelimiter("\\A").next();
}
当您知道去哪里寻找时很简单!谢谢!
Simple when you know where to look! lol Thanks!
推荐答案
根据 文档,使用 encoding='utf-8'
而不是 compiler-arg
.
As per The documentation, use encoding='utf-8'
instead of compiler-arg
.
然而......我怀疑你的问题是运行时,而不是编译时间.您是否在不传递编码的情况下创建 OutputStreamWriter
对象?或者将有趣的字符传递给 System.out.println
?或者相应的InputStreamReader
或Scanner
?在这种情况下,修复方法是将 -Dfile.encoding=utf-8
添加到命令行.
However ... I suspect that your problem is runtime, not compile time. Do you create OutputStreamWriter
objects without passing an encoding? Or pass interesting chars to System.out.println
? Or correspondingly InputStreamReader
or Scanner
? The fix in this case would be to add -Dfile.encoding=utf-8
to the command line.
这篇关于Java 应用程序读取 UTF-8 编码的文本文件,但在 ant 构建后,字符不符合预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!