从 jar 文件生成 UTF-8 文件
我想使用以下代码从 jar 文件创建一个 UTF-8 文件:
public class UTF8 {
public static void main(String[] args) throws Exception {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
"utf8.txt"), "UTF8"));
out.write("Look at this BASTÖÖÜÜÄÄ!");
}
}
此代码使用 eclipse 效果很好。但如果我将其打包到 jar 文件中,它会使用依赖于平台的文件编码(例如 Windows 上的 Cp1252)。我能做什么呢?
I want to create a UTF-8 file from a jar-file with the following code:
public class UTF8 {
public static void main(String[] args) throws Exception {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
"utf8.txt"), "UTF8"));
out.write("Look at this BASTÖÖÜÜÄÄ!");
}
}
This code works very well using eclipse. But if I pack it to a jar file, it uses a platform depended file encoding (Cp1252 for example on Windows). What could I do against that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Launch4j 中 JRE 选项卡上的 JVM 选项字段中(加载配置文件后)输入以下内容:
或者,可以在应用程序的 ini 文件中指定相同的内容 - 请参阅 launch4j 文档:
http://launch4j.sourceforge.net/docs.html#Additional_jvm_options
In the JVM options field on the JRE tab in Launch4j (after loading your configuration file) enter the following:
ALternatively, can specify the same thing in an ini file for your application - see the launch4j documentation at:
http://launch4j.sourceforge.net/docs.html#Additional_jvm_options
"UTF8"
应为"UTF-8"
。另外
OutputStreamReader(outputStream, encosingString)
抛出UnsupportedEncodingException
,您应该捕获它。主要错误:从不捕获(或传递给main)通用
异常
这使您无法看到真正的异常是什么以及发生在哪里。
"UTF8"
should be"UTF-8"
.Also
OutputStreamReader(outputStream, encosingString)
throwsUnsupportedEncodingException
which you should catch.Major mistake: never catch (or pass to main) a generic
Exception
This prevented you to see what your real exception was and where happened.
调试编码问题时的经验法则:如果字符串文字中有非 us-ascii 字母,请尝试用 Unicode 转义符替换它们(即
BAST\u00d6\u00d6\u00dc\u00dc\u00c4\u00c4
)。如果问题消失,则说明源/编译器编码不匹配。例如,如果源编码是 Windows-1252 而编译器编码是 UTF-8,则可能会发生您所描述的行为。
The rule of thumb when debugging encoding problems: if you have non-us-ascii letters in string literals, try to replace them by Unicode escapes (i.e.
BAST\u00d6\u00d6\u00dc\u00dc\u00c4\u00c4
). If the problem disappears, you have a source/compiler encoding mismatch.For example, the behaviour you describe may happen if your source encoding is Windows-1252 whereas compiler encoding is UTF-8.
尝试
"UTF-8"
相反。Try
"UTF-8"
instead.我遇到了同样的问题..我解决了它
java -Dfile.encoding=UTF8 -jar CAP.jar (其中 CAP 是 JAR 文件的名称)
我创建了一个批处理文件。打开文本文件,写入上面的行并将其另存为 .bat,它应该可以工作
i faced the same problem .. and i solved it with
java -Dfile.encoding=UTF8 -jar CAP.jar ( where CAP the name of the JAR File)
I mad a batch file. open text file, write the line above and save it as .bat and it should work