讀取文件時候空指針異常
//獲取文件
File file = new File(ReadJSONSample.class.getResource("/wxe.json").getFile());
if(file == null){
? ?System.out.print("空指針異常");
}else{
? ?System.out.print(file.getName());
? ?//讀取文件內(nèi)容
? ?String content = FileUtils.readFileToString(file,"utf-8");
? ?JSONObject jsonObject = new JSONObject(content);
? ?System.out.print("姓名是:" + jsonObject.getString("name"));
? ?System.out.print("年齡是:" + jsonObject.getDouble("age"));
? ?System.out.print("是否有女朋友:" + jsonObject.getBoolean("has_girlfriend"));
? ?System.out.print("姓名是:" + jsonObject.getString("name"));
}
2017-06-13
路徑是在src下,不是在java包下
2017-03-30
//獲取文件
File file = new File("C:\\Users\\65197\\jsontest\\src\\main\\java\\wxe.json");
改成這樣就沒有錯了
2017-03-30
路徑對嗎 ?代碼在哪個文件?