File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes How to Delete a Directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to Delete a Directory" Watch "How to Delete a Directory" New topic

How to Delete a Directory

Abdul Salam

Joined: Feb 25, 2002
Posts: 10
Is there any reliable way of deleting a directory
and it's contents from java (recursively).
I have tried

It also did not work.My OS is win 2000 server.
Thx in advance.

Knowledge Xchange. SCJ2P.
netharam ram
Ranch Hand

Joined: Aug 09, 2001
Posts: 202
I think u'll have to delete it thro' coding methods.U can write a method that accepts the directory path,using this delete the files of the current directory ,in case of sub-folders call this funtion recursively with the sub-folder path.This is the way I know.
Happy middling with java.
etrivelli nainar

Joined: Feb 14, 2002
Posts: 5
So, can we delete a file using java I/O?
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Netharam's right. You have to delete all the files in the directory before deleting the directory itself. The methods in the File class isDirectory(), listFiles(), and delete() should be all you need.
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
FAQ: How do I delete a directory that isn't empty?
I agree. Here's the link:
subject: How to Delete a Directory
It's not a secret anymore!