Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Files

Status
Not open for further replies.

SSJ

Programmer
Sep 26, 2002
54
PT
I'm having problems trying to delete 2 files, since when I try to delete the 2nd the first one isn't yet deleted and so the other delete will fail. I'm using:

Newfile.delete(); //Delete the temporary file
boolean jj = tmpfile.delete();

jj returns false everytime, so I need to wait for the NewFile be actually deleted b4 deleting the other one. I was thinking on something like:

Newfile.delete(); //Delete the temporary file
while (Newfile.exists()) { }
boolean jj = tmpfile.delete();

Any idea on how should I guarantee that the 2nd delete will only be ran after the 1st one was actually done?
 


From what you say, I take it that in some part of your code, fileOne is created, and then at some point later deleted. If when you delete that fileOne, you set a boolean to that action...

and then you start a Thread within the code, which monitors when that file is deleted :


File fileOne = new File("c:\wherever\fileOne.txt");
//... some other code / methods
boolean bFileOne = fileOne.delete();

//... some other code /methods

File fileTwo = new File("c:\wherever\fileTwo.txt");

//... some other code /methods

Thread thread = new Thread(new Runnable() {
public void run() {
try {
while (!bFileOne) {
;//do nothing
}


fileTwo.delete();

}
catch ( IOException e ) {
System.out.println(e.toString());
}
}
});



This thread will then sit and wait for the first file to be deleted by whatever code section/method
 
ups this was a mistake of mine... I had another thread that was still using the file when I was trying to delete it that's why I couldn't delete it, but since no exception was raised I didn't even think about it. thanks for the reply anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top