Java Directory Delete emptyDir(File dir)

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

Description

empty Dir

License

Apache License

Declaration

protected static void emptyDir(File dir) throws IOException 

Method Source Code

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

import java.io.File;

import java.io.IOException;

public class Main {

    public static void emptyDir(String path) throws IOException {
        File dir = new File(path);
        emptyDir(dir);//from ww w  . ja v a2s.  com
    }

    protected static void emptyDir(File dir) throws IOException {

        if (!(dir.exists() && dir.canWrite())) {
            throw new IOException(
                    "Kann Verzeichnis nicht leeren, es ist nicht vorhanden " + "oder nicht beschreibbar");
        }

        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                File current = new File(dir, children[i]);
                if (current.isDirectory()) {
                    deleteDirectoryRecursivly(current);
                }
                if (current.isFile()) {
                    current.delete();
                }
            }
        } else {
            throw new IOException("Kann Datei nicht leeren");
        }
    }

    public static void deleteDirectoryRecursivly(String path) throws IOException {
        File file = new File(path);
        deleteDirectoryRecursivly(file);
    }

    public static void deleteDirectoryRecursivly(File dir) throws IOException {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                deleteDirectoryRecursivly(new File(dir, children[i]));
            }
        }
        dir.delete();
    }
}

Related

  1. emptyDir(File dir)
  2. emptyDir(File dir)
  3. emptyDir(File dir)
  4. emptyDir(File dir)
  5. emptyDir(File directory)