/**
* 向指定文件写入内容(如文件存在,则先删除再创建写入)
*
* @param content 保存内容
* @param path 文件完整路径
* @throws IOException
*/
public static void save(String content, String path) throws IOException {
FileWriter writer = null;
try {
File file = new File(path);
if (file.exists()) {
file.delete();
}
//目录不存在 则创建
if (!file.getParentFile().exists()) {
boolean mkdir = file.getParentFile().mkdirs();
if (!mkdir) {
throw new RuntimeException("创建目标文件所在目录失败!");
}
}
file.createNewFile();
writer = new FileWriter(file);
if (null != content) {
writer.write(content);
}
writer.flush();
} catch (IOException e) {
logger.error("文件写入异常", e);
throw e;
} finally {
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
logger.error("文件写入时流关闭异常", e);
}
}
}
}