Java Path Delete nio delete(Path root)

Here you can find the source of delete(Path root)

Description

Deletes the file, if it is a directory, deletes its content recursively.

License

Open Source License

Parameter

Parameter Description
root path to the file.

Exception

Parameter Description
IOException thrown on filesystem error.

Declaration

public static void delete(Path root) throws IOException 

Method Source Code


//package com.java2s;
// License as published by the Free Software Foundation; either

import java.io.IOException;

import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    /**/*ww  w  .  j  av a  2  s .com*/
     * Deletes the file, if it is a directory, deletes its content recursively.
     *
     * @param root
     *        path to the file.
     * @throws IOException
     *         thrown on filesystem error.
     */
    public static void delete(Path root) throws IOException {
        if (Files.isDirectory(root)) {
            final DirectoryStream<Path> subPaths = Files.newDirectoryStream(root);
            for (Path path : subPaths) {
                delete(path);
            }
            subPaths.close();
            Files.delete(root);
        } else {
            Files.delete(root);
        }
    }
}

Related

  1. delete(final Path path)
  2. delete(final String[] paths)
  3. delete(Path path)
  4. delete(Path path)
  5. delete(Path path)
  6. delete(Path targetPath)
  7. delete(Path... paths)
  8. delete(String filePath, String fileName)
  9. delete(String targetFilePath)