java向指定文件继续写内容_java 向指定文件写入内容(如文件存在,则先删除再创建;写入如目录不存在,则创建)...
/**
* 向指定文件寫入內容(如文件存在,則先刪除再創建寫入)
*
* @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);
}
}
}
}
總結
以上是生活随笔為你收集整理的java向指定文件继续写内容_java 向指定文件写入内容(如文件存在,则先删除再创建;写入如目录不存在,则创建)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国ddos攻击事件(2007年ddos
- 下一篇: JAVA中使用bos做视频上传_JAVA