• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need urgent help with File.Delete

 
Muhammad Asif
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!
Can ne body help me with thsi problem.
Below is the functions that is gonna copy a file from source to destination and then delte the source file.
Basically a move process!
Can ne body tell me why the source file is not being deleted .
The file is copied sucessfully but the deltion process is not working.
need urgent help!
Thanx in advance
Muhammad Asif Abbasi
public void plzDelete() throws Exception {
File sourceFileName = new File("c://", "zz2client.properties");
File destFileName = new File("c://dst//", "zz2client.properties");

//get an input stream from source filename and
//get an outputstream to destination
FileInputStream fis = null;
FileOutputStream fos = null;
fis = new FileInputStream(sourceFileName);
fos = new FileOutputStream(destFileName);
byte byteRead = -1;
while ((byteRead = (byte) fis.read()) != -1) {
fos.write(byteRead);
}

System.out.println("about to delete ");
boolean deletionSucessful= false;
if (sourceFileName.isFile()){
System.out.println(" it is a file");
deletionSucessful = sourceFileName.delete();
}else{
System.out.println(" it is not a file");
}

if (deletionSucessful){
System.out.println("Sucessfully deleted file ");
}else{
System.out.println("Unable to delete");
}

}
=== the output ======
about to delete
it is a file
Unable to delete
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They don't really document the reasons why a delete might fail, but the main one I've found is - if anyone else is currently accessing the file. In this case, the most likely cause is your fis object, which is still open and ready to read from the file (even though it's at the end of the file). Add an fis.close() right after you're done copying. In general it's a good idea to always close any stream immediately after you're done with it - otherwise you don't know when it will be closed. (Garbage collection will probably take care of it eventually, but you don't know when.)
[This message has been edited by Jim Yingst (edited August 09, 2001).]
 
Muhammad Asif
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Jim!
U figured out the right problem!
IF their are ne live references to a file object u cannot delete the file associated with it!
Thanx for taking time to answer


Muhammad Asif Abbasi
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!