How can we know the file is opened?

Hi all

I am using fileChooser save dialog for a file...

when ever i m trying to save the file name which is already opened

i m getting FileNotFoundException...

Now i want to know how can we know the file is already opened....

Can anyone give me some idea.....

Thanks in advance

[320 byte] By [vrkrajeshrajua] at [2007-11-27 9:08:28]
# 1
I'm afraid that this is very platform specific, and Java does not do platform specific things.So if you want to check, try to save and if you get your exception you know that the file is probably already opened so you can notify the user.
gimbal2a at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 2
Interesting.Just curious: what does File.exists() return in this situation?
OleVVa at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 3
Ole,That's a Very Good Question.I'm on winblows at home and solaris at work... I'll try it on both and post the results.... Solaris will have to wait till Monday. I did the Friday happy dance a day early this week.Keith.
corlettka at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks oleThe File.Exists() return the the file is exists or not...But i m in the situation that the file is opened or not...can u tell me any other ideas..thanksrajesh
vrkrajeshrajua at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 5

Well, not a very pleasant solution, but you might settle with it: If the file exists and you get a FileNotFoundException trying to write to it, assume it's open. If it doesn't exist, you can be sure it's not open, right? If you get some other exception, assume there's a different problem (the file is not open). If you get no exceptions, be happy :-) I'll cross my fingers that the assumptions hold :-)

OleVVa at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 6

> If it doesn't exist, you can be sure it's not open, right?

Under Unix, you can normally delete (remove) or rename (move) an open file. A file can have several names, so called hard links. If the last hard link is gone, the file contents are erased when the last open descriptor is closed.

So in some sense, under Unix, a non-existent file can still be open.

BIJ001a at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 7

You can't tell if the is open or not in pure java because it's an Operating System specific operation... and in fact the whole issue is an o/s specific constraint.

Window's won't let you move or rename a folder whilst files that in contains are open... unix has no such qualms.

If your code is intended to run ONLY EVER on windows, then I suspect (though I don't know for sure) that you could call a windows API method via JNI.

searching MSDN for "file is already open" leads to:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilemodeclasstopic.asp

google "JNI" leads to:

JNI - http://java.sun.com/j2se/1.5.0/docs/guide/jni/index.html

Keith.

corlettka at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 8

> Under Unix, you can normally delete (remove) or

> rename (move) an open file. A file can have several

> names, so called hard links. If the last hard link is

> gone, the file contents are erased when the last open

> descriptor is closed.

>

> So in some sense, under Unix, a non-existent file can

> still be open.

Point taken.

OleVVa at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 9

I had the same question about knowing when a file is open. I want to catch the FileNotFoundException but my code is not catching it.

try

{

ImageIO.write(image, suffix, file);

}

catch (IOException e)

{

System.out.println("EXCEPTION CAUGHT");

}

Does anyone know why this is not being caught.

flyer12a at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 10
If that catch block is not being executed, then the call to write() is not throwing IOException. It's either succeeding or throwing a different exception.
jverda at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 11

> If that catch block is not being executed, then the

> call to write() is not throwing IOException. It's

> either succeeding or throwing a different exception.

The error I'm getting is:

java.io.FileNotFoundException: <my file> (The process cannot access the file because it is being used by another process)

...

I also tried

try

{

ImageIO.write(image, suffix, file);

}

catch (FileNotFoundException e)

{

System.out.println("EXCEPTION CAUGHT");

}

catch (IOException e)

{

System.out.println("EXCEPTION CAUGHT");

}

But the catch blocks are not executing.

flyer12a at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 12
> But the catch blocks are not executing.Then neither of those exceptions is being thrown by that line of code.Or else you're not actually running that code, but some older version of your code.
jverda at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 13

Thanks for your help. I guess no exception is being thrown here. I don't understand why I'm getting the message:

java.io.FileNotFoundException: <my file> (The process cannot access the file because it is being used by another process)

...

but everything seems to be working now.

flyer12a at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 14

> Thanks for your help. I guess no exception is being

> thrown here. I don't understand why I'm getting the

> message:

> java.io.FileNotFoundException: <my file> (The process

> cannot access the file because it is being used by

> another process)

The exception is being thrown somewhere. It should tell you what line. If it doesn't, it's because you caught it and didn't call printStackTrace.

jverda at 2007-7-12 21:46:42 > top of Java-index,Java Essentials,Java Programming...
# 15

My code is now:

try

{

boolean result = ImageIO.write(image, suffix, file);

}

catch (NullPointerException e)

{

System.out.println("EXCEPTION CAUGHT");

}

catch (IOException e)

{

System.out.println("EXCEPTION CAUGHT");

}

catch (Exception e)

{

System.out.println("EXCEPTION CAUGHT");

}

When the file I'm trying to save already open there is a NullPointerException which I catch.

But I also get the FileNotFoundException message. It says the line is:

boolean result = ImageIO.write(image, suffix, file);

It doesn't seem to be effecting my program though so hopefully this will be ok.

flyer12a at 2007-7-21 22:55:36 > top of Java-index,Java Essentials,Java Programming...
# 16

> When the file I'm trying to save already open there

> is a NullPointerException which I catch.

Then problem is that you then go on as if everything is fine. Catching an exception doesn't make the problem go away.

Catching an exception provides a way for you to actually deal with the problem. If you can't fix it, you either shouldn't catch the exception, or if you do, you should wrap it in a more layer-appropriate exception and rethrow.

> But I also get the FileNotFoundException message. It

> says the line is:

> boolean result = ImageIO.write(image, suffix, file);

> It doesn't seem to be effecting my program though so

> hopefully this will be ok.

I doubt it.

jverda at 2007-7-21 22:55:36 > top of Java-index,Java Essentials,Java Programming...
# 17

I do deal with the NullPointerException, I just didn't show it in the code above. I don't have a problem there. It's this FileNotFoundException message that I can't figure out, (it occurs before the NullPointerException is thrown). I can't catch it so there doesn't seem to be anything I can do about it.

flyer12a at 2007-7-21 22:55:36 > top of Java-index,Java Essentials,Java Programming...
# 18
Well, since you're not showing actual code, or even representative code, I don't know what else to say.Good luck.
jverda at 2007-7-21 22:55:36 > top of Java-index,Java Essentials,Java Programming...
# 19
> I can't catch it> so there doesn't seem to be anything I can do about> it.If you look at the stack trace, you'll see where it's occurring, and you can catch it there, or change your code so it doesn't occur.
jverda at 2007-7-21 22:55:36 > top of Java-index,Java Essentials,Java Programming...
# 20
Thanks for your help, I will look into that. (I shouldn't have mentioned the NullPointerException, it just made my question more confusing.)
flyer12a at 2007-7-21 22:55:36 > top of Java-index,Java Essentials,Java Programming...