i have 3 class handle configs. (2000 lines in 2 class)
and want add support utf-8 chars without writing code 0 ;/
i show smallest class (is not finished , don't want spam 1400 lines :)) have actual 2 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 use utf-8 here.
i don't want delete features.
if can, give me advices code :)
//sorry english
based on comment, i'm going blame use of filewriter
:
filewriter fw = new filewriter(langfile);
this relies on installation's default encoding, i'm willing bet not utf-8. can verify following:
system.out.println(charset.defaultcharset());
regardless, correct way write utf-8 characters file use outputstreamwriter
:
fileoutputstream fos = new fileoutputstream("your_file"); outputstreamwriter osw = new outputstreamwriter(fos, "utf-8"); printwriter pw = new printwriter(osw);
and unless one-off code, should close fileoutputstream
in finally
block.
Comments
Post a Comment