Java Utililty Methods FileLock

List of utility methods to do FileLock

Description

The list of methods to do FileLock are organized into topic(s).

Method

booleanisBlocking(Channel channel)
Indicates if the channel is in blocking mode.
boolean result = true;
if (channel instanceof SelectableChannel) {
    SelectableChannel selectableChannel = (SelectableChannel) channel;
    result = selectableChannel.isBlocking();
return result;
booleanisFileLocked(File file)
Uses Java 1.4's FileLock class to test for a file lock
boolean isLocked = false;
FileOutputStream input = null;
FileLock lock = null;
if (!file.exists()) {
    return false;
try {
    input = new FileOutputStream(file);
...
booleanisFileLocked(File file)
is File Locked
if (!file.exists()) {
    return false;
if (file.isDirectory()) {
    return false;
if (isSymlink(file)) {
    return false;
...
booleanisLocked(File file)
is Locked
if (!file.isFile()) {
    return false;
try {
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
    try {
        FileChannel channel = raf.getChannel();
        FileLock lock = channel.tryLock();
...
booleanisLocked(File file)
is Locked
if (!file.isFile()) {
    return false;
try (FileChannel fc = FileChannel.open(file.toPath(), StandardOpenOption.CREATE,
        StandardOpenOption.WRITE)) {
    FileLock lock = fc.tryLock();
    if (lock != null) {
        lock.release();
...
FileOutputStreamlock(File file)
lock
try {
    FileOutputStream out = new FileOutputStream(file);
    FileLock lock = out.getChannel().tryLock();
    if (lock == null) {
        throw new IllegalStateException("Cannot acquire a lock on file " + file
                + ". Please verify that this file is not used by another process.");
    locks.put(file, lock);
...
FileLocklockFile(File file)
Locks a file for the current JVM.
FileOutputStream input = null;
FileLock lock = null;
try {
    input = new FileOutputStream(file);
    FileChannel fileChannel = input.getChannel();
    lock = fileChannel.tryLock();
    if (lock.isValid())
        return lock;
...
FileLocklockFile(File file, RandomAccessFile raf)
lock File
FileLock fl = null;
FileChannel fc = raf.getChannel();
if (fc != null) {
    try {
        fl = fc.tryLock();
    } catch (OverlappingFileLockException ex) {
        throw new IOException(file.getPath() + ":\nDatei ist gesperrt.\n"
                + "Bitte schlie\u00DFen Sie die Datei in dem Programm,\n"
...
booleanlockFileExists(File file)
lock File Exists
Path lockFilePath = getLockFilePath(file);
return Files.exists(lockFilePath);
SocketChannelopenTcpSocket(boolean blocking)
Open and return a socket channel with the specified blocking mode enabled or null if there is an error opening the channel.
try {
    return (SocketChannel) SocketChannel.open().configureBlocking(blocking);
} catch (Exception ex) {
    return null;