Java File Delete delete(File file)

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

Description

Recursively deletes the given File file .

License

Open Source License

Parameter

Parameter Description
file the file to delete

Return

true iff the file was successfully deleted, false otherwise

Declaration

public static boolean delete(File file) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /**//w w  w.  ja  v a2s  . co m
     * Recursively deletes the given {@link File} {@code file}. If the {@code file} is a directory, this method deletes
     * all sub-files of that directory by calling itself on the sub-file. Otherwise, it simply deletes the file using
     * {@link File#delete()}.
     *
     * @param file
     *    the file to delete
     *
     * @return {@code true} iff the file was successfully deleted, {@code false} otherwise
     */
    public static boolean delete(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    delete(f);
                }
            }
        }
        return file.delete();
    }

    /**
     * Recursively deletes the given {@link File} {@code file}. If the {@code file} is a directory, this method deletes
     * all sub-files of that directory by calling itself on the sub-file. Otherwise, it simply deletes the file using
     * {@link File#delete()}. The given {@code maxDepth} is used to limit the recursive process to a maximum directory
     * depth. If it set to {@code 0}, it is ignored whether or not the given {@code file} is a directory.
     *
     * @param file
     *    the file to delete
     * @param maxDepth
     *    the maximum recursion depth
     *
     * @return {@code true} iff the file was successfully deleted, {@code false} otherwise
     */
    public static boolean delete(File file, int maxDepth) {
        if (maxDepth > 0 && file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    delete(f, maxDepth - 1);
                }
            }
        }
        return file.delete();
    }
}

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)