Java File Delete delete(File f)

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

Description

Delete a file or directory

License

Apache License

Parameter

Parameter Description
f The file or directory to be deleted

Exception

Parameter Description
RuntimeException if the file or directory could not be deleted

Declaration

public static void delete(File f) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

public class Main {
    /**//from  w w w . jav  a 2  s. co  m
     * Delete a file or directory
     * @param f The file or directory to be deleted
     * @throws RuntimeException if the file or directory could not be deleted
     */
    public static void delete(File f) {
        delete_(f, false);
    }

    private static boolean delete_(File f, boolean silent) {
        boolean ok = true;
        if (f.exists()) {
            if (f.isDirectory()) {
                for (File c : f.listFiles()) {
                    ok = ok && delete_(c, silent);
                }
            }
            try {
                boolean deleted = f.delete();
                ok = ok && deleted;
                if (!deleted && !silent) {
                    throw new RuntimeException("Failed to delete file or directory: " + f.getPath());
                }
            } catch (Exception ex) {
                ok = false;
                if (!silent) {
                    throw new RuntimeException("Failed to delete file or directory: " + f.getPath(), ex);
                }
            }
        }
        return ok;
    }
}

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, boolean recursive)
  9. delete(File file)