Here you can find the source of deleteEmptyDirectoryRecursive(File directory)
Parameter | Description |
---|---|
directory | The start directory |
Parameter | Description |
---|---|
IOException | In case of an I/O error |
public static void deleteEmptyDirectoryRecursive(File directory) throws IOException
//package com.java2s; /**/*from ww w . j a v a2s .c o m*/ * Copyright 2011-2016 Three Crickets LLC. * <p> * The contents of this file are subject to the terms of the LGPL version 3.0: * http://www.gnu.org/copyleft/lesser.html * <p> * Alternatively, you can obtain a royalty free commercial license with less * limitations, transferable or non-transferable, directly from Three Crickets * at http://threecrickets.com/ */ import java.io.File; import java.io.IOException; public class Main { /** * Deletes an empty directory, including all parent directories that are * also empty, stopping at the first non-empty parent. * * @param directory * The start directory * @throws IOException * In case of an I/O error */ public static void deleteEmptyDirectoryRecursive(File directory) throws IOException { if ((directory != null) && directory.isDirectory()) { File[] files = directory.listFiles(); if ((files == null) || (files.length == 0)) { if (!directory.delete()) throw new IOException("Could not delete empty directory: " + directory); deleteEmptyDirectoryRecursive(directory.getParentFile()); } } } }