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

1  
!7
2  
3  
import java.nio.channels.*;
4  
import java.nio.file.*;
5  
import java.nio.*;
6  
7  
p {
8  
  File path = programFile("lockfile");
9  
  FileChannel channel = FileChannel.open(toPath(path), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
10  
  repeat with sleep 1 {
11  
    {
12  
      temp FileLock lock = channel.tryLock();
13  
      if (lock == null) print("Couldn't lock.");
14  
      else {
15  
        print("Locked!");
16  
        //appendToFile(path, "test\n");
17  
        long size = channel.size();
18  
        print("Size=" + size);
19  
        channel.position(size);
20  
        channel.write(ByteBuffer.wrap(toUtf8("test\n")));
21  
        print("New size=" + channel.size());
22  
        sleepSeconds(1);
23  
      }
24  
    }
25  
  }
26  
}

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: 452 / 1111
Version history: 4 change(s)
Referenced in: [show references]