java 讀寫json檔案
阿新 • • 發佈:2018-11-09
json檔案放在maven工程的resource 的html 檔案下面
package com.dl.utils; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class NewsNumber { //寫檔案 public String writeJsonfile(String content){ String pathorg = getClass().getClassLoader().getResource("/html/news.json").toString(); String path = pathorg.substring(6); System.out.println(path); try { FileUtils.writeStringToFile(new File(path), content, "UTF-8"); } catch (IOException e) { e.printStackTrace(); } return "success"; } //讀取檔案 public String readJsonFile(){ String pathorg = getClass().getClassLoader().getResource("/html/news.json").toString(); String path = pathorg.substring(6); String read = null; try { read = FileUtils.readFileToString(new File(path), "UTF-8"); } catch (IOException e) { e.printStackTrace(); } return read; } }