how can we delete a directory in java???

Reply

Join Date: May 2008
Posts: 45
Reputation: alpe gulay is an unknown quantity at this point 
Solved Threads: 0
alpe gulay's Avatar
alpe gulay alpe gulay is offline Offline
Light Poster

how can we delete a directory in java???

 
0
  #1
May 6th, 2008
.,'is any knows the code to delete a directory in java...
help me pls...

YOUR ANSWER IS HIGHLY APPRECIATED!..
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

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

 
0
  #2
May 6th, 2008
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
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #3
May 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1
Reputation: gagansnt is an unknown quantity at this point 
Solved Threads: 0
gagansnt gagansnt is offline Offline
Newbie Poster

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

 
0
  #4
May 6th, 2008
Originally Posted by alpe gulay View Post
.,'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();
}
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 45
Reputation: alpe gulay is an unknown quantity at this point 
Solved Threads: 0
alpe gulay's Avatar
alpe gulay alpe gulay is offline Offline
Light Poster

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

 
0
  #5
May 6th, 2008
.,thank you so much...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 45
Reputation: alpe gulay is an unknown quantity at this point 
Solved Threads: 0
alpe gulay's Avatar
alpe gulay alpe gulay is offline Offline
Light Poster

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

 
0
  #6
May 6th, 2008
actually i am still beginners in java...that's why!!!!
thanx a lot!!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC