Java tutorial
//package com.java2s; /** * Copyright 2005-2010 Noelios Technologies. * * The contents of this file are subject to the terms of one of the following * open source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL 1.0 (the * "Licenses"). You can select the license that you prefer but you may not use * this file except in compliance with one of these Licenses. * * You can obtain a copy of the LGPL 3.0 license at * http://www.opensource.org/licenses/lgpl-3.0.html * * You can obtain a copy of the LGPL 2.1 license at * http://www.opensource.org/licenses/lgpl-2.1.php * * You can obtain a copy of the CDDL 1.0 license at * http://www.opensource.org/licenses/cddl1.php * * You can obtain a copy of the EPL 1.0 license at * http://www.opensource.org/licenses/eclipse-1.0.php * * See the Licenses for the specific language governing permissions and * limitations under the Licenses. * * Alternatively, you can obtain a royalty free commercial license with less * limitations, transferable or non-transferable, directly at * http://www.noelios.com/products/restlet-engine * * Restlet is a registered trademark of Noelios Technologies. */ public class Main { /** * Deletes an individual file or an empty directory. * * @param file * The individual file or directory to delete. * @return True if the deletion was successful. */ public static boolean delete(java.io.File file) { return delete(file, false); } /** * Deletes an individual file or a directory. A recursive deletion can be * forced as well. Under Windows operating systems, the garbage collector * will be invoked once before attempting to delete in order to prevent * locking issues. * * @param file * The individual file or directory to delete. * @param recursive * Indicates if directory with content should be deleted * recursively as well. * @return True if the deletion was successful or if the file or directory * didn't exist. */ public static boolean delete(java.io.File file, boolean recursive) { String osName = System.getProperty("os.name").toLowerCase(); return delete(file, recursive, osName.startsWith("windows")); } /** * Deletes an individual file or a directory. A recursive deletion can be * forced as well. The garbage collector can be run once before attempting * to delete, to workaround lock issues under Windows operating systems. * * @param file * The individual file or directory to delete. * @param recursive * Indicates if directory with content should be deleted * recursively as well. * @param garbageCollect * Indicates if the garbage collector should be run. * @return True if the deletion was successful or if the file or directory * didn't exist. */ public static boolean delete(java.io.File file, boolean recursive, boolean garbageCollect) { boolean result = true; boolean runGC = garbageCollect; if (file.exists()) { if (file.isDirectory()) { java.io.File[] entries = file.listFiles(); // Check if the directory is empty if (entries.length > 0) { if (recursive) { for (int i = 0; result && (i < entries.length); i++) { if (runGC) { System.gc(); runGC = false; } result = delete(entries[i], true, false); } } else { result = false; } } } if (runGC) { System.gc(); runGC = false; } result = result && file.delete(); } return result; } }