Here you can find the source of deleteRecursive(File fileOrDirectory)
Parameter | Description |
---|---|
fileOrDirectory | the file or directory to cancel |
true
if specified File was deleted and false
otherwise
public static boolean deleteRecursive(File fileOrDirectory)
//package com.java2s; import java.io.File; public class Main { /**/*from w w w . j a v a2 s. c om*/ * Remove specified file or directory. * * @param fileOrDirectory * the file or directory to cancel * @return <code>true</code> if specified File was deleted and <code>false</code> otherwise */ public static boolean deleteRecursive(File fileOrDirectory) { if (fileOrDirectory.isDirectory()) { File[] list = fileOrDirectory.listFiles(); if (list == null) { return false; } for (File f : list) { if (!deleteRecursive(f)) { return false; } } } return !fileOrDirectory.exists() || fileOrDirectory.delete(); } /** * Remove specified file or directory. * * @param fileOrDirectory * the file or directory to cancel * @param followLinks * are symbolic links followed or not? * @return <code>true</code> if specified File was deleted and <code>false</code> otherwise */ public static boolean deleteRecursive(File fileOrDirectory, boolean followLinks) { if (fileOrDirectory.isDirectory()) { // If fileOrDirectory represents a symbolic link to a folder, // do not read a target folder content. Just remove this symbolic link. if (!followLinks && java.nio.file.Files.isSymbolicLink(fileOrDirectory.toPath())) { return !fileOrDirectory.exists() || fileOrDirectory.delete(); } File[] list = fileOrDirectory.listFiles(); if (list == null) { return false; } for (File f : list) { if (!deleteRecursive(f, followLinks)) { return false; } } } return !fileOrDirectory.exists() || fileOrDirectory.delete(); } }