Java Delete Tree deltree(String dir)

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

Description

deltree

License

Open Source License

Declaration

public static boolean deltree(String dir) 

Method Source Code

//package com.java2s;
/*//from  w  w w .  j  ava  2  s . co  m
 * Copyright (C) ${year} Omry Yadan <${email}>
 * All rights reserved.
 *
 * See https://github.com/omry/banana/blob/master/BSD-LICENSE for licensing information
 */

import java.io.*;

public class Main {
    public static boolean deltree(String dir) {
        File f = new File(dir);
        if (f.isDirectory()) {
            File[] files = f.listFiles();
            for (File ff : files)
                deltree(ff.getAbsolutePath());
        }

        return f.delete();
    }

    public static boolean deltree(File root, String dir) {
        boolean b = deltree(new File(root, dir).getAbsolutePath());
        while (true) {
            if (dir != null && !dir.equals("") && !dir.equals(File.separator) && dir.length() > 0) {
                File f = new File(root, dir);
                if (f.isDirectory() && f.exists()) {
                    File[] files = f.listFiles();
                    if (files.length == 0) // empty dir.
                        f.delete();
                    else
                        break;
                }

                dir = new File(dir).getParent();
            } else {
                break;
            }
        }
        return b;
    }
}

Related

  1. delTree(File file)
  2. delTree(File file)
  3. delTree(File root)
  4. deltree(File root)
  5. deltree(final String directory)
  6. deltree(String dir)
  7. deltree(String directory)
  8. delTree(String dirName, String excludedDirName)