• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deleteing a file

 
hard coder
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am doing this:
File f = new File("whatever/whatever/whatever.whatever");
f.delete();
But the file is only deleted sometimes ( once in 5 times)
Also I run it in a loop over several files at once. Then only some files get deleted actually (and i have noticed that the bigger ones are left behind)
I even tried
f = null
after the delete immediately... just in case
BUT IT DOES NOT WORK.
Please help.
This is on a windows 98 system
thanx in advance.
 
raghavender rao
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ther,
the problem cd be might be some other resources are using ur file....once when it releases the resources it wil delete else it wont....

if this is not the problem let me kn the complete details
Rao
 
hard coder
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
thanks for replying.
I have tried to nullify all references to the File and also immediately nullify the reference on which i invoke delete(), right after the call. but the file does n't get deleted.
I tried to catch SEcurityException but that is not thrown by delete. However, delete returns a false - indicating that the file could not be deleted.
Than I found that there was no security manager installed - so i installed a new SecurityManager() using System.setSecurityManager(new SecurityMAnager()) - now I get a FilePermission Exception that says I don't have write permission in the first place - (sorry - actually i first copy a file from source to destination and then delete the original - a move operation).
So now I am trying to get the Security manager to use a File Permission of all permissions granted - but I can't find out to use a FilePermission object with the Security Manager or simply how to use it.
Any more hints?
 
raghavender rao
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ther

add this code to ***java.policy*** file....(dont forget to specify the path) the file will be in the /jre/lib/security/java.policy
///////////////////////////////////////////
grant {
permission java.io.FilePermission "/", "read";

permission java.io.FilePermission "/tmp", "read";
permission java.io.FilePermission "/tmp", "write";

permission java.io.FilePermission "/home/", "read";

permission java.io.FilePermission "/home/dave/", "read";
permission java.io.FilePermission "/home/dave/", "write";

permission java.io.FilePermission "/home/dave/-", "read";
permission java.io.FilePermission "/home/dave/-", "write";

permission java.io.FilePermission "/home/dave/bio/maxd/maxdView/rmiDemo/-", "read";
permission java.io.FilePermission "/home/dave/bio/maxd/maxdView/-", "read, write";
};

///////////////////////////////////////////
Rao
 
raghavender rao
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u can even try this code i wd suggest tht u go through this link
http://java.sun.com/j2se/1.3/api/java/io/FilePermission.html
import java.io.*;

class FilepermissionDel {

public static void main(String args[]){

try {


FilePermission fp = new FilePermission("/space/*","delete");



File f = new File("/space/testing.html");

f.delete();


}catch(Exception e){

e.printStackTrace();


}


}



}

Rao
 
hard coder
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I Solved the problem.
This is actually a bug that was fixed by Sun ie JDK 1.1 I think.
The scene is that you can call delete on the File straightaway, but that does not delete the file.
So do file = file.getCanonicalFile() and then file.delete();
This DOES delete the file!!!
Ok then. Hope that helps some desperate souls out there...
Mother, Did it have to be so high...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!