读取json文件参数

package utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
@Slf4j
public class FIleUtil {

    public static String readCsv(String filePath) {
        StringBuilder sb = new StringBuilder();
        
        try {
            //获取当前项目的绝对路径,加上文件的相对路径,得到文件的绝对路径
       File file=new File("");
       filePath=file.getAbsolutePath()+"\src\test\"+filePath;

            FileReader fr = new FileReader(filePath);
            BufferedReader bf = new BufferedReader(fr);

            String line = "";
            //组装读取结果
            while ((line = bf.readLine()) != null) {
                sb.append(line + "
");
            }
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(),e.fillInStackTrace());
        } catch (IOException e) {
            log.error(e.getMessage(),e.fillInStackTrace());
        }
        return sb.toString();
    }

    public static void main(String[] args) throws IOException {
        String str = readCsv("data\param.json");
        JSONObject json = JSON.parseObject(str);
        System.out.println(json.getString("name"));
    }
}

关于获取文件的路径,参考

https://blog.csdn.net/qq_36838191/article/details/82726833

原文地址:https://www.cnblogs.com/yjh1995/p/12163056.html