导出JSON文件

编程入门 行业动态 更新时间:2024-10-28 20:26:07

导出JSON<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件"/>

导出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文件

本文发布于:2024-02-27 16:29:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1707372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!