Reputation: 9784
I attempt to lock a file on my device's "external" storage (not removable in my case) as it's being written. After I've obtained the lock, I am able to go into a file browser (in another app) and read or delete the file as it's being written. Is there no way to prevent this?
FileOutputStream fos = new FileOutputStream(fileNameOut);
lock = fos.getChannel().lock();
outputStream = new BufferedOutputStream(fos);
Upvotes: 1
Views: 317
Reputation: 718916
Is there no way to prevent this?
There is no way to prevent this ... short of rewriting the other application.
The Android platform has a Linux-based kernel, and on Linux / Unix systems file locking is discretionary. An application will only notice that a file is "locked" if it attempts to acquire a lock on the file for itself. They typically don't, and if they don't there is nothing you can do about it.
FileLock on Android doesn't do anything?
Not correct. It does something, but not what you expected.
Read the "Platform Dependencies" section of the javadoc.
Upvotes: 1