0

I have 3 class to handle all configs. (2000 lines in 2 class)

And I want add support to UTF-8 chars without writing all code from 0 ;/

I show my smallest class (is not finished and I don't want to spam with 1400 lines :)) Have actual only two variables:

package com.gmail.bukkitSmerf.professionalWarns;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

public class LangHandler {
    private static String lWarns_DeletedWarnAddon, lWarns_ExpiredWarnAddon;

    public static String getlWarns_DeletedWarnAddon() {
        return lWarns_DeletedWarnAddon;
    }

    public static String getlWarns_ExpiredWarnAddon() {
        return lWarns_ExpiredWarnAddon;
    }

    public void createConfig(boolean forceConfigUpdate) {
        try {
            String langFileName = "languageEN.yml";
            InputStream input = ProfessionalWarns
                    .getPluginResource_languageEN();
            if (ConfigHandler.getcGeneral_Language().equalsIgnoreCase("PL")) {
                langFileName = "languagePL.yml";
                input = ProfessionalWarns.getPluginResource_languagePL();
            }
            File langFile = new File(ProfessionalWarns.getPluginDataFolder(),
                    langFileName);
            if (!langFile.exists()) {
                langFile.getParentFile().mkdirs();
                ConfigHandler.copy(input, langFile);
            }
            YamlConfiguration lang = new YamlConfiguration();
            FileConfiguration rawLang = YamlConfiguration
                    .loadConfiguration(input);
            lang.load(langFile);

            lWarns_DeletedWarnAddon = lang.getString("Warns.DeletedWarnAddon",
                    rawLang.getString("Warns.DeletedWarnAddon"));

            lWarns_ExpiredWarnAddon = lang.getString("Warns.ExpiredWarnAddon",
                    rawLang.getString("Warns.ExpiredWarnAddon"));

            if (ConfigHandler.iscGeneral_AutoUpdateConfigs()
                    || forceConfigUpdate) {
                FileWriter fw = new FileWriter(langFile);
                PrintWriter pw = new PrintWriter(fw);
                pw.flush();
                pw.write("Warns:\n  DeletedWarnAddon: '"
                        + lWarns_DeletedWarnAddon + "'\n  ExpiredWarnAddon: '"
                        + lWarns_ExpiredWarnAddon + "'");
                pw.close();
            }
        } catch (IOException | InvalidConfigurationException e) {
            e.printStackTrace();
            ProfessionalWarns
                    .logWarning("Error when trying create/write/reload language file!");
        }
    }
}

I don't have idea how to use that UTF-8 here.

I also don't want delete any features.

If you can, give me also some advices about that code :)

//Sorry for my English

user2250333
  • 133
  • 3
  • 17
  • What do you mean by "add support to UTF-8 chars"? Do you mean that you want to process files that contain UTF-8 characters? What happens now when you have such a file? – parsifal Jul 11 '13 at 20:30
  • @parsifal In that moment I can't test it. (I don't have any code, only Handlers for config/MySQL and players). But in old versions (this same methods) then i have problems with saving ą,ę and other symbols ;/ I try test it and then add second commend :) But first I must add small test-code on startup. – user2250333 Jul 11 '13 at 20:39

1 Answers1

0

Based on your comment, I'm going to blame your use of FileWriter:

FileWriter fw = new FileWriter(langFile);

This relies on your installation's default encoding, which I'm willing to bet is not UTF-8. You can verify this with the following:

System.out.println(Charset.defaultCharset());

Regardless, the correct way to write UTF-8 characters to a file is to use an OutputStreamWriter:

FileOutputStream fos = new FileOutputStream("your_file");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
PrintWriter pw = new PrintWriter(osw);

And unless this is one-off code, you should close the FileOutputStream in a finally block.

parsifal
  • 811
  • 7
  • 4
  • Sorry, but i can't test it. I have problem with code... I export without any changes in one of file and then that file is broken... I write when I fix that... :) – user2250333 Jul 12 '13 at 07:10