Java Delete Directory deleteDirectory(String folder)

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

Description

delete Directory

License

Open Source License

Declaration

public static boolean deleteDirectory(String folder) 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.File;

public class Main {
    public static boolean deleteDirectory(String folder) {
        String dir = folder;//  w  ww.  ja  v a2s . co m
        if (!dir.endsWith(File.separator)) {
            dir = dir + File.separator;
        }
        File dirFile = new File(dir);

        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        boolean flag = true;
        // delete all the file
        File[] files = dirFile.listFiles();
        for (File file : files) {
            // delete the file
            if (file.isFile()) {
                flag = deleteFile(file.getAbsolutePath());
                if (!flag) {
                    break;
                }
            }

            else {
                flag = deleteDirectory(file.getAbsolutePath());
                if (!flag) {
                    break;
                }
            }
        }

        if (!flag) {
            return false;
        }

        // delete the directory
        if (dirFile.delete()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.isFile() && file.exists()) {
            file.delete();
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. deleteDirectory(String directoryName)
  2. deleteDirectory(String dirName)
  3. deleteDirectory(String dirName)
  4. deleteDirectory(String dirname, boolean recursive)
  5. deleteDirectory(String fileName)
  6. deleteDirectory(String name)
  7. deleteDirectoryContent(final String strDir)
  8. deleteDirectoryContents(File directory)
  9. deleteDirectoryContents(File directory)