文件读取与保存
Long startTime=System.currentTimeMillis();
String jsonStr = "";
try {
File jsonFile = new File("E:\\json\\"+ "json" + ".json");
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
StringBuffer sb = new StringBuffer();
BufferedReader buf = new BufferedReader(reader);
String line=null;
while((line = buf.readLine())!=null)
{
sb.append(line);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
Long endTime = System.currentTimeMillis();
System.out.println("程序运行时间:" + (endTime - startTime) + "ms");
} catch (IOException e) {
e.printStackTrace();
}
List<FbAttachment> fbAttachmentList=fbAttachmentService.getFbList();
String json= JSON.toJSONString(fbAttachmentList);
BufferedWriter writer = null;
File file = new File("E:\\json\\"+ "json" + ".json");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true), "UTF-8"));
writer.write(json);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("文件写入成功!");
}