Here you can find the source of deleteDir(File dir)
Parameter | Description |
---|---|
dir | the directory to delete. |
private static boolean deleteDir(File dir)
//package com.java2s; /**//from w ww . j a v a 2s. c o m * * This file is part of Stuffed. * * Stuffed is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Stuffed is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Stuffed. If not, see <http://www.gnu.org/licenses/>. * * File name: Common.java * Package: cs.man.ac.uk.common * Created: May 1, 2013 * Author: Rob Lyon * * Contact: rob@scienceguyrob.com or robert.lyon@postgrad.manchester.ac.uk * Web: <http://www.scienceguyrob.com> or <http://www.cs.manchester.ac.uk> * or <http://www.jb.man.ac.uk> */ import java.io.File; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /** * Deletes all files and sub directories under a directory. * Returns true if all deletions were successful. This method is * only called within the dirDelete(String path) method, and * should only be called from there. * * If a deletion fails, the method stops attempting to delete and returns false. * @param dir the directory to delete. * @return true if the directory was deleted, else false. */ private static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // The directory is now empty so delete it return dir.delete(); } /** * Checks that the object at the specified path is a directory. * @param path the path to check. * @return true if the object is a directory. */ public static boolean isDirectory(String path) { if (isPathValid(path)) { File dir = new File(path); if (dir.isDirectory()) { return true; } else { return false; } } else return false; } /** * Checks that the object at the specified path is a directory. * @param dir the directory to check. * @return true if the object is a directory. */ public static boolean isDirectory(File dir) { if (dir.isDirectory()) { return true; } else { return false; } } /** * Tests whether a path is valid. * @param path the path to check. * @return true if the file name is valid, else false. */ public static boolean isPathValid(String path) { File f; if (path != null) f = new File(path); else return false; try { @SuppressWarnings("unused") String canonicalPath = f.getCanonicalPath(); // Check for invalid characters if (isWindows()) { return isValidWinPath(path); } else if (isMac()) { return isValidUnixPath(path); } else if (isUnix()) { return isValidUnixPath(path); } else return true; } catch (IOException e) { return false; } } /** * Tests to see if the application is running on windows. * @return true if the application is running under windows, else false. */ public static boolean isWindows() { String os = System.getProperty("os.name").toLowerCase(); //windows return (os.indexOf("win") >= 0); } /** * Tests to see if a path is valid for the Windows OS. * @param path the path to test. * @return true if a valid windows path, else false. */ public static boolean isValidWinPath(String path) { // If user inputs only a drive letter. if (path.length() == 3) { return false; } else if (path.endsWith("\\")) { return false; } else { String regex = "([a-z]:\\\\(?:[-\\w\\.\\d]+\\\\)*(?:[-\\w\\.\\d]+)?)"; Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL); Matcher m = p.matcher(path); // Invalid path characters to explicitly check for. String[] invalidChars = { "\"", "/", "*", "?", "<", ">", "|" }; for (int i = 0; i < invalidChars.length; i++) { if (path.contains(invalidChars[i])) return false; } // Check the only colon appears at start of path, i.e. C:\ if (path.indexOf(":") > 1) return false; return (m.find()); } } /** * Tests to see if the application is running on MacIntosh. * @return true if the application is running under MacIntosh, else false. */ public static boolean isMac() { String os = System.getProperty("os.name").toLowerCase(); return (os.indexOf("mac") >= 0); } /** * UNTESTED. * Tests to see if a path is valid for the Unix/Linux OS. * @param path the path to test. * @return true if a valid unix path, else false. */ public static boolean isValidUnixPath(String path) { String regex = "((?:\\/[\\w\\.\\-]+)+)"; Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL); Matcher m = p.matcher(path); return m.find(); } /** * Tests to see if the application is running on Linux/Unix. * @return true if the application is running under Linux/Unix, else false. */ public static boolean isUnix() { String os = System.getProperty("os.name").toLowerCase(); return (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0); } }