Java File Path Delete deleteDirectory(String dirPath)

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

Description

delete Directory

License

Open Source License

Declaration

public static void deleteDirectory(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 deleteDirectory(String dirPath) throws Exception {
        deleteDirectory(new File(dirPath));
    }//  ww  w.  j a va2  s.co  m

    public static void deleteDirectory(File dir) throws Exception {

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

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

    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 };
    }
}

Related

  1. deleteDirectory(final String path)
  2. deleteDirectory(String dir_path)
  3. deleteDirectory(String directoryPath)
  4. DeleteDirectory(String directoryPath)
  5. deleteDirectory(String dirPath)
  6. deleteDirectory(String filePath)
  7. deleteDirectory(String path)
  8. deleteDirectory(String path)
  9. deleteDirectory(String path)