Java Delete File or Directory deleteFileOrDir(File filehandle)

Here you can find the source of deleteFileOrDir(File filehandle)

Description

Returns true if all deletions were successful.

License

Open Source License

Parameter

Parameter Description
filehandle a parameter

Return

true if all deletions were successful

Declaration

public static boolean deleteFileOrDir(File filehandle) 

Method Source Code

//package com.java2s;
/*/*from w  w w .  j a  v a  2  s .c  o  m*/
 * Stage - Spatial Toolbox And Geoscript Environment 
 * (C) HydroloGIS - www.hydrologis.com 
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * (http://www.eclipse.org/legal/epl-v10.html).
 */

import java.io.File;

public class Main {
    /**
     * Returns true if all deletions were successful. If a deletion fails, the method stops
     * attempting to delete and returns false.
     * 
     * @param filehandle
     * @return true if all deletions were successful
     */
    public static boolean deleteFileOrDir(File filehandle) {

        if (filehandle.isDirectory()) {
            String[] children = filehandle.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteFileOrDir(new File(filehandle, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        boolean isdel = filehandle.delete();
        if (!isdel) {
            // if it didn't work, which often happens on windows systems,
            // remove on exit
            filehandle.deleteOnExit();
        }

        return isdel;
    }
}

Related

  1. deleteFileOrDir(File dir, int max)
  2. deleteFileOrDir(File fd)
  3. deleteFileOrDir(File file)
  4. deleteFileOrDir(File file)
  5. deleteFileOrDir(File instanceFileOrDir)
  6. deleteFileOrDirectory(File dir)
  7. deleteFileOrDirectory(File file)
  8. deleteFileOrDirectory(File fileOrDirectory)