Java File Delete delete(File f)

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

Description

Deletes the file/directory recursively and quietly.

License

Open Source License

Parameter

Parameter Description
f the file/directory to delete.

Declaration

public static boolean delete(File f) 

Method Source Code

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

import java.io.File;

public class Main {
    /**//from   w  w  w .j  ava 2 s.co  m
     * Deletes the file/directory recursively and quietly. It does not throw exceptions if the deletion fails. Instead,
     * it returns false.
     * 
     * @param f
     *            the file/directory to delete.
     * @return
     */
    public static boolean delete(File f) {

        if (f == null) {
            return false;
        }

        // if the file is a directory, delete the contents first.
        if (f.exists()) {
            if (f.isDirectory()) {
                for (File ff : f.listFiles()) {
                    if (ff.isDirectory()) {
                        delete(ff);
                    } else {
                        try {
                            ff.delete();
                        } catch (Exception e) {
                            return false;
                        }
                    }
                }
            }
        }

        // delete the file
        try {
            return f.delete();
        } catch (Exception e) {
            return false;
        }

    }
}

Related

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