在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ java 怎么讀取test.json 文件,怎么改這個錯誤

java 怎么讀取test.json 文件,怎么改這個錯誤

圖片描述

package com.utils;
import java.io.*;

public class JsonUtils {
    public static String start(String pathString) {
        System.out.println(pathString);
        return pathString;
    }
    //從給定位置讀取Json文件
    public static String readJson(String path){
        //從給定位置獲取文件
        File file = new File(path);
        BufferedReader reader = null;
        //返回值,使用StringBuffer
        StringBuffer data = new StringBuffer();
        //
        try {
            reader = new BufferedReader(new FileReader(file));
            //每次讀取文件的緩存
            String temp = null;
            while((temp = reader.readLine()) != null){
                data.append(temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //關(guān)閉文件流
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return data.toString();
    }
}
package com.utils;


public class utilsTest {

    public static void main(String[] args) {
        String jo = JsonUtils.readJson("./test.json");
        System.out.println(jo);
    }

}
{
  "cat":"it",
  "languages":[
    {"id":1,"ide":"Eclipse","name":"Java"},
    {"id":2,"ide":"Xcode","name":"Swift"},
    {"id":3,"ide":"Visual Studio","name":"C#"}
  ],
  "pop":true
}
回答
編輯回答
舊螢火
String classPath = JsonUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();

JsonUtils.readJson(classPath + "test.json");

編譯完成后,class 文件和 json 文件在 target 目錄下,你可以觀察一下這個目錄

2017年5月3日 19:25