Java Folder Delete clearDir(File dir)

Here you can find the source of clearDir(File dir)

Description

clear Dir

License

Apache License

Declaration

public static boolean clearDir(File dir) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

public class Main {

    public static boolean clearDir(File dir) {
        if (!dir.isDirectory())
            return false;
        File[] files = dir.listFiles();
        if (files == null || files.length == 0)
            return true;
        boolean cleared = true;
        for (File sub : files) {
            cleared &= delete(sub);/*from w w w.ja  v  a 2 s.  co m*/
        }
        return cleared;
    }

    public static boolean isDirectory(String fileName) {
        File testFile = new File(fileName);

        if ((testFile.exists()) && (testFile.isDirectory())) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean delete(File f) {
        if (f.isFile())
            return f.delete();
        else if (f.isDirectory()) {
            boolean b = clearDir(f);
            b &= f.delete();
            return b;
        } else
            return false;
    }

    public static boolean isFile(String fileName) {
        File testFile = new File(fileName);

        if ((testFile.exists()) && (testFile.isFile())) {
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. clearDir(File d)
  2. clearDir(File dir)
  3. clearDir(File dir)
  4. clearDir(File dir)
  5. clearFiles(File dir)
  6. clearFilesOnPath(String path)
  7. clearFolder(File f)
  8. clearFolder(File[] children)