New to Java - How to check if a file is already open

D

Dino Buljubasic

How can I check if a file is already open before trying to delete it
programmatically?

something like this


if file is open

close it first

end if

then delete file


thank you
_dino_
 
H

hilz

Dino said:
How can I check if a file is already open before trying to delete it
programmatically?

something like this


if file is open

close it first

end if

then delete file


thank you
_dino_

if the file was opened by a process different than your program, you
cannot close it.
so in this case, your code should be:

if file is open

delete it

else

display error message

end if


HTH
 
R

Roedy Green

if file is open

close it first

end if

then delete file

If you have a handle to an OutputStream and the logic makes it unclear
whether it is open or closed at any point, say because of exceptions,
use the convention of setting the handle to null before opening and
after closing. Then by testing for null, you can tell an open from a
closed file. Of course this will tell you nothing about what other
tasks or threads or even handles to the same file in the same thread
are doing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top