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) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class Main {

    public static boolean isLocked(File file) throws IOException {
        if (!file.isFile()) {
            return false;
        }//www .jav a2 s.c  om
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            try {
                FileChannel channel = raf.getChannel();
                FileLock lock = channel.tryLock();
                if (lock == null) {
                    return true;
                }
                lock.release();
                return false;
            } finally {
                raf.close();
            }
        } catch (FileNotFoundException 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)