Opening a file output stream in a file that is already locked overwrites it

I came across this scenario and did not understand why this is happening. Can someone please help me understand the nio file lock behavior.

I opened the file using FileOutputStream, and after acquiring an exclusive lock using nio FileLock, I wrote some data to the file. Did not release the castle. I opened another FileOutputStream file in the same file with the intention of getting a lock and performing a write operation and expect it to work. But opening the second outoutstream file overwrites the already locked file in which the data was written, even before I try to get the second lock. Is this expected? My understanding was to acquire an exclusive lock that would prevent any changes to the locked file. How can I prevent my file from being overwritten when I try to get another lock? (as if another process is trying to get a lock in the same file on another vm?)

An example of the program I tried:

        File fileToWrite = new File("C:\\temp\\myfile.txt");
        FileOutputStream fos1 = new FileOutputStream(fileToWrite);
        FileOutputStream fos2 =null;
        FileLock lock1,lock2 =null;
        lock1=fos1.getChannel().tryLock();
        if(lock1!=null){
            //wrote date to myfile.txt after acquiring lock
            fos1.write(data.getBytes());
            //opened myfile.txt again and this replaced the file
            fos2 = new FileOutputStream(fileToWrite);
            //got an overlappingfilelock exception here
            lock2=fos2.getChannel().tryLock();
            fos2.write(newdata.getBytes());
            }

        lock1.release();
        fos1.close();
        if(lock2!=null)
            lock2.release();
        fos2.close();

. 1- , 1- . , 1, 2. :

Program1:

    File fileToWrite = new File("C:\\temp\\myfile.txt");
    FileOutputStream fos1 = new FileOutputStream(fileToWrite);
    FileLock lock1 =null;
    lock1=fos1.getChannel().tryLock();
    if(lock1!=null){
        //wrote date to myfile.txt after acquiring lock
        fos1.write(data.getBytes());
        System.out.println("wrote data and waiting");
        //start other program while sleep
        Thread.sleep(10000);
        System.out.println("finished wait");
        }

    lock1.release();
    fos1.close();

Program2:

   File fileToWrite = new File("C:\\temp\\myfile.txt");
    System.out.println("opening 2nd out stream");
    //this overwrote the file
    FileOutputStream fos2 = new FileOutputStream(fileToWrite);
    FileLock lock2 =null;
    lock2=fos2.getChannel().tryLock();
    //lock is null here
    System.out.println("lock2="+lock2);
    if(lock2!=null){
        //wrote date to myfile.txt after acquiring lock
        System.out.println("writing  NEW data");
        fos2.write(newdata.getBytes());
        }

    if(lock2!=null)
        lock2.release();
    fos2.close();

+4
2

FileLock, JVM. FileOutputStream JVM FileLock - JVM . , OverlappingFileLockException , ( tryLock null), , : .

JVM , , . , .

new FileOutputStream(fileToWrite, true), . , JVM .

, , . , , . FileOutputStream , , .

, API FileChannel ( Java 7). , < href= "http://docs.oracle.com/javase/8/docs/api/java/nio/file/StandardOpenOption.html#APPEND" rel= "nofollow" > . :

try(FileChannel fch=FileChannel.open(fileToWrite.toPath(),
                                     StandardOpenOption.CREATE, StandardOpenOption.WRITE)){
  try(FileLock lock=fch.tryLock()) {
    if(lock!=null) {
      // you can directly write into the channel
      // but in case you really need an OutputStream:
      OutputStream fos=Channels.newOutputStream(fch);
      fos.write(testData.getBytes());
      // you may explicitly truncate the file to the actually written content:
      fch.truncate(fch.position());
      System.out.println("waiting while holding lock...");
      LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(5));
    }
    else System.out.println("couldn't acquire lock");
  }
}

Java 7, . , CREATE, , , CREATE_NEW, , .

- open , . .

+1

.

Javadoc:

, , . , , , . , , , , . , , , . , , API, .

+1
source

Source: https://habr.com/ru/post/1589120/


All Articles