Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Possible to test if file is open?

Thread view: 
Chris - 31 Dec 2006 20:32 GMT
I need to test if a file is open by another process.

The "other process" will always be another instance of my program,
possibly running on a physically separate server, and accessing the file
on a shared drive.

Is this possible in Java?
Arne Vajhøj - 31 Dec 2006 21:20 GMT
> I need to test if a file is open by another process.
>
[quoted text clipped - 3 lines]
>
> Is this possible in Java?

You can try FileChannel lock.

But whether it will work as you want depends
on the operating system and the software providing
the shared drive.

Arne
Mike Schilling - 31 Dec 2006 22:00 GMT
On Windows, you can try to delete it, sicne Windows won't let you delete an
open file.

This approach does have certain disadvantages.
ck - 01 Jan 2007 19:01 GMT
On Jan 1, 3:00 am, "Mike Schilling" <mscottschill...@hotmail.com>
wrote:
> On Windows, you can try to delete it, sicne Windows won't let you delete an
> open file.
>
> This approach does have certain disadvantages.

Not necessary. Try this out on windows. Open any text document in
notepad, and then try to delete. Document gets deleted.

Cheers,
Ck
http://www.gfour.net
Arne Vajhøj - 01 Jan 2007 19:09 GMT
> On Jan 1, 3:00 am, "Mike Schilling" <mscottschill...@hotmail.com>
> wrote:
[quoted text clipped - 5 lines]
> Not necessary. Try this out on windows. Open any text document in
> notepad, and then try to delete. Document gets deleted.

And what does that prove ?

Most likely that notepad does not keep the file
open (in the meaning of "open file" used by programmers).

Arne
ck - 01 Jan 2007 21:10 GMT
Arne Vajh?j wrote:
> > On Jan 1, 3:00 am, "Mike Schilling" <mscottschill...@hotmail.com>
> > wrote:
[quoted text clipped - 12 lines]
>
> Arne

Well I did not try to prove anything. The question was can we do check
if the file is open by another process or not, so there could be a lot
of other process (like notepad) which might use the file without
locking out the file.

Cheers,
Ck
http://www.gfour.net
Lew - 02 Jan 2007 06:27 GMT
"Mike Schilling" wrote:
>> On Windows, you can try to delete it, sicne Windows won't let you delete an
>> open file.

>>> Not necessary. Try this out on windows. Open any text document in
>>> notepad, and then try to delete. Document gets deleted.

Arne Vajhøj wrote:
>> And what does that prove ?
>>
>> Most likely that notepad does not keep the file
>> open (in the meaning of "open file" used by programmers).

> Well I did not try to prove anything. The question was can we do check
> if the file is open by another process or not, so there could be a lot
> of other process (like notepad) which might use the file without
> locking out the file.

The question, as you pointed out, is whether the file is *open* by another
process or notl your example did not actually address that issue. If notepad
is not holding the file open, as Arne explained, then it is not part of the
scenario in question.

"Mike Schilling" wrote:
>> On Windows, you can try to delete it, sicne Windows won't let you delete an
>> open file.
>>
>> This approach does have certain disadvantages.

You did realize that this was not actually a serious suggestion, right?

- Lew
Martin Gregorie - 02 Jan 2007 13:21 GMT
> "Mike Schilling" wrote:
>>> On Windows, you can try to delete it, sicne Windows won't let you
[quoted text clipped - 9 lines]
>>> Most likely that notepad does not keep the file
>>> open (in the meaning of "open file" used by programmers).

Experience says that most ASCII text editors (notepad, vi, microEmacs.
PFE etc) do not hold files open. Each load and save operation typically
 opens the file, does what it needs to do and closes it again.

Many word processors, OTOH, seem to hold files open. I think Word falls
into this class but can't prove it.

But, why just speculate about this when you can easily try an
experiment. All you need is a simple CLI program whose logic is
something line:

    open a file
    write a prompt to the console
    read the response to the prompt
    close the file
    exit

The program could have options to write or read the file. Its benefit is
that, unlike using some program with unknown characteristics, with this
you *KNOW* that the file is open once you've seen the prompt but haven't
yet responded to it. At this point you can run other commands or even
(shock, horror!) code that you think should check whether the file is in
use or not.

Running tests is the only safe way to go because actual behavior is
likely to be platform-specific. I'd guess that File.canRead() and
File.canWrite() may detect file locks under most Unices, MVS or
Guardian, because these OSen have kernel support for file locking, but
probably do not under Windows. I may be wrong here because I haven't
seriously programmed for that environment since Win 95: IIRC there is no
kernel level locking in Win 95 or its associated version of DOS.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

ck - 02 Jan 2007 14:27 GMT
> Experience says that most ASCII text editors (notepad, vi, microEmacs.
> PFE etc) do not hold files open. Each load and save operation typically
>   opens the file, does what it needs to do and closes it again.

I am not sure about vi either. It gives a warning if you try to open an
already open file.

# E325: ATTENTION
# Found a swap file by the name ".s6.c.swp"
# Swap file ".s6.c.swp" already exists!

I guess its not due to lock but due to existence of a swap file.

Cheers,
Ck
Martin Gregorie - 02 Jan 2007 23:07 GMT
> # E325: ATTENTION
> # Found a swap file by the name ".s6.c.swp"
> # Swap file ".s6.c.swp" already exists!
>
> I guess its not due to lock but due to existence of a swap file.

Yes. A quick glance at the manpage says that vi uses it primarily for
recovery after a crash. There will always be a swap file (unless the
user invoked the option to disable it) BUT it isn't the same as the file
being edited. Its name is the same as the file being edited with ".swp"
appended. At least, that applies to vim but may not apply to other
implementations like stevie and elvis. he lightweight vi clone pvic
doesn't use a swap file.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Daniel Pitts - 31 Dec 2006 21:56 GMT
> I need to test if a file is open by another process.
>
[quoted text clipped - 3 lines]
>
> Is this possible in Java?
Open it with exclusive access? I'm not sure if its possible in Java,
but I'd be surprised if it wasn't.
mearvk - 02 Jan 2007 00:00 GMT
Check out this URL:
http://java.sun.com/j2se/1.5.0/docs/api/java/nio/channels/FileChannel.html

FileChannel.tryLock()
FileChannel.lock()

These won't tell you if another process has the file open
non-exclusively, but they will tell you if they are open by a process
exclusively.

Mearvk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.