FileLock: how to delete a locked file?

Y

yay_frogs

I'm writing a method that needs to:

1. Open a local file as a FileInputStream.
2. Lock the local file.
3. Upload the local file to a remote location.
4. Delete the local file.
5. Release the lock.

Now it seems it is impossible to do this with the existing java.nio.*
FileChannel and FileLock classes. There is no way to delete a locked
file. Once the locked file is uploaded to the server, I can't afford to
release the lock and let some other process accidentally try to access
the local file before my process deletes it. What can I do?
 
J

josh.s17

Why not have a seperate lock file to the file you are uploading. Then
you can delete the uploaded file and then release your lock.
 
Y

yay_frogs

Why not have a seperate lock file to the file you are uploading. Then
you can delete the uploaded file and then release your lock.

Other programs won't care about my lock file or honor it.
 
T

Tim Smith

Now it seems it is impossible to do this with the existing java.nio.*
FileChannel and FileLock classes. There is no way to delete a locked file.
Once the locked file is uploaded to the server, I can't afford to release
the lock and let some other process accidentally try to access the local
file before my process deletes it. What can I do?

Well, if you are willing to give up some portability, you could always step
outside of Java to delete the file. For example, if you are on a Unix
system, you could spawn a processes to do "rm yourfile". That should work,
as Unix allows deleting opened files, and a lock should have no effect on
that (as deleting isn't an operation on the file--it is just removing a
directory link to the file).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top