读取json文件为String

public static String readJsonFile(String path){
    String jsonStr = "";
    try {
        File jsonFile = new File(path);
        FileReader fileReader = new FileReader(jsonFile);
        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);
        }
        fileReader.close();
        reader.close();
        jsonStr = sb.toString();
        return jsonStr;

    }catch (IOException e){
        e.printStackTrace();
        return null;
    }
}

 

posted @ 2021-09-22 11:29  二先森  阅读(392)  评论(0编辑  收藏  举报