Java Delete Directory deleteDirectory(String directoryName)

Here you can find the source of deleteDirectory(String directoryName)

Description

Deletes the directory (and any files) represented by directoryName, if directory doesn't exits does nothing.

License

LGPL

Parameter

Parameter Description
directoryName the string representing the directory to be deleted.

Declaration

public static void deleteDirectory(String directoryName) 

Method Source Code

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

import java.io.File;

public class Main {
    /**/*  www . j av  a2 s .co m*/
     * Deletes the directory (and any files) represented by <tt>directoryName</tt>, if directory
     * doesn't exits does nothing.
     * @param directoryName the string representing the directory to be deleted.
     */
    public static void deleteDirectory(String directoryName) {
        File directory = new File(directoryName);
        if (directory.exists()) {
            if (directory.isDirectory()) {
                File[] files = directory.listFiles();
                if (files != null) {
                    for (File file : files) {
                        if (file.isDirectory())
                            deleteDirectory(file.getAbsolutePath());
                        file.delete();
                    }
                }
            }
            directory.delete();
        }

    }

    /**
     * Given a string representing a file determines whether the file exists.
     * @param filename the string representing the path of the file.
     * @return tru if the file exists, false otherwise.
     */
    public static boolean exists(String filename) {
        File f = new File(filename);
        return f.exists();
    }
}

Related

  1. deleteDirectory(IProgressMonitor monitor, File directory, File base, int step)
  2. deleteDirectory(String dir)
  3. deleteDirectory(String directory)
  4. deleteDirectory(String directory)
  5. deleteDirectory(String directoryName)
  6. deleteDirectory(String dirName)
  7. deleteDirectory(String dirName)
  8. deleteDirectory(String dirname, boolean recursive)
  9. deleteDirectory(String fileName)