在項目中遇到讀取以json格式存儲的配置文件,為了方便操作,將文件內容讀取出來並轉換為json對象,本文使用的是fastjson
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.nio.charset.StandardCharsets;
/**
* 讀取配置文件
* @author oldwei
*/
public class ConfJsonUtil {
public static JSONObject toJson() throws IOException {
String filePath = System.getProperty("user.dir") + "/sdk/conf.json";
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
String conf = IOUtils.toString(in, StandardCharsets.UTF_8);
return JSON.parseObject(conf);
}
}
