Json文件读写工具类
at 3周前 ca java by Achin
/** * Json文件读写工具类 */ @Component public class FileWritereadUtil { //写 public static void Writer(String filePath, JSONObject jsonObject){ //生成文件 FileUtil.writeUtf8String(jsonObject.toString(), filePath); } //读 public static JSONObject Writer(String filePath){ JSONObject js = null; try { //读取文件 String jsonStr = FileUtil.readUtf8String(filePath); js = new JSONObject(jsonStr); return js; } catch (Exception e) { System.out.println(e.getMessage()); return null; } } }
写
public static void main(String[] args) { JSONObject jsonObject = new JSONObject(); //相对路径 String resourcePath = System.getProperty("user.dir") + "/src/main/resources/configJson/"; // resourcePath 相对路径 // admin.json 文件名称与格式 String filePath = resourcePath + "admin.json"; jsonObject.put("key1","你在看我的博客吗"); jsonObject.put("key2","我的博客地址是:achinblog.cn"); Writer(filePath,jsonObject); }
结果
读
public static void main(String[] args) { //相对路径 String resourcePath = System.getProperty("user.dir") + "/src/main/resources/configJson/"; // resourcePath 相对路径 // admin.json 文件名称与格式 String filePath = resourcePath + "admin.json"; System.out.println(Writer(filePath));; }
结果
版权声明
1、本BLOG介绍。此BLOG为个人BLOG,内容均来自原创及互连网转载。最终目的为记录自己需要的内容或自己的学习感悟,不涉及商业用途。
2、本BLOG有关原创文章的版权本BLOG上原创文章未经本人许可,不得用于商业用途及传统媒体。网络媒体转载请注明出处,否则属于侵权行为。
3、本BLOG有关本站侵权本BLOG所转载的内容,均是本人未发现有对文章版权声明的文章且无来自传统媒体。如果本BLOG侵犯了您的权益,请联系本人,本人将会在第一时间删除侵权文章!
4、本站地址:achinblog.cn 联系方式:001@achinblog.cn
已有0条评论