文件"/>
导出JSON文件
/**
* 生成.json格式文件
*
* @param jsonString
* @param filePath
* @param fileName 不包含扩展名
* @return
*/
public static boolean createJsonFile(String jsonString, String filePath, String fileName) {
// 标记文件生成是否成功
boolean flag = true;
// 拼接文件完整路径
String fullPath = filePath + File.separator + fileName + “.json”;
Writer write = null;
// 生成json格式文件
try {
// 保证创建一个新文件
File file = new File(fullPath);
// 如果父目录不存在,创建父目录
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
// 将格式化后的字符串写入文件
write = new OutputStreamWriter(new FileOutputStream(file), “UTF-8”);
write.write(jsonString);
write.flush();
write.close();
} catch (Exception e) {
flag = false;
e.printStackTrace();
} finally {
if (write != null) {
try {
write.close();
} catch (Exception e1) {
}
}
}
// 返回是否成功的标记
return flag;
}
更多推荐
导出JSON文件
发布评论