Java Directory Clear cleanDirectory(String dirPath)

Here you can find the source of cleanDirectory(String dirPath)

Description

clean Directory

License

Open Source License

Declaration

public static void cleanDirectory(String dirPath) throws Exception 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.io.*;

public class Main {
    public static void cleanDirectory(String dirPath) throws Exception {
        cleanDirectory(new File(dirPath));
    }//  www .j a  v  a  2  s.co m

    public static void cleanDirectory(File dir) throws Exception {
        if (!dir.exists())
            return;
        if (!dir.isDirectory())
            throw new Exception("The path '" + dir.getPath() + "' is not a directory. ");

        File[] fs = dir.listFiles();
        for (File f : fs) {
            if (f.isDirectory())
                deleteDirectory(f.getPath());
            else
                f.delete();
        }
    }

    /**
     * list all files of a given folder
     *
     * @param dirPath a given folder
     * @return file list
     */
    public static File[] listFiles(String dirPath) {
        File dir = new File(dirPath);
        if (dir.isDirectory())
            return dir.listFiles();
        else
            return new File[] { dir };
    }

    public static void deleteDirectory(String dirPath) throws Exception {
        deleteDirectory(new File(dirPath));
    }

    public static void deleteDirectory(File dir) throws Exception {

        cleanDirectory(dir);
        dir.delete();
    }
}

Related

  1. cleanDirectory(final File directory)
  2. cleanDirectory(final File directory)
  3. cleanDirectory(IProgressMonitor monitor, File directory)
  4. cleanDirectory(IProgressMonitor monitor, File directory, File base, int step)
  5. cleanDirectory(String dir)
  6. clearDirctory(File dir)
  7. clearDirectory(File dir)
  8. clearDirectory(File dir)
  9. clearDirectory(File dir, boolean doTree)