11package net .b07z .sepia .server .core .tools ;
22
3- import java .io .FileReader ;
4- import java .io .FileWriter ;
3+ import java .io .FileInputStream ;
4+ import java .io .FileOutputStream ;
5+ import java .io .InputStreamReader ;
6+ import java .io .OutputStreamWriter ;
7+ import java .io .Reader ;
8+ import java .nio .charset .StandardCharsets ;
59import java .util .Arrays ;
610import java .util .Collections ;
711import java .util .Iterator ;
@@ -630,7 +634,7 @@ public static JSONObject deepMerge(JSONObject source, JSONObject target){
630634 }
631635
632636 /**
633- * Write a JSONObject to a file.
637+ * Write a JSONObject to a file (UTF-8 encoding) .
634638 * @param filePath - path including file name
635639 * @param obj - JSONObject
636640 * @return true/false
@@ -639,8 +643,10 @@ public static boolean writeJsonToFile(String filePath, JSONObject obj){
639643 if (obj == null ){
640644 return false ;
641645 }
642- try (FileWriter file = new FileWriter (filePath )) {
643- file .write (obj .toJSONString ());
646+ //try (FileWriter file = new FileWriter(filePath)) {
647+ try (OutputStreamWriter w = new OutputStreamWriter (new FileOutputStream (filePath ), StandardCharsets .UTF_8 );){
648+ //file.write(obj.toJSONString());
649+ w .write (obj .toJSONString ());
644650 return true ;
645651 }catch (Exception e ){
646652 System .err .println (DateTime .getLogDate () + " ERROR - JSON.java / writeJsonToFile() - Failed to write: " + filePath + " - MSG: " + e .getMessage ());
@@ -649,14 +655,15 @@ public static boolean writeJsonToFile(String filePath, JSONObject obj){
649655 }
650656 }
651657 /**
652- * Read a JSONObject from file.
658+ * Read a JSONObject from file (UTF-8 encoding) .
653659 * @param filePath - path including file name
654660 * @return JSONObject or null
655661 */
656662 public static JSONObject readJsonFromFile (String filePath ){
657- try {
663+ try ( Reader r = new InputStreamReader ( new FileInputStream ( filePath ), StandardCharsets . UTF_8 );) {
658664 JSONParser parser = new JSONParser ();
659- Object obj = parser .parse (new FileReader (filePath ));
665+ Object obj = parser .parse (r );
666+ //Object obj = parser.parse(new FileReader(filePath));
660667 JSONObject jsonObject = (JSONObject ) obj ;
661668 return jsonObject ;
662669
0 commit comments