huang
2025-10-30 a99650cb00bf5b0650c33f39a4221b765201d228
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
package com.mes.common;
 
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
 
import java.io.*;
 
/**
 * 读取文件类
 *
 * @author yzx
 * @version 1.0
 */
public class ReadFile {
    /**
     * 读取Json文件内容
     * @param fileUrl
     * @return
     * @throws IOException
     */
    public static JSONObject readJson(String fileUrl) throws IOException {
        // 资源路径(相对于resources根目录)
 
        String resourcePath = fileUrl;
        // 获取类加载器
 
        try (InputStream inputStream = new FileInputStream(resourcePath)) {
            if (inputStream == null) {
                throw new IOException("资源未找到: " + resourcePath);
            }
            StringBuilder content = new StringBuilder();
            // 读取文本内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
            String str = content.toString();
 
            return JSONObject.parseObject(str);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}