How to know whether a file is opened or not ?

Hi all,How to know whether a file is opened by its editor or not ? File may be any of the type.
[109 byte] By [mahesh.komuravellia] at [2007-11-27 2:19:28]
# 1

If you have access to file's stream(s), InputStream or OutputStream, a read or write test can be used, catching IOException if it is closed. Alternatively, if you have access to the file's FileDescriptor, valid() can be checked. Unfortunately, neither test can tell you (definitively) if it is the editor that has the file open.

developer_jbsa at 2007-7-12 2:20:00 > top of Java-index,Core,Core APIs...
# 2

There are platform-dependent commands that tell whether a file (or directory, port etc.) is used by which processes.

[oracle@izsak ~]$ fuser .

.:9299c

[oracle@izsak ~]$ ps -p 9299 -f

UIDPID PPID C STIME TTY TIME CMD

oracle9299 9298 0 Apr25 pts/000:00:00 -bash

[oracle@izsak ~]$ ls -l $fn

-rw-r-- 1 oracle oinstall 111215876 Apr 25 19:05 /opt/oracle/product/AS/10g/R2/opmn/logs/OC4J~ebank~default_island~1

[oracle@izsak ~]$ fuser $fn

/opt/oracle/product/AS/10g/R2/opmn/logs/OC4J~ebank~default_island~1: 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3758 3761 3762 3765 3767 3769 3770 3771 18638 21605 21743 21744 21745 22140 22143

[oracle@izsak ~]$ ps -p 3746 -f

UIDPID PPID C STIME TTY TIME CMD

oracle3746 26427 0 Apr23 ?00:00:01 /opt/oracle/product/AS/10g/R2/jdk/bin/java -server -Djava.security.policy=/opt...

BIJ001a at 2007-7-12 2:20:00 > top of Java-index,Core,Core APIs...
# 3

Open tcp socket ports:

[oracle@izsak ~]$ uname -a

Linux izsak.khb.hu 2.6.9-42.0.3.ELsmp #1 SMP Mon Sep 25 17:28:02 EDT 2006 i686 i686 i386 GNU/Linux

[oracle@izsak ~]$ fuser -n tcp 7777

here: 7777

7777/tcp:21061

[oracle@izsak ~]$ fuser -n tcp 7778

here: 7778

7778/tcp:21025 22158 22160 22164 22165 22168 22171 22174 22182 22185 22242

[oracle@izsak ~]$ ps -f -p 21061 21025 22185

UIDPID PPID C STIME TTYSTATTIME CMD

oracle210251 0 Feb20 ?S4:35 /opt/oracle/product/AS/10g/R2/Apache/Apache/bin/httpd -d /opt/oracle/produc

oracle210611 0 Feb20 ?Sl17:55 /opt/oracle/product/AS/10g/R2/webcache/bin/webcached -OPMN -U 669188145

oracle22185 21025 0 Apr25 ?Sl0:04 /opt/oracle/product/AS/10g/R2/Apache/Apache/bin/httpd -d /opt/oracle/produc...

BIJ001a at 2007-7-12 2:20:00 > top of Java-index,Core,Core APIs...
# 4
Thanks Ivan (Bij001) ,But i want to do it in windows not in Unix/Linux. Sorry, i did not mention in my first post. Can u suggest any way to do this in windows ?
mahesh.komuravellia at 2007-7-12 2:20:00 > top of Java-index,Core,Core APIs...
# 5
within the JVM you are limited to variations of my previous post
developer_jbsa at 2007-7-12 2:20:00 > top of Java-index,Core,Core APIs...
# 6
> Can u suggest any way to do this in windows ?There must be some native way but I do not know. There were Sysinternals Utilities (now belonging to M$): http://www.microsoft.com/technet/sysinternals/fileanddiskutilities.mspx
BIJ001a at 2007-7-12 2:20:00 > top of Java-index,Core,Core APIs...
# 7

Thanks Ivan.

I did not used sys internal utilities, logically i resolved the problem.

I tried it with handle.exe. Handle is a utility that displays information about open handles for any process in the system.

http://www.microsoft.com/technet/sysinternals/ProcessesAndThreads/Handle.mspx

Also i tried it with oh (sys internal utility), but finally i did not used any of these.

My solution :

I tried to rename the file with the same name, the result true indicates no handler is using that file, if the result is false, it indicates the file is using by some other handlers.

But by renaming we can not get the details like process id, handler name, .. in my case these details are not required. That is why i used this logic.

In case if we need those details we should use system internal utilities as you suggested..

mahesh.komuravellia at 2007-7-12 2:20:00 > top of Java-index,Core,Core APIs...