Java File Path Delete deleteDirectory(String sPath)

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

Description

delete Directory

License

Open Source License

Declaration

public static boolean deleteDirectory(String sPath) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {

    public static boolean deleteDirectory(String sPath) {
        boolean flag = false;
        if (!sPath.endsWith(File.separator)) {
            sPath = sPath + File.separator;
        }/*from   www.  j av a 2 s  .  co m*/
        File dirFile = new File(sPath);
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        flag = true;
        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                flag = files[i].delete();
                if (!flag)
                    break;
            } else {
                flag = deleteDirectory(files[i].getAbsolutePath());
                if (!flag)
                    break;
            }
        }
        if (!flag)
            return false;
        if (dirFile.delete()) {
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. deleteDirectory(String path)
  2. deleteDirectory(String path)
  3. deleteDirectory(String path)
  4. deleteDirectory(String path)
  5. deleteDirectory(String path)
  6. deleteDirectoryAndContents(final File srcPath)
  7. deleteDirectoryAndContentsRecursive(final File srcPath, final boolean deleteSrcDir)
  8. deleteDirectoryContents(File path)
  9. deleteDirectoryContents(String directoryPath, boolean deleteChildDirectories)