Java File Delete nio delete(File file)

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

Description

Deletes a file recursively.

License

Open Source License

Parameter

Parameter Description
file The file to be deleted.

Exception

Parameter Description
IOException If an I/O error occurs

Declaration

public static void delete(File file) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.IOException;

import java.nio.file.Files;

public class Main {
    /**//from   www  . ja  v  a 2  s  . co  m
     * Deletes a file recursively. If <code>file</code> is a non empty directory
     * the directorys content will be deleted recursively.
     *
     * @param file The file to be deleted.
     * @throws IOException If an I/O error occurs
     */
    public static void delete(File file) throws IOException {
        if (file.isDirectory()) {
            File[] children = file.listFiles();
            if (children == null) {
                String msg = "Could not access content of " + file.getAbsolutePath();
                throw new IOException(msg);
            }
            for (File f : children)
                delete(f);
        }
        Files.delete(file.toPath());
    }
}

Related

  1. delete(File f)
  2. delete(File file)
  3. delete(File file)
  4. delete(File file)
  5. delete(File file)