Java Delete Directory deleteDirectory(File directory)

Here you can find the source of deleteDirectory(File directory)

Description

http://stackoverflow.com/questions/3775694/deleting-folder-from-java

License

Open Source License

Parameter

Parameter Description
directory the directory to delete.

Exception

Parameter Description
IOException if the file access fails.

Return

true if deletion was successful.

Declaration

public static boolean deleteDirectory(File directory) throws IOException 

Method Source Code

//package com.java2s;
/** The FileUtils class contains methods for file and directory handling,
which comprises reading a file, copying or deleting a directory
as well as things like that.//w ww . j av a2  s  . c  om
    
<pre>
This file is part of FidoCadJ.
    
FidoCadJ 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 3 of the License, or
(at your option) any later version.
    
FidoCadJ 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 FidoCadJ.  If not, see <http://www.gnu.org/licenses/>.
    
Copyright 2008-2015 by Davide Bucci
</pre>
    
*/

import java.io.*;

public class Main {
    /**
    http://stackoverflow.com/questions/3775694/deleting-folder-from-java
    @param directory the directory to delete.
    @return true if deletion was successful.
    @throws IOException if the file access fails.
    */
    public static boolean deleteDirectory(File directory) throws IOException {
        if (directory.exists()) {
            File[] files = directory.listFiles();
            if (null != files) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        deleteDirectory(files[i]);
                    } else {
                        if (!files[i].delete())
                            throw new IOException("Can not delete file" + files[i]);
                    }
                }
            }
        }
        return directory.delete();
    }
}

Related

  1. deleteDirectory(File directory)
  2. deleteDirectory(File directory)
  3. deleteDirectory(File directory)
  4. deleteDirectory(File directory)
  5. deleteDirectory(File directory)
  6. deleteDirectory(File directory)
  7. deleteDirectory(File directory)
  8. deleteDirectory(File directory)
  9. deleteDirectory(File directory, boolean ignoreErrors)