aspose file tools
The moose likes Beginning Java and the fly likes How to delete subfolder and its content in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to delete subfolder and its content in Java" Watch "How to delete subfolder and its content in Java" New topic
Author

How to delete subfolder and its content in Java

udhayan kumar
Greenhorn

Joined: Jan 28, 2010
Posts: 16
I want to delete subfolders and its contents from a folder using Java.

For example:
Folder
|__Subfolder1 - a.txt
|__Subfolder2 - b.txt
|__Subfolder3 - c.txt

I need to delete content of subfolders and its contents using Java. But Folder(Parent) should not be deleted.
Please help me.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

Class java.io.File has a delete() method which you can use to delete files and directories.

You'll have to first delete all files in the directory, and then the directory itself.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
udhayan kumar
Greenhorn

Joined: Jan 28, 2010
Posts: 16
Thanks for the reply Jesper. I am using following snippet to delete contents of the subfolders, but I don't know how to delete the subfolder(directory) itself. I am new to Java, please help me. Thanks in advance.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

Just call directory.delete(); at the end of the method, between lines 13 and 14.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to delete subfolder and its content in Java
 
Similar Threads
Delete folder on window close
How to create Java document from java files?
How to find a file (any file) in Java
Differentiate between file and a folder
How how compile source files of .jar File?