Java Folder Delete nio deleteFolder(Path folder)

Here you can find the source of deleteFolder(Path folder)

Description

delete Folder

License

Apache License

Declaration

public static void deleteFolder(Path folder) 

Method Source Code

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

import java.io.File;
import java.nio.file.Path;

public class Main {
    public static void deleteFolder(Path folder) {
        if (folder.toFile().exists() && folder.toFile().isDirectory()) {
            System.out.println("deleting " + folder.toAbsolutePath());
            deleteRecursive(folder.toFile());
        }/*from   w  w  w  .  j ava2s  .com*/
    }

    static void deleteRecursive(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    deleteRecursive(f);
                }
                if (!file.delete()) {
                    System.out.println("Could not delete folder " + file);
                }
            }
        } else {
            if (!file.delete()) {
                System.out.println("Could not delete file " + file);
            }
        }
    }
}

Related

  1. delete(Path folder, List noDeleteFiles)
  2. deleteEmptyFolder(String... dirNames)
  3. deleteFile(String folderName, String fileName)
  4. deleteFolder(Path path)
  5. deleteFolderAndSubfolders(Path pathToFile)
  6. deleteRecursively(File folder)