Java Delete Directory deleteDirectory(File file)

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

Description

Deletes the directory if the directory has no files.

License

Open Source License

Parameter

Parameter Description
file The file to be deleted

Exception

Parameter Description
IOException Throws IOException if there isn't any directory

Declaration


public static void deleteDirectory(File file) throws IOException 

Method Source Code

//package com.java2s;

import java.io.File;
import java.io.IOException;

public class Main {
    /**/*from   www .  java2  s  . c om*/
     * 
     * Deletes the directory if the directory has no files. If the directory has files
     * it deletes all the files in the directory and then the directory gets delted
     * 
     * @param file
     * The file to be deleted
     * @throws IOException
     * Throws IOException if there isn't any directory
     */

    public static void deleteDirectory(File file) throws IOException {
        if (file.isDirectory()) {
            if (file.list().length == 0) {
                file.delete();
            } else {
                String[] files = file.list();
                for (String temp : files) {
                    File fileDelete = new File(file, temp);
                    deleteDirectory(fileDelete);
                }

                if (file.list().length == 0) {
                    file.delete();
                }
            }

        } else {
            file.delete();
        }
    }
}

Related

  1. deleteDirectory(File f)
  2. deleteDirectory(File f)
  3. deleteDirectory(File file)
  4. deleteDirectory(File file)
  5. deleteDirectory(File file)
  6. deleteDirectory(File file)
  7. deleteDirectory(File file)
  8. deleteDirectory(File file)
  9. deleteDirectory(File fileOrDir)