java 读写json文件

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;
		}
}

猜你喜欢

转载自blog.csdn.net/ppwwp/article/details/83067313