1. 程式人生 > >java對檔案操作,刪除檔案,強制刪除檔案

java對檔案操作,刪除檔案,強制刪除檔案

/** * 刪除資料夾(強制刪除) * * @param path */ public staticvoid deleteAllFilesOfDir(File path) {if (null != path) {if (!path.exists())return;if (path.isFile()) { boolean result = path.delete();int tryCount = 0;while (!result && tryCount++ < 10) {System.gc(); // 回收資源result = path.delete(); } }
File[] files = path.listFiles();if (null != files) {for (int i = 0; i < files.length; i++) { deleteAllFilesOfDir(files[i]); } } path.delete(); } } 刪除檔案 /** * 刪除檔案 * * @param pathname * @return * @throws IOException */ public static boolean deleteFile(String pathname){ boolean result = false; File file = new File(pathname); if (file.exists()) { file.delete(); result = true; System.out.println("檔案已經被成功刪除"); } return result; }