FileLock and FileInputStream/FileOutputStream

Discussion in 'Java' started by bcernohous@hotmail.com, Apr 18, 2005.

  1. Guest

    How do I lock a (Properties) file, read it, change it, and write it
    back? I'm stuck because Properties.load() and Properties.store() use
    FileInputStream and FileOutputStream respectively and I can't seem to
    tie them together with a FileLock. If I FileLock the FileOutputStream,
    then I can't read it with FileInputStream. I want to hold the FileLock
    across load() and store(). Here's an attempt.


    FileOutputStream fos = null;
    FileChannel fosChannel;
    FileLock fosLock = null;
    fos = new FileOutputStream("c:\\$user\\test\\file.properties");
    fosChannel = fos.getChannel();
    fosLock = fosChannel.lock();

    FileInputStream fis = null;
    fis = new FileInputStream("c:\\$user\\test\\file.properties");

    // Open the existing repository
    Properties existingRepository = new Properties();
    try
    {
    existingRepository.load(fis);
    // FAILS with
    // java.io.IOException: The process cannot access the file because
    another process has locked a portion of the file
    // at java.io.FileInputStream.readBytes(Native Method)
    }
    catch (IOException ioe)
    {
    ioe.printStackTrace();
    }

    Properties newRepository = new Properties();
    newRepository.setProperty("count", "1");
    newRepository.store(fos, "master"); //Ok here since fos is locked

    ....

    I also tried a variation use File, hoping the lock would share the same
    File somehow, but it didn't help:

    File file = new File("c:\\$user\\test\\file.properties");
    ....
    fos = new FileOutputStream(file);
    ....
    fis = new FileInputStream(file);

    And I tried to lock the fis directly, but I can't get another FileLock
    when I hold the FileLock on the fos:

    FileChannel fisChannel;
    FileLock fisLock = null;
    fis = new FileInputStream("c:\\$user\\test\\file.properties");
    fisChannel = fis.getChannel();
    fisLock = fisChannel.tryLock(0L, Long.MAX_VALUE, true);
    // FAILS: fisLock will be null

    That's not what I expected from reading about FileLocks:

    "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."

    They certainly seem to be controlling access within one thread in one
    JVM. Holding it exclusive means I can't get it shared, at least from
    my trial and error attempts.

    I suppose a solution to my problem, although not a specific answer to
    my question, is to lock some other file like: c:\user\test\file.lock
    and hold it while I process c:\user\test\file.properties. That works if
    everyone cooperates and follows the same scheme. Doesn't stop some
    other application (notepad) from openning it but that's unlikely anyway.
    , Apr 18, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Zimmerman

    confused abt FileLock behavior

    David Zimmerman, Jul 28, 2003, in forum: Java
    Replies:
    3
    Views:
    440
  2. Krick
    Replies:
    2
    Views:
    14,230
    Marco Schmidt
    Aug 28, 2003
  3. Replies:
    4
    Views:
    8,450
    toddwh50
    Mar 11, 2010
  4. pcouas
    Replies:
    0
    Views:
    479
    pcouas
    Dec 5, 2006
  5. alejandrina

    Problem with FileLock

    alejandrina, Jul 13, 2007, in forum: Java
    Replies:
    12
    Views:
    629
    alejandrina
    Jul 30, 2007
Loading...

Share This Page