Here you can find the source of deleteDirectory(File file)
Parameter | Description |
---|---|
file | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void deleteDirectory(File file) throws IOException, Exception
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.IOException; public class Main { /**/*from w ww. j a va 2s .com*/ * Delete a Directory. * * @param file * @throws IOException */ public static void deleteDirectory(File file) throws IOException, Exception { if (file.isDirectory()) { //directory is empty, then delete it if (file.list().length == 0) { file.setWritable(true); boolean b = file.delete(); if (!b) throw new IOException( "Directory cannot be deleted : " + file.getAbsolutePath() + "\nCheck permissions."); } else { //list all the directory contents String files[] = file.list(); for (String temp : files) { //construct the file structure File fileDelete = new File(file, temp); //recursive delete deleteDirectory(fileDelete); } //check the directory again, if empty then delete it if (file.list().length == 0) { file.setWritable(true); boolean b = file.delete(); if (!b) throw new IOException( "Directory cannot be deleted : " + file.getAbsolutePath() + "\nCheck permissions."); } } } else { //if file, then delete it file.setWritable(true); boolean b = file.delete(); if (!b) throw new IOException( "File cannot be deleted : " + file.getAbsolutePath() + "\nCheck permissions."); } } }