Java File Path Delete delete(String path)

Here you can find the source of delete(String path)

Description

Method to delete a file or a directory and all it's contents if there are any.

License

Open Source License

Parameter

Parameter Description
path a file or directory to delete

Exception

Parameter Description
IOException an exception

Return

true if all contents under and including path are deleted

Declaration

public static boolean delete(String path) throws IOException 

Method Source Code


//package com.java2s;
/*//from w  w  w  . ja v a  2s.c om
 * DAITSS Copyright (C) 2007 University of Florida
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.io.IOException;

import java.io.File;

public class Main {
    /**
     * Method to delete a file or a directory and all it's contents if there are
     * any. Unlike runtime methods this method is platform independent.
     * 
     * @param path
     *            a file or directory to delete
     * @return true if all contents under and including <code>path</code> are
     *         deleted
     * @throws IOException
     */
    public static boolean delete(String path) throws IOException {
        // assume success
        boolean rc = true;

        File file = new File(path);
        if (!file.exists()) {
            // there is no such directory in the disk, throw exception
            throw new IOException("Unable to delete " + path + ". Path does not exist.");
        }

        if (file.isFile()) {
            // this is a file, so delete it directly
            if (file.delete()) {
                return true;
            }

            return false;
        }

        // delete the directory recursively
        String files[];
        files = file.list();
        int i;
        for (i = 0; i < files.length; i++) {
            File f = new File(path + File.separator + files[i]);
            if (f.isFile()) {
                // this is a file, delete it directly
                if (!f.delete())
                    rc = false;
            }
            if (f.isDirectory()) {
                // this is a directory, delete it recursively
                if (!delete(f.getAbsolutePath()))
                    rc = false;
            }

            // clean up
            f = null;
        }

        // now the directory should be empty, delete it directly
        if (!file.delete())
            rc = false;

        // clean up
        file = null;

        return rc;
    }
}

Related

  1. delete(String filePath)
  2. delete(String filePath)
  3. delete(String filePath)
  4. delete(String filePath, boolean recursive)
  5. delete(String path)
  6. delete(String path)
  7. delete(String path)
  8. deleteAll(File path)
  9. deleteAll(File path)