Java File Delete nio deleteFileCascade(String directory)

Here you can find the source of deleteFileCascade(String directory)

Description

Delete a folder with all the sub files.

License

LGPL

Parameter

Parameter Description
directory a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void deleteFileCascade(String directory) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
    /**/*from ww  w  . ja v a  2s.  co m*/
     * Delete a folder with all the sub files.
     * @param directory
     * @throws IOException
     */
    public static void deleteFileCascade(String directory) throws IOException {
        deleteFileCascade(new File(directory));
    }

    /**
     * Delete a folder with all the sub files.
     * @param directory
     * @throws IOException
     */
    public static void deleteFileCascade(File directory) throws IOException {

        // delete all the sub files recursively
        if (directory.isDirectory()) {

            File[] files = directory.listFiles();

            // some JVM return null for listfiles
            if (files == null)
                return;

            for (File file : files) {
                file.setWritable(true);
                deleteFileCascade(file);
            }
        }

        Files.delete(Paths.get(directory.getAbsolutePath()));

        // delete the directory
        //if ( !directory.delete() )
        //   throw new FileNotFoundException( "Failed to delete file: " + directory );
    }
}

Related

  1. deleteEntriesFromZip(File zipFile, List entriesToDelete)
  2. deleteFile(File f)
  3. deleteFile(File f)
  4. deleteFile(String fileName)
  5. deleteFile(String filename)
  6. deleteFileRescursive(File file)
  7. deleteFiles(File directory, String affix)
  8. deleteIfEmpty(final File directory)
  9. deleteLocalFile(final String fileName)