Check if file is opened

is there any way in java to check if a file is opened and closed?

i found this on web but doesnt work, seems that it is a right approach..dont know where goes wrong..

import java.io.*;

import java.nio.channels.*;

publicclass FileStatusChecking{

publicstaticvoid main(String[] args){

try{

// Get a file channel for the file

File file =new File("empty.txt");

FileChannel channel =new RandomAccessFile(file,"rw").getChannel();

// Use the file channel to create a lock on the file.

// This method blocks until it can retrieve the lock.

FileLock lock = channel.lock();

// Try acquiring the lock without blocking. This method returns

// null or throws an exception if the file is already locked.

try{

lock = channel.tryLock();

System.out.println("FILE CLOSED");

}catch (OverlappingFileLockException e){

// File is already locked in this thread or virtual machine

System.out.println("FILE OPENING");

}

// Release the lock

lock.release();

// Close the file

channel.close();

}catch (Exception e){

}

}

}

[2278 byte] By [servletBoy499a] at [2007-11-26 17:12:20]
# 1
> i found this on web but doesnt work, seems that it is> a right approach..dont know where goes wrong..What does "doesnt [sic] work" look like?%
duffymoa at 2007-7-8 23:40:13 > top of Java-index,Java Essentials,Java Programming...
# 2
if you found this code on the net then there should not be anything wrong with it...it looks ok....can u plz tell me what sort of an error it is giving?thanx
KayDeEa at 2007-7-8 23:40:13 > top of Java-index,Java Essentials,Java Programming...
# 3
No matter the target file is opened or closed, its still print out "File opening"....
servletBoy499a at 2007-7-8 23:40:13 > top of Java-index,Java Essentials,Java Programming...
# 4

this channel mess is silly, dudes.

use this:

public static boolean isFileLockedReadOnly(File file)

{

try {

// Get a file channel for the file

RandomAccessFile test = new RandomAccessFile(file, "rw");

test.close();

return false;

} catch (Exception e) {

System.out.println("so sad - "+e.toString());

return true;

}

}

Mike_Katchourinea at 2007-7-8 23:40:13 > top of Java-index,Java Essentials,Java Programming...