コードコピーは次のとおりです。
java.io.*をインポートします。
/**
* Tangによって14-3-1に作成されました。
*/
パブリッククラスjsonutils {
//特定の場所からJSONファイルを読み取ります
public static string readjson(string path){
//特定の場所からファイルを取得します
file file = new file(path);
bufferedreader reader = null;
//値を返し、stringbufferを使用します
stringbuffer data = new StringBuffer();
//
試す {
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();
} ついに {
//ファイルストリームを閉じます
if(reader!= null){
試す {
reader.close();
} catch(ioException e){
e.printstacktrace();
}
}
}
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()){
試す {
file.createNewFile();
} catch(ioException e){
e.printstacktrace();
}
}
//書く
試す {
writer = new BufferedWriter(new filewriter(file));
writer.write(json.tostring());
} catch(ioException e){
e.printstacktrace();
} ついに {
試す {
if(writer!= null){
writer.close();
}
} catch(ioException e){
e.printstacktrace();
}
}
// system.out.println( "ファイルライティングは正常に!");
}
}