从 jar 文件生成 UTF-8 文件

发布于 2024-10-04 06:45:12 字数 392 浏览 0 评论 0原文

我想使用以下代码从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云淡风轻 2024-10-11 06:45:12

在 Launch4j 中 JRE 选项卡上的 JVM 选项字段中(加载配置文件后)输入以下内容:

-Dfile.encoding=UTF8

或者,可以在应用程序的 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:

-Dfile.encoding=UTF8

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

独行侠 2024-10-11 06:45:12
  1. "UTF8" 应为 "UTF-8"

  2. 另外OutputStreamReader(outputStream, encosingString) 抛出 UnsupportedEncodingException,您应该捕获它。

  3. 主要错误:从不捕获(或传递给main)通用异常

    public static void main(String[] args) 抛出异常 {}
    

    这使您无法看到真正的异常是什么以及发生在哪里。

  1. "UTF8" should be "UTF-8".

  2. Also OutputStreamReader(outputStream, encosingString) throws UnsupportedEncodingException which you should catch.

  3. Major mistake: never catch (or pass to main) a generic Exception

    public static void main(String[] args) throws 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.

白昼 2024-10-11 06:45:12

尝试 "UTF-8" 相反。

Try "UTF-8" instead.

笑着哭最痛 2024-10-11 06:45:12

我遇到了同样的问题..我解决了它
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文