Skip to content

Commit 99ad732

Browse files
committed
replaced FileReader to fix UTF-8 encoding
1 parent 657a7cf commit 99ad732

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/main/java/net/b07z/sepia/server/core/tools/FilesAndStreams.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public static interface LineOperation {
3838
}
3939

4040
/**
41-
* Collect all data of an InputStream to a string.
41+
* Collect all data of an InputStream to a string.<br>
42+
* NOTE: Please define encoding of stream!
4243
*/
4344
public static String getStringFromStream(InputStream stream) {
4445
try (BufferedReader in = new BufferedReader(new InputStreamReader(stream))) {

src/main/java/net/b07z/sepia/server/core/tools/JSON.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package 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;
59
import java.util.Arrays;
610
import java.util.Collections;
711
import 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

Comments
 (0)