Here you can find the source of deleteDirectory(File path)
Parameter | Description |
---|---|
path | the path |
public static boolean deleteDirectory(File path)
//package com.java2s; /*/*from w w w . j a v a 2 s . c o m*/ * (c) 2012 Michael Schwartz e.U. * All Rights Reserved. * * This program is not a free software. The owner of the copyright * can license the software for you. You may not use this file except in * compliance with the License. In case of questions please * do not hesitate to contact us at idx@mschwartz.eu. * * Filename: FileUtil.java * Created: 30.05.2012 * * Author: $LastChangedBy$ * Date: $LastChangedDate$ * Revision: $LastChangedRevision$ */ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Main { /** * Deletes a directory and its files recursively. * * @param path * the path * @return true, if successful */ public static boolean deleteDirectory(File path) { File[] files = path.listFiles(); for (File file : files) { try { if (file.isDirectory()) { // System.out.println(" delete dir " + // file.getCanonicalPath()); boolean ok = deleteDirectory(file); if (!ok) { System.out.println(" ups, " + file.getCanonicalPath() + " cannot be deleted"); } } else { // System.out.println(" delete file " + // file.getCanonicalPath()); boolean ok = file.delete(); if (!ok) { System.out.println(" ups, " + file.getCanonicalPath() + " cannot be deleted"); } } } catch (IOException e) { e.printStackTrace(); } } return (path.delete()); } /** * List the elements of the directory and its sub-directories. * * Return a list of the files * */ public static List<File> listFiles(File directory) { List<File> l = new ArrayList<File>(); File listOfFiles[] = directory.listFiles(); if (listOfFiles != null) { for (File f : listOfFiles) { if (f.isDirectory()) { l.addAll(listFiles(f)); } else { l.add(f); } } } return l; } }