File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Delete() odes not always delete file!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Delete() odes not always delete file!!!!" Watch "Delete() odes not always delete file!!!!" New topic

Delete() odes not always delete file!!!!

Sujata Abc

Joined: Dec 27, 2000
Posts: 1
hi everyone,
i have a piece of code which i run through visualage. if i run it separately as a class , it deltes the files as wanted. but if i create an instance of that class and then call the fn which does besides other things deletes the files, it does not delete all the files. here is the code

public class Deletion {
public boolean deleteAll(String fileName)
boolean status = false;

File f = new File(fileName);
if (f.list() == null)
System.out.println("can u delete file f " + f.canWrite());
status = f.delete();
System.out.println("1st status is " + status + " file name is " + f.getName());
String[] file_list = f.list();
for (int i = 0; i < file_list.length; i++)
File f1 = new File(file_list[i]);
System.out.println(f.getPath() + "\\" + f1.getPath());
deleteAll(f.getPath() + "\\" + f1.getPath());
status = f.delete();
System.out.println("2nd status is " + status);
return status;
}// end of fn deleteAll
public static void main(String[] args)
Deletion d = new Deletion();
d.deleteAll("CFS Valid ValuesEJBDeployed");

}///end of class
in my other class , say class1 , i create an instance of class Deletion in one of its methods and then call the deleteAll() .but it deletes only some of the files in a directory. but if i run the Deletion class it deletes everthing.
i would highly appreciate if someone could help me out
Chandhrasekar Saravanan
Ranch Hand

Joined: Jun 16, 2002
Posts: 57
I do face the same problem, could anyone suggest why we are not able to delete the files when we call an instance of a class
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671

Double check the canonical path; it's easy to accidentally refer to a different directory than the one you think you're dealing with. If the file is where you think it should be and you have the appropriate permissions (double check these on the OS as well) then the most likely reasons why you can't delete are:
(1) Your Java program may have an open stream / channel / RandomAccessFile referring to this file. Make sure that any of these things are definitely closed prior to attempting to delete. Often you can use a finally clause to ensure this.
(2) Some other process (outside your JVM) is using the file, and the OS won't let you have it. It's up to you to figure out what this is. Try going to the directory the file's in and deleting it manually; you may get a message explaining the problem.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Delete() odes not always delete file!!!!
It's not a secret anymore!