Java File Path Delete deleteDir(File dir)

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

Description

Helper function to (recursively) delete a directory and all its contents

License

Open Source License

Parameter

Parameter Description
dir - path to directory to delete

Return

- boolean True if directory no longer exists

Declaration

static boolean deleteDir(File dir) 

Method Source Code


//package com.java2s;
/*-//  w  w  w.ja  v  a 2 s. c  o m
 * Copyright 2012 Diamond Light Source Ltd.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.File;

public class Main {
    /**
     * Helper function to (recursively) delete a directory and all its contents
     * 
     * @param dir
     *            - path to directory to delete
     * @return - boolean True if directory no longer exists
     */
    static boolean deleteDir(File dir) {
        if (!dir.exists()) {
            return true;
        }
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (String element : children) {
                boolean success = deleteDir(new File(dir, element));
                if (!success) {
                    System.out.println("1. deleteDir could not delete: " + new File(dir, element));
                    return false;
                }
            }
        }
        if (dir.delete()) {
            return true;
        }
        System.out.println("2. deleteDir could not delete: " + dir
                + ". Make sure each test creates a uniquely named folder. Otherwise stale NFS locks may prevent folder deletion.");
        return false;
    }
}

Related

  1. deleteDir(File dir)
  2. deleteDir(File dir)
  3. deleteDir(File dir)
  4. deleteDir(File dir)
  5. deleteDir(File dir)
  6. deleteDir(File dir)
  7. deleteDir(File dir)
  8. deleteDir(File dir)
  9. deleteDir(File dir, boolean deleteRoot)