• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Need urgent help with File.Delete

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) {

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

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

=== the output ======
about to delete
it is a file
Unable to delete
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
A timing clock, fuse wire, high explosives and a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic