Java Force Delete forceDelete(File file)

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

Description

Deletes a file.

License

Open Source License

Parameter

Parameter Description
file file or directory to delete, must not be <code>null</code>

Exception

Parameter Description
NullPointerException if the directory is <code>null</code>
FileNotFoundException if the file was not found
IOException in case deletion is unsuccessful

Declaration

public static void forceDelete(File file) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//from   www.  j  a  v a2 s  .  c o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

public class Main {
    /**
     * Deletes a file. If file is a directory, delete it and all sub-directories.
     * <p>
     * The difference between File.delete() and this method are:
     * <ul>
     * <li>A directory to be deleted does not have to be empty.</li>
     * <li>You get exceptions when a file or directory cannot be deleted. (java.io.File methods returns a boolean)</li>
     * </ul>
     * 
     * @param file
     *            file or directory to delete, must not be <code>null</code>
     * @throws NullPointerException
     *             if the directory is <code>null</code>
     * @throws FileNotFoundException
     *             if the file was not found
     * @throws IOException
     *             in case deletion is unsuccessful
     */
    public static void forceDelete(File file) throws IOException {
        if (file.isDirectory()) {
            deleteDirectory(file);
        } else {
            boolean filePresent = file.exists();
            if (!file.delete()) {
                if (!filePresent) {
                    throw new FileNotFoundException("File does not exist: " + file);
                }
                String message = "Unable to delete file: " + file;
                throw new IOException(message);
            }
        }
    }

    /**
     * Deletes a directory recursively.
     * 
     * @param directory
     *            directory to delete
     * @throws IOException
     *             in case deletion is unsuccessful
     * @see FileUtils#recursiveDelete(File)
     */
    public static void deleteDirectory(File directory) throws IOException {
        if (!directory.exists()) {
            return;
        }
        if (!directory.delete()) {
            String message = "Unable to delete directory " + directory + ".";
            throw new IOException(message);
        }
    }
}

Related

  1. forceDelete(File f)
  2. forceDelete(File f)
  3. forceDelete(File file)
  4. forceDelete(File file)
  5. forceDelete(File file)
  6. forceDelete(File file)
  7. forceDelete(File file)
  8. forceDelete(File file)