Check if a file is open or not

I am trying to check if a file is open or not using Java. I did get a partial solution. It is:

Take the filename.

Rename the file to a different name

If the new filename exists, then it is not being used by another program( which in turn means that the file is not open).

If the file is accessed by some other program, then Windows (which is my target OS right now) is not letting the rename happen. So, if the new filename does not exist, it means the file is open.

Now, the problem is here. If we try that for a Word Document, it works fine. For a pdf, it works great. And I guess it works for most of the files. But, I tried that out for a .txt document. Even if the file is open, it creates a new file under the new filename. So, the check does not work.

Any comments, help?

Related Thread: http://forum.java.sun.com/thread.jspa?threadID=662171

Thanks.

[911 byte] By [vinothkpa] at [2007-11-27 11:14:44]
# 1

What if you copy it and then try to delete the original file?

Message was edited by:

ajaimes

ajaimesa at 2007-7-29 14:08:36 > top of Java-index,Java Essentials,Java Programming...
# 2

What are you wanting to do that causes you to want to know:

a "file is open", whatever that means?

ChuckBinga at 2007-7-29 14:08:36 > top of Java-index,Java Essentials,Java Programming...
# 3

> Now, the problem is here. If we try that for a Word

> Document, it works fine. For a pdf, it works great.

> And I guess it works for most of the files. But, I

> tried that out for a .txt document. Even if the file

> is open, it creates a new file under the new

> filename. So, the check does not work.

I'd bet the .txt file isn't "open" either. By "open" do you mean you have Notepad running with the contents of that file displayed in its window? That doesn't mean the file is "open" at all. That would just mean Notepad HAD opened it, read the contents into memory, and likely closed it. Then it showed the memory contents in a window.

warnerjaa at 2007-7-29 14:08:36 > top of Java-index,Java Essentials,Java Programming...
# 4

>What if you copy it and then try to delete the original file?

>Message was edited by:

>ajaimes

In that case, the file will be deleted. Then, when you save the opened file, it will save as a new file. Then, how would you know if the file is being accessed or not?

However, this could be the possible reason.....

> I'd bet the .txt file isn't "open" either. By "open"

> do you mean you have Notepad running with the

> contents of that file displayed in its window? That

> doesn't mean the file is "open" at all. That would

> just mean Notepad HAD opened it, read the contents

> into memory, and likely closed it. Then it

> showed the memory contents in a window.

> By: warnerja

Yes. That is probably right. Now, I have some improvement in understanding what is happening. This does not happen with the other types of files like Word Documents, etc. Why? Dont they do the same thing? And Then, in the case of .txt files, how can we know if the file is open or not.

I will reframe the title a little bit here: How can we check if a file is being accessed by other program or not?

Any more comments or ideas.

Thanks.

vinothkpa at 2007-7-29 14:08:36 > top of Java-index,Java Essentials,Java Programming...
# 5

Just because something like Notepad is displaying what it saw in the file doesn't mean the file is being accessed by anything.

You have a weird 'requirement'.

If you "print" the contents of the file and hand out copies to people to look at, do you need your application to know if anyone is currently looking at the paper too? It's being "accessed" just as much in that situation as it is when Notepad has it displayed on your screen.

warnerjaa at 2007-7-29 14:08:36 > top of Java-index,Java Essentials,Java Programming...