method File.delete() fails!!!

Hello dear programmers!

I'm trying to delete certain files. I've built a class with members:
File myFile;
File myDirFile;
the c'tor allocates myDirFile and sets it to point to the my requested directory.

Now, check out the following method in which method File.delete() always returns false. Do u know why?

public void removeAllCookies()
{
// "filesToRemove" will hold a list of
// file names of the files to be removed       
String[] filesToRemove = myTestDir.list();
for (int i=0; i<filesToRemove.length; i++)
 {
myFile = new File(filesToRemove[i]);
 if (myFile.delete() == true)
    System.out.println("File: " +  
    filesToRemove[i] + " has been
    removed.");
 else
    System.out.println("File: " +  
    filesToRemove[i] + " has not been
    removed.");
 }
}

filesToRemove holds what it should - so that part is fine. Debugging shows that
myFile.delete() always returns false.

If u can help please write your answer simply and with no "hints" that may seem obvious to u, for I am new in Java
(I'm a student).
Otherwise - Thank u 4 your time.
itailevitanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andyhedgesCommented:
Do the files actuall get deleted?
0
sgomsCommented:
you need to form a new file object  mentioning the directory.

Example:

import java.io.*;

class RemoveFiles{
File myFile,myDirFile=new File("c:\\tmp");
public void removeAllCookies(){
  String[] filesToRemove = myDirFile.list();
  for (int i=0; i<filesToRemove.length; i++){  
        myFile = new File(myDirFile+"\\"+filesToRemove[i]);
   if (myFile.delete() == true)
      System.out.println("File: " +    
      filesToRemove[i] + " has been removed.");
   else
      System.out.println("File: " +  
      filesToRemove[i] + " has not been  removed.");
   }
 }
 public static void main(String args[]){
        RemoveFiles f=new RemoveFiles();
            f.removeAllCookies();
 }

}

-sgoms
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mbormannCommented:
sgoms,

It is better if u dont check if it's deleted or not,just delete it,thats been my experience.
Also sometimes files never get deleted ,I dunno why.
maybe it has got some lock on  it.

Always when u r deleting files from Java,if possible call System.gc() at end of ur code.

always keep all ur genarted files in a specific folder and try to delete those files everytime ur App is run.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

sgomsCommented:
mbormann,
point noted.i'll surelu\y keep this in mind nxt time i face a problem deleting files!
-sgoms
0
mbormannCommented:
sgoms,
The reason I said to call a System.gc() is that when ur App is highly loaded then those files remain deleted in Memory but not from Hard disk,i.e their indexes r deleted.
so to flush out this we call a gc()

Regards
0
sgomsCommented:
agreed
-sgoms
0
mbormannCommented:
sgoms,
dont be so seriuos,Enjoy life man
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.