Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

26
LINES

< > BotCompany Repo | #1016937 // Test File Locking & appending to file (OK on Linux & Windows)

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (1576L/11K).

!7

import java.nio.channels.*;
import java.nio.file.*;
import java.nio.*;

p {
  File path = programFile("lockfile");
  FileChannel channel = FileChannel.open(toPath(path), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
  repeat with sleep 1 {
    {
      temp FileLock lock = channel.tryLock();
      if (lock == null) print("Couldn't lock.");
      else {
        print("Locked!");
        //appendToFile(path, "test\n");
        long size = channel.size();
        print("Size=" + size);
        channel.position(size);
        channel.write(ByteBuffer.wrap(toUtf8("test\n")));
        print("New size=" + channel.size());
        sleepSeconds(1);
      }
    }
  }
}

Author comment

Began life as a copy of #1016917

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

-
Snippet ID: #1016937
Snippet name: Test File Locking & appending to file (OK on Linux & Windows)
Eternal ID of this version: #1016937/5
Text MD5: c281f36f8f794bed56db2b51a2a2e5b5
Transpilation MD5: 636f20d4e8e44c79b5c4ab1e80eed208
Author: stefan
Category: javax / io
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-07-08 17:40:12
Source code size: 703 bytes / 26 lines
Pitched / IR pitched: No / No
Views / Downloads: 451 / 1106
Version history: 4 change(s)
Referenced in: