Java FileLock isLocked(File file)

Here you can find the source of isLocked(File file)

Description

is Locked

License

Open Source License

Declaration

public static boolean isLocked(File file) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.IOException;

import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

import java.nio.file.StandardOpenOption;

public class Main {

    public static boolean isLocked(File file) {
        if (!file.isFile()) {
            return false;
        }//from   ww w .  j  a va  2  s .  co  m
        try (FileChannel fc = FileChannel.open(file.toPath(), StandardOpenOption.CREATE,
                StandardOpenOption.WRITE)) {
            FileLock lock = fc.tryLock();
            if (lock != null) {
                lock.release();
                return false;
            } else {
                return true;
            }
        } catch (IOException e) {
            return true;
        }
    }
}

Related

  1. isBlocking(Channel channel)
  2. isFileLocked(File file)
  3. isFileLocked(File file)
  4. isLocked(File file)
  5. lock(File file)
  6. lockFile(File file)
  7. lockFile(File file, RandomAccessFile raf)
  8. lockFileExists(File file)