How do I delete directory recursively?

Category: commons.io, viewed: 7887 time(s).

Using the FileUtils.deleteDirectory() method can help to simplify the process of deleting directory and everything below it recursively.

package org.kodejava.example.commons.io;

import org.apache.commons.io.FileUtils;

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

public class DeleteDirectory {
    public static void main(String[] args) {
        try {
            File directory = new File("/home/foobar/Temp/Data");

            //
            // Deletes a directory recursively. When deletion process is fail an
            // IOException is thrown and that's why we catch the exception.
            //
            FileUtils.deleteDirectory(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats