UTF-8 when saving FileConfiguration

This commit is contained in:
LOOHP 2021-02-21 14:58:05 +08:00
parent 9fe48fc2a9
commit 48cd3a8b9a
1 changed files with 7 additions and 5 deletions

View File

@ -1,11 +1,14 @@
package com.loohp.limbo.File; package com.loohp.limbo.File;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Reader; import java.io.Reader;
import java.io.StringWriter; import java.io.StringWriter;
@ -106,8 +109,8 @@ public class FileConfiguration {
customRepresenter.setPropertyUtils(customProperty); customRepresenter.setPropertyUtils(customProperty);
Yaml yaml = new Yaml(customRepresenter, options); Yaml yaml = new Yaml(customRepresenter, options);
StringWriter writer = new StringWriter(); ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(writer); PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
if (header != null) { if (header != null) {
pw.println("#" + header.replace("\n", "\n#")); pw.println("#" + header.replace("\n", "\n#"));
} }
@ -115,8 +118,7 @@ public class FileConfiguration {
pw.flush(); pw.flush();
pw.close(); pw.close();
String str = writer.toString(); String str = new String(out.toByteArray(), StandardCharsets.UTF_8);
writer.close();
return str; return str;
} }
@ -135,7 +137,7 @@ public class FileConfiguration {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
PrintWriter pw = new PrintWriter(file, StandardCharsets.UTF_8.toString()); PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
if (header != null) { if (header != null) {
pw.println("#" + header.replace("\n", "\n#")); pw.println("#" + header.replace("\n", "\n#"));
} }