Java Directory Clear clearDirectory(String dir)

Here you can find the source of clearDirectory(String dir)

Description

clear Directory

License

Open Source License

Declaration

public static void clearDirectory(String dir) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.IOException;

public class Main {
    public static void clearDirectory(String dir) throws IOException {
        File f = new File(dir);
        if (!f.exists()) {
            throw new IOException("Directory not present or not writable");
        }//  w ww . j a  va2  s . c  o  m
        if (f.isDirectory()) {
            deleteFolder(f);
        } else {
            throw new IOException("Is not a directory");
        }
    }

    public static void deleteFolder(File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFolder(f);
                } else {
                    f.delete();
                }
            }
        }
        folder.delete();
    }
}

Related

  1. clearDirectory(File dir, boolean doTree)
  2. clearDirectory(File f)
  3. clearDirectory(File f)
  4. clearDirectory(File src)
  5. clearDirectory(String absolutePath)
  6. clearDirectory(String path)