Java File Delete delete(File file)

Here you can find the source of delete(File file)

Description

Delete a File.

License

fedora commons license

Parameter

Parameter Description
file the File to delete.

Return

true if the operation was successful; false otherwise (which includes a null input).

Declaration

public static boolean delete(File file) 

Method Source Code


//package com.java2s;
/* The contents of this file are subject to the license and copyright terms
 * detailed in the license directory at the root of the source tree (also
 * available online at http://fedora-commons.org/license/).
 *//*from  ww  w .j  av a 2s.  c  o  m*/

import java.io.File;

public class Main {
    /**
     * Delete a File.
     *
     * @param file
     *        the File to delete.
     * @return <code>true</code> if the operation was successful;
     *         <code>false</code> otherwise (which includes a null input).
     */
    public static boolean delete(File file) {
        boolean result = true;

        if (file == null) {
            return false;
        }
        if (file.exists()) {
            if (file.isDirectory()) {
                // 1. delete content of directory:
                File[] children = file.listFiles();
                for (File child : children) { //for each file:
                    result = result && delete(child);
                } //next file
            }
            result = result && file.delete();
        } //else: input directory does not exist or is not a directory
        return result;
    }

    /**
     * Delete the specified file or directory.
     *
     * @param file
     *        File or directory to delete
     * @return <code>true</code> if the operation was successful;
     *         <code>false</code> otherwise (which includes a null input).
     */
    public static boolean delete(String file) {
        return delete(new File(file));
    }
}

Related

  1. delete(File file)
  2. delete(File file)
  3. delete(File file)
  4. delete(File file)
  5. delete(File file)
  6. delete(File file)
  7. delete(File file)
  8. delete(File file)
  9. delete(File file)