写入文件
public static void writeFile(String filePath, String str) {
FileOutputStream fos = null;
OutputStreamWriter os = null;
File f= null;
try {
File file = new File(filePath);
f = file.getParentFile();
if (null != f&& !f.exists()) {
boolean mkdirFlag = f.mkdirs();
if (!mkdirFlag) {
logger.error("mkdirs f failed!");
}
}
// 如果该文件已经存在,则删除重新生成
if (file.exists()) {
boolean deleteFlag = file.delete();
if (!deleteFlag) {
logger.error("delete file failed!");
}
}
fos = new FileOutputStream(file);
os = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
os.write(str);
os.flush();
} catch (IOException e) {
logger.error("writeFile failed.", e);
} finally {
closeStream(osw);
closeStream(fos);
}
}