//将数据转为json文件
ReportDto reportDto = new ReportDto();
reportDto.setDatas(planDtoList);
String jsonInfo = JSON.toJSONString(reportDto);
String textName = parkCode + "_YJYA_" + parse;
dataReportUtil.isFilePath("D:/downloadResult/YJYA"); //判断目录是否存在并清空文件夹
dataReportUtil.jsonExport("D:/downloadResult/YJYA", jsonInfo, textName); //将文件转成json 保存到本地
// 方法
public static void jsonExport(String filePath, String jsonString, String fileName) {
String fullPath = filePath + '/' + fileName + ".json";
//例如:fullPath="D:/temp/test.json"
// 生成json格式文件
try {
// 保证创建一个新文件
File file = new File(fullPath);
if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录
file.getParentFile().mkdirs();
}
if (file.exists()) { // 如果已存在,删除旧文件
file.delete();
}
file.createNewFile();
// 将格式化后的字符串写入文件
Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
write.write(jsonString);
write.flush();
write.close();
} catch (Exception e) {
e.printStackTrace();
}
}