wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes File Delete Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "File Delete Problem" Watch "File Delete Problem" New topic
Author

File Delete Problem

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

I am working on a project where my application monitors files/folders. The user can add/delete files/folders. Such files/folder can be remote shares too (NOT mapped drives)
Whenever a new file/folder is added my application also computes and maintains some data regarding these files.
Whenever files are deleted, my application needs to flush out the corresponding data. Unfortunately (for me) the flushing out is a non reversible operation.

Since I wanted the FS and my data to be in synch, I tried the following approach.
1) Check if my application has sufficient access permissions to delete the file/folder
2) If yes, flush out the data/delete file.

Since there is no File#canDelete() I used File#canWrite() (imagining both to effectively mean the same permissions).
Surprisingly, file.canWrite() returns true but file.delete() returns false!
Thus my data and the FS is out of synch.

Is there any way I can figure out if a particular file/folder can be deleted by my application?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

What does canWrite return for the parent folder? Usually, deleting a file is partly changing the parent folder.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

It returns true for the parent as well !
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Can you delete the file from Windows explorer manually?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Yes.

Now I have set the permissions giving everyone full access. (initially there were no permissions and canWrite() was supposed to return false.)
Now the files are getting deleted. But that was never the problem.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Ok. I tried this:
and it throws a SecurityException (inspite of the permissions)
Could this be because the delete code is invoked from a servlet?

If I try System.getSecurityManager it returns null!

Only if the file.canWrite() was working properly !
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Ok.
I got this working by calling the file.setLastModified(System.currentTimeMillis());
As expected this does NOT generate the exception so I am checking the return boolean and proceeding accordingly.

But I still cant figure out how canWrite() returns true !
 
 
subject: File Delete Problem