Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sujata Abc
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
import java.io.*;

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());
}
else
{
String[] file_list = f.list();
System.out.println(file_list.length);
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
thanks
 
Chandhrasekar Saravanan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic