Java File Path Delete deleteDir(File f)

Here you can find the source of deleteDir(File f)

Description

delete Dir

License

Apache License

Declaration

public static boolean deleteDir(File f) 

Method Source Code


//package com.java2s;
/*/*from ww  w  . ja  v  a 2 s  .  co m*/
 Copyright (C) 2012 The Stanford MobiSocial Laboratory
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import java.io.*;

public class Main {
    public static boolean deleteDir(File f) {
        if (f.isDirectory()) {
            String[] children = f.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(f, children[i]));
                if (!success) {
                    System.err.println("warning: failed to delete file " + f);
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        return f.delete();
    }

    public static void deleteDir(String path) {
        if (path == null)
            return;
        File f = new File(path);
        if (f.exists()) {
            boolean success = deleteDir(f);
            warnIf(!success, "Unable to delete file: " + f.getPath());
        } else
            warnIf(true, "Sorry, can't delete path because it doesn't even exist: " + path);
    }

    public static void warnIf(boolean b, String message) {
        if (b) {
            System.err.println("REAL WARNING: " + message + "\n");
            // Thread.dumpStack();
            breakpoint();
        }
    }

    public static void breakpoint() {
        // permanent breakpoint
    }
}

Related

  1. deleteDir(File directory, boolean removeAll)
  2. deleteDir(File dirFile)
  3. deleteDir(File dirPath)
  4. deleteDir(File f)
  5. deleteDir(File f)
  6. deleteDir(File fDir)
  7. deleteDir(File file)
  8. deleteDir(File path)
  9. deleteDir(File path)