Java Directory Delete emptyDirectory(File path)

Here you can find the source of emptyDirectory(File path)

Description

empty Directory

License

Open Source License

Declaration

public static boolean emptyDirectory(File path) 

Method Source Code


//package com.java2s;
/*/*from w w  w  . j a  va 2 s.c om*/
 * Copyright: (c) 2004-2010 Mayo Foundation for Medical Education and 
 * Research (MFMER). All rights reserved. MAYO, MAYO CLINIC, and the
 * triple-shield Mayo logo are trademarks and service marks of MFMER.
 *
 * Except as contained in the copyright notice above, or as used to identify 
 * MFMER as the author of this software, the trade names, trademarks, service
 * marks, or product names of the copyright holder shall not be used in
 * advertising, promotion or otherwise in connection with this software without
 * prior written authorization of the copyright holder.
 * 
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 *       http://www.eclipse.org/legal/epl-v10.html
 * 
 */

import java.io.File;

public class Main {
    public static boolean emptyDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    if (!files[i].delete()) {
                        return false;
                    }
                }
            }
        }
        //if it doesn't exists, consider the delete a success.
        return true;
    }

    /**
     * Delete directory.
     * 
     * @param path the path
     * 
     * @return true, if successful
     */
    public static boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    if (!files[i].delete()) {
                        return false;
                    }
                }
            }
            return path.delete();
        }
        //if it doesn't exists, consider the delete a success.
        return true;
    }
}

Related

  1. emptyDirectory(File dir)
  2. emptyDirectory(File dir)
  3. emptyDirectory(File dir)
  4. emptyDirectory(File directory)
  5. emptyDirectory(File directory)
  6. emptyDirectory(final File directory)