943,714 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 13845
  • Java RSS
May 6th, 2008
0

how can we delete a directory in java???

Expand Post »
.,'is any knows the code to delete a directory in java...
help me pls...

YOUR ANSWER IS HIGHLY APPRECIATED!..
Similar Threads
Reputation Points: 9
Solved Threads: 1
Light Poster
alpe gulay is offline Offline
45 posts
since May 2008
May 6th, 2008
0

Re: how can we delete a directory in java???

Hi alpe gulay,

It seems from your questions that you are a bit of a beginner at Java. Take a look at the post at the top of this forum. It contains several great links that might be able to help you in your journey. The website java.sun.com is particularly helpful, with tutorials, examples and the API documentation as well as many other Java resources.

In answer to this specific question, there are probably many ways to do what you want. Off the top of my head I would suggest taking a look at the java.io.File.deleteOnExit() method for a simple way of deleting any file (and therefore directory) when the virtual machine terminates.

Hope this helps,
darkagn
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
May 6th, 2008
0

Re: how can we delete a directory in java???

A directory is deleted just like any file - with File.delete(). A directory must be empty to be deleted, so if it contains files you must recurse the directory and delete all files and subdirectories first.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,759 posts
since May 2007
May 6th, 2008
0

Re: how can we delete a directory in java???

Click to Expand / Collapse  Quote originally posted by alpe gulay ...
.,'is any knows the code to delete a directory in java...
help me pls...

YOUR ANSWER IS HIGHLY APPRECIATED!..
There is no API in java that deletes a folder directly. You first have to recursively delete all the contents of the folder and then only you can delete folder.

Like this
// Deletes all files and subdirectories under dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}

// The directory is now empty so delete it
return dir.delete();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gagansnt is offline Offline
1 posts
since May 2008
May 6th, 2008
0

Re: how can we delete a directory in java???

.,thank you so much...
Reputation Points: 9
Solved Threads: 1
Light Poster
alpe gulay is offline Offline
45 posts
since May 2008
May 6th, 2008
0

Re: how can we delete a directory in java???

actually i am still beginners in java...that's why!!!!
thanx a lot!!!
Reputation Points: 9
Solved Threads: 1
Light Poster
alpe gulay is offline Offline
45 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Netbeans and query
Next Thread in Java Forum Timeline: How can I delete empty lines from file?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC