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)文章
Java畢業(yè)設(shè)計實戰(zhàn)之校園一卡通系統(tǒng)的實現(xiàn)
這是一個使用了java+Springboot+Maven+mybatis+Vue+mysql+wd開發(fā)的校園一卡通系統(tǒng),是一個畢業(yè)設(shè)計的實戰(zhàn)練習(xí),具有校園一卡通系統(tǒng)該有的所有功能,感興趣的朋友快來看看吧2022-01-01
mybatis plus saveOrUpdate實現(xiàn)有重復(fù)數(shù)據(jù)就更新,否則新增方式
這篇文章主要介紹了mybatis plus saveOrUpdate實現(xiàn)有重復(fù)數(shù)據(jù)就更新,否則新增方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
SpringBoot集成itext實現(xiàn)html轉(zhuǎn)PDF
iText是著名的開放源碼的站點sourceforge一個項目,是用于生成PDF文檔的一個java類庫,本文主要介紹了如何利用itext實現(xiàn)html轉(zhuǎn)PDF,需要的可以參考下2024-03-03
java基礎(chǔ)知識之FileInputStream流的使用
這篇文章主要介紹了java基礎(chǔ)知識之FileInputStream流的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
java-RGB調(diào)色面板的實現(xiàn)(事件監(jiān)聽器之匿名內(nèi)部類)
這篇文章主要介紹了java-RGB調(diào)色面板的實現(xiàn)(事件監(jiān)聽器之匿名內(nèi)部類),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
SpringBoot集成阿里巴巴Druid監(jiān)控的示例代碼
這篇文章主要介紹了SpringBoot集成阿里巴巴Druid監(jiān)控的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

