1

Possible Duplicate:
Problem with Java file locking mechanism (FileLock etc)

In below code, I wanna test the FileLock class.

import java.io.File;
import java.nio.channels.*;
import java.nio.MappedByteBuffer;
import java.io.RandomAccessFile;

class test{
    File f= new File("./in.txt");
    RandomAccessFile in = new RandomAccessFile(f, "rw");
    FileChannel fc = in.getChannel();

    byte[] t = new byte[20];
    in.read(t, 0, 8);
    System.out.println(new String(t));

    FileLock fl = fc.tryLock(0, 4, false);
    if(fl!=null){
        System.out.println("the file has been locked");
        Thread.sleep(10000);
        fl.release();
        System.out.println("no lock!");
    }
    fc.close();
    in.close();
    }
};

But the problem is that when I run "java test" twice, the first one should lock the file in.txt and the second one should not have access to in.txt, then should not print anything in screen. But the fact is the second one also print the string on the screen, even though it returns as expected. And the region from 0 to 3rd byte should not be read by others. But in fact it is not.

But there seems something wrong. If the file is locked exclusively, it cannot be accessible to others. But it seems not.

Community
  • 1
  • 1
city
  • 2,036
  • 2
  • 32
  • 40
  • Well, I'm getting this `java.io.IOException: The process cannot access the file because another process has locked a portion of the file` so it prevents the reading here. – atomman Jan 10 '13 at 19:53
  • really? How do you run it? I open two terminals and run it respectively under Ubuntu – city Jan 10 '13 at 20:39
  • It works for everybody else. Are you running it from the same directory in both cases? – user207421 Jan 10 '13 at 20:41
  • Yes, I put the file in.txt on Desktop, and then open two terminals to run this code....am I wrong? – city Jan 10 '13 at 20:44
  • I'm using Windows 7, and running it through my IDE (Netbeans). However I don't feel like the IDE should have any effect. It's more probable that it has something to do with directories as @EJP said, or some Linux specs. – atomman Jan 10 '13 at 20:44
  • 1
    Please take a look at the answer at this post: http://stackoverflow.com/questions/2479222/problem-with-java-file-locking-mechanism-filelock-etc – atomman Jan 10 '13 at 20:46
  • @atomman, thanks for your help. I find the reason " File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine." I run it by eclipse and terminal at the same time, it works...I feel it is a bug in java, because it do not provide a strict lock on a file. – city Jan 10 '13 at 21:00
  • 1
    File locks are provided by the platform, not by Java. The locks are working as specified. Ergo not a bug. – user207421 Jan 11 '13 at 22:54

1 Answers1

2

FileLock does not work under Linux (at least not redhat last i tried). EDIT: that's not true. I did it wrong :-)

Under windows Vista/7/8 it worked last time I tried.

Maybe there are filesystem implementations for linux where this works. You have to use createion of [file].lck temporary files to emoulate locking (though its not recommended in the documentation of File.create).

There is a special atomic method to create a file if it does not exist

R.Moeller
  • 3,436
  • 1
  • 17
  • 12