Emanuele Mazzoni
Emanuele Mazzoni

Reputation: 716

How to determine if file is in use by another process (Java)

I have tried many examples, but no one works. I try this but don't work.

I tried also to use the tryLock(). It always returns false. why?

private boolean checkCompleteFile(File f)
{           
    RandomAccessFile file = null;
    FileLock fileLock = null;

    try
    {
        file = new RandomAccessFile(f, "rw");
        FileChannel fileChannel = file.getChannel();

        fileLock = fileChannel.lock();
        if (fileLock != null)
        {
            fileLock.release();
            file.close();
            return false;
        }

    }
    catch(Exception e)
    {
         return false;
    }

    return true;
}

Upvotes: 9

Views: 16974

Answers (3)

StarCrafter
StarCrafter

Reputation: 461

Try using this:

try {
    @SuppressWarnings("resource")
    FileChannel channel = new RandomAccessFile(fileToRead, "rw").getChannel();
    //This method blocks until it can retrieve the lock. 
    FileLock lock = channel.lock(); // Try acquiring the lock without blocking. 
    try { 
        lock = channel.tryLock();
    } catch (OverlappingFileLockException e){

    }
    lock.release(); //close the file.
    channel.close();
} catch (Exception e) {             

}

Upvotes: 2

CloudyMarble
CloudyMarble

Reputation: 37576

You catch an exception and return false, thats why you get false all the time, do something with the exception or do not catch it so you know if an exception was thrown, if you catch a general exception a false return value is not really meaningful.

try {
  lock = channel.tryLock();
  // ...
} catch (OverlappingFileLockException e) {
  // File is already locked in this thread or virtual machine
}
lock.release();
channel.close();

You cam just try to access the file and catch an exception if it fails:

boolean isLocked=false;
RandomAccessFile fos=null;
try {
      File file = new File(filename);
      if(file.exists())
        fos=new RandomAccessFile(file,"rw");        
}catch (FileNotFoundException e) {
    isLocked = true;
}catch (SecurityException e) {
    isLocked = true;
}catch (Exception e) {
    // handle exception
}finally {
    try {
        if(fos!=null) {
            fos.close();
        }
    }catch(Exception e) {
        //handle exception
    }
}

Notice that the RandomAccessFile class throws:

FileNotFoundException -

if the mode is "r" but the given string does not denote an existing regular file, or if the mode begins with "rw" but the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.

SecurityException -

if a security manager exists and its checkRead method denies read access to the file or the mode is "rw" and the security manager's checkWrite method denies write access to the file

Upvotes: 3

qrtt1
qrtt1

Reputation: 7957

How about using the linux command ?

lsof -p 

The command will show the file open status, you can parse it to check who use it.

Upvotes: 0

Related Questions