简体   繁体   English

如何在不将字符转换为 Java 中转义的 unicode 字符的情况下保存 UTF-8 文件?

[英]How can I save UTF-8 files without converting characters to escaped unicode characters in Java?

I'm trying to save YAML files with UTF-8 characters but characters are automatically turning into escaped unicode characters.我正在尝试使用 UTF-8 字符保存 YAML 文件,但字符会自动转换为转义的 unicode 字符。

For example:例如:

&4&l➸ &5&lLegendary Crate

turns into:变成:

&4&l\xe2\ufffd\xb8 &5&lLegendary Crate

But when I run my program from Intellij IDEA with Run button it saves the files the way it originally is.但是当我使用运行按钮从 Intellij IDEA 运行我的程序时,它会按照原来的方式保存文件。

I'm using Bukkit API's FileConfiguration class to save YAML files.我正在使用Bukkit API 的 FileConfiguration 类来保存 YAML 文件。

Here is my code:这是我的代码:

private Map<String, FileConfiguration> configurations = new HashMap<>();
private void saveConfig(String fileName) {
    try {
        configurations.get(fileName).save(new File("db/", fileName));
    } catch (IOException ex) {
        System.out.println("Couldn't save! Exception: " + ex);
    }
}

I'm assuming it might be solved with launch parameters but -Dfile.encoding=UTF-8 didn't do any help.我假设它可以通过启动参数解决,但-Dfile.encoding=UTF-8没有任何帮助。

Well apparently you should add launch parameters before -jar <program>.jar .显然你应该在-jar <program>.jar之前添加启动参数。

java -Dfile.encoding=UTF-8 -jar <program>.jar solved the issue for me. java -Dfile.encoding=UTF-8 -jar <program>.jar为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM