Java Directory Delete nio deleteDir(String dirName)

Here you can find the source of deleteDir(String dirName)

Description

delete Dir

License

Open Source License

Declaration

public static void deleteDir(String dirName) 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 {
    public static void deleteDir(String dirName) throws IOException {
        deleteDir(new File(dirName));
    }// ww w.  ja  v  a  2  s  . c  o m

    public static void deleteDir(File dir) throws IOException {
        if (!dir.exists())
            return;

        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                deleteDir(new File(dir, children[i]));
            }
        }

        // The directory is now empty so delete it
        // NB The Files class was introduced in Java 7, this won't compile in earlier Java versions
        Files.delete(dir.toPath());
    }
}

Related

  1. deleteDir(File dir)
  2. deleteDir(File file)
  3. deleteDir(final File targetDir)
  4. deleteDir(String path)
  5. deleteDirectory(File dir)
  6. deleteDirectory(File dir)
  7. deleteDirectory(File directory)