Here you can find the source of deleteDirectory(final String path)
Parameter | Description |
---|---|
path | - the full path |
Parameter | Description |
---|---|
IOException | an exception |
public static void deleteDirectory(final String path) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2015 CA. All rights reserved. * * This source file is licensed under the terms of the Eclipse Public License 1.0 * For the full text of the EPL please see https://www.eclipse.org/legal/epl-v10.html *******************************************************************************/ import java.io.File; import java.io.IOException; public class Main { /**//from ww w . j a va 2 s.co m * Deletes a file or directory * * @param path - the full path * @throws IOException */ public static void deleteDirectory(final String path) throws IOException { File file = new File(path); deleteDirectory(file); } /** * Deletes a directory. * * @param file - the file or directory * @throws IOException */ public static void deleteDirectory(File file) throws IOException { if (file.isDirectory()) { //list all the directory contents String[] files = file.list(); //directory is empty, then delete it if (files == null || files.length == 0) { deleteFileOrDirectory(file); } else { 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 files = file.list(); if (files == null || files.length == 0) { deleteFileOrDirectory(file); } } } else { //if file, then delete it deleteFileOrDirectory(file); } } /** * Delete a directory or file * * @param file - the file or directory * @throws IOException */ public static void deleteFileOrDirectory(File file) throws IOException { if (file.delete()) System.out.println("Deleted : " + file.getAbsolutePath()); else System.out.println("Failed to delete : " + file.getAbsolutePath()); } }