// 清空指定文件夾,採用遞歸方法!(會刪除該目錄下全部文件和子文件夾,但該根文件夾會保留!)
public void deleteFolder(String dir) {
File delfolder = new File(dir);
File oldFile[] = delfolder.listFiles();
try {
for (int i = 0; i < oldFile.length; i++) {
if (oldFile[i].isDirectory()) {
deleteFolder(dir + File.separator+oldFile[i].getName()); // 遞歸清空子文件夾
}
oldFile[i].delete();
}
} catch (Exception e) {
System.out.println("清空文件夾操做出錯!");
e.printStackTrace();
}
}遞歸