Java Force Delete forceDelete(final File file)

Here you can find the source of forceDelete(final File file)

Description

Deletes a file.

License

Apache License

Parameter

Parameter Description
file file or directory to delete, must not be null

Exception

Parameter Description
IOException in case deletion is unsuccessful

Declaration

public static void forceDelete(final File file) throws IOException 

Method Source Code

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

import java.io.File;

import java.io.IOException;

public class Main {
    /**// ww  w. ja  v  a2s .c  o m
     * Deletes a file. If file is a directory, delete it and all sub-directories.
     * 
     * @param file file or directory to delete, must not be null
     * @throws IOException in case deletion is unsuccessful
     */
    public static void forceDelete(final File file) throws IOException {
        if (file.exists()) {
            if (file.isDirectory()) {
                for (final File c : file.listFiles()) {
                    forceDelete(c);
                }
            }
            if (!file.delete()) {
                throw new IOException("Failed to delete " + file.getAbsolutePath());
            }
        }
    }
}

Related

  1. forceDelete(File file)
  2. forceDelete(File file)
  3. forceDelete(File file)
  4. forceDelete(File path)
  5. forceDelete(final File file)