直接上代码:
/** * 递归删除 文件/文件夹 * * @param file */ public static void deleteFile(File file) { System.out.println("delete file path=" + file.getAbsolutePath()); if (!file.exists()) { System.out.println("delete file no exists " + file.getAbsolutePath()); return; } if (file.isFile()) { file.delete(); } else if (file.isDirectory()) { File files[] = file.listFiles(); for (int i = 0; i < files.length; i++) { deleteFile(files[i]); } } file.delete(); System.out.println("删除完毕!!"); }
调用:
public static void main(String[] args) { deleteFile(new File("D:\KuGou")); }