Json讀寫本地文件實現(xiàn)代碼
import java.io.*;
/**
* Created by tang on 14-3-1.
*/
public class JsonUtils {
//從給定位置讀取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();
}
//給定路徑與Json文件,存儲到硬盤
public static void writeJson(String path,Object json,String fileName){
BufferedWriter writer = null;
File file = new File(path + fileName + ".json");
//如果文件不存在,則新建一個
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//寫入
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(json.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// System.out.println("文件寫入成功!");
}
}
相關(guān)文章
SpringBoot異常: nested exception is java.lang.NoClassDefFoundE
這篇文章主要介紹了SpringBoot異常: nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext解決方案,說明了錯誤原因和解決方案,需要的朋友可以參考下2021-06-06關(guān)于SpringCloud灰度發(fā)布的實現(xiàn)
這篇文章主要介紹了關(guān)于SpringCloud灰度發(fā)布的實現(xiàn),灰度發(fā)布又稱金絲雀發(fā)布,是在系統(tǒng)升級的時候能夠平滑過渡的一種發(fā)布方式,灰度發(fā)布可以保證整體系統(tǒng)的穩(wěn)定,在初始灰度的時候就可以發(fā)現(xiàn)、調(diào)整問題,以保證其影響度,需要的朋友可以參考下2023-08-08淺談springfox-swagger原理解析與使用過程中遇到的坑
本篇文章主要介紹了淺談springfox-swagger原理解析與使用過程中遇到的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02SpringBoot框架集成ElasticSearch實現(xiàn)過程示例詳解
這篇文章主要為大家介紹了SpringBoot如何集成ElasticSearch的實現(xiàn)過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11