0

I wrote a java application that accesses a file while other Processes in other VMs try to do the same. Therefore I use the FileLock class:

FileOutputStream fos = new  FileOutputStream(filePath,append);
    FileChannel f = fos.getChannel();
    FileLock lock;

    while ((lock = f.tryLock()) == null){
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    OutputStreamWriter out = new OutputStreamWriter( new FileOutputStream(filePath,append));
    out.write(textToWrite);
    out.close();
    lock.release();

All works fine on Mac OSX, but when I run the code on Windows 7 it throws an IOException at the line

out.close();

, when trying to flush.

java.io.IOException: The process cannot access the file because another process has locked a portion of the file
at java.io.FileOutputStream.writeBytes(Native Method)

As far as I understand from How does FileLock work?, the actual obtaining of the lock with

f.tryLock()

forbids me to access it since another process (this one apparently) has exclusive lock.

Now that strikes me as a paradoxon - how am I to obtain an exlusive lock to enable me to write to the file without danger of other processes messing with it at the same time when the actual act of obtaining the lock hinders me to do so?

And consequently why does it work on Mac OS and not on windows? I know from the JavaDocs that there are OS specific differences and difficulties with the FileLock class, but surely not with respect to its designed-for functionality. Since this can't be the case, I am doing something wrong and this is where I ask for your help.

Thx, M

Community
  • 1
  • 1
Mathew
  • 307
  • 1
  • 11

1 Answers1

0

There is no file locking on UNIX.: http://www.coderanch.com/t/551144/java/java/File-lock-doesn-prevent-threads. In fact, on UNIX, you can delete a file from under a process and it may not even notice...
So you need to use a lock file that you can check exists.
Paradoxically your code is working on Windows but not on UNIX (i.e. Mac OS), the exception should be the expected result of trying to write to a file that is locked by another process.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Thx for your reply. I am not sure if I fully understand you and the coderanch post, though. Do you mean by "there is no file locking" that UNIX does not provide the functionality and therefore I have to provide it using the FileLock class? And, following your argument, windows provides the functionality of file locking and I therefore need not use FileLock? If I understand you, then it even goes a step further and trying to use FIleLock on Windows throws the exception? – Mathew Feb 20 '13 at 11:21
  • The `FileLock` class is very OS specific - on UNIX it does absolutely nothing; which, I admin, is somewhat useless. On Windows it has it's own problems as in the post you mention. In short it's a bit of a dodgy one. So I would recommend using a lock file - i.e. write something to somewhere and the use that as an external mutex. – Boris the Spider Feb 20 '13 at 11:32
  • Do you have more evidence for this than just that article? Forgive the ad hominem, but I don't consider coderanch.com especially reliable, as I've seen considerable amounts of misinformation there. If the file locking methods aren't in fact calling a Unix system's underlying `flock()` or `lockf()` function or some equivalent, why are there bugs like [Java bug 4877242](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4877242) ("File locks are per-thread on Linux")? – VGR Feb 20 '13 at 11:44
  • There is quite alot of information but this on the internet. Something on [Ubuntu forums](http://ubuntuforums.org/archive/index.php/t-204467.html), [another article](http://asktej.wordpress.com/2012/06/23/does-java-filelock-work-in-linux/) with test cases you can try and a bug report on [Oracle](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4024206) - that one's ancient but explains the underlying problem. Hope that helps. – Boris the Spider Feb 20 '13 at 12:52