The code copy is as follows:
import java.io.*;
/**
* Created by tang on 14-3-1.
*/
public class JsonUtils {
//Read the Json file from the given location
public static String readJson(String path){
//Get file from the given location
File file = new File(path);
BufferedReader reader = null;
//Return value, use StringBuffer
StringBuffer data = new StringBuffer();
//
try {
reader = new BufferedReader(new FileReader(file));
//Every time the file is read cache
String temp = null;
while((temp = reader.readLine()) != null){
data.append(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//Close the file stream
if (reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data.toString();
}
//Give a path and Json file, store it to the hard disk
public static void writeJson(String path,Object json,String fileName){
BufferedWriter writer = null;
File file = new File(path + fileName + ".json");
//If the file does not exist, create a new one
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//Write
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("File writing was successfully!");
}
}