swb
2024-09-12 a7181383bbd5475e7dcbd1fcd29020521c85d58d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.moral.api.utils;
 
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
 
import java.io.*;
 
public class JsonUtil {
 
    /**
     * 读取JSON文件转换为字符串
     * @param filePath
     * @return
     */
    public static String readJsonFile(String filePath) {
        String jsonStr = "";
        try {
            File jsonFile = new File(filePath);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
 
    public static String testReadFile(String filePath) throws IOException {
//        ClassPathResource classPathResource = new ClassPathResource("resource.properties");
        String jsonStr = "";
        Resource resource = new ClassPathResource(filePath);
        InputStream is = resource.getInputStream();
        Reader reader = new InputStreamReader(resource.getInputStream(), "utf-8");
        int ch = 0;
        StringBuffer sb = new StringBuffer();
        while ((ch = reader.read()) != -1) {
            sb.append((char) ch);
        }
        reader.close();
        jsonStr = sb.toString();
        return jsonStr;
    }
 
}