코드 사본은 다음과 같습니다.
java.io.*;
/**
* 14-3-1로 Tang에 의해 만들어졌습니다.
*/
공개 클래스 jsonutils {
// 주어진 위치에서 JSON 파일을 읽습니다
public static string readjson (문자열 경로) {
// 주어진 위치에서 파일을 가져옵니다
파일 = 새 파일 (Path);
bufferedReader reader = null;
// 값을 반환하고 StringBuffer를 사용하십시오
StringBuffer data = new StringBuffer ();
//
노력하다 {
reader = new bufferedReader (New Filereader (파일));
// 파일을 읽을 때마다 캐시가 표시 될 때마다
문자열 온도 = 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 ();
}
}
}
return data.toString ();
}
// 경로와 JSON 파일 제공, 하드 디스크에 저장하십시오.
public static void writejson (문자열 경로, 객체 json, 문자열 filename) {
완충 작가 작가 = null;
파일 = 새 파일 (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 ( "파일 쓰기가 성공적으로!");
}
}