• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking if file is open in Java  RSS feed

 
jonilson cardoso
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there !

I am doing the exception handling in a java program. I don't want to copy, transfer or delete a file if it is open. The trouble is: I can't find in the java API's any method that would check if a file is open. Can anyone help ?

Cardoso
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

I don't know of any such API. I looked at FileOutputStream and it will fail if the file is already open and the OS doesn't allow multiples. That sounds a bit unreliable.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to IO...

You might want to do a search - I remember that there was a similar discussion recently...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you concerned that the file might be held open by some other code runing in the same JVM, or by some other process outside the JVM? If it's the latter, you may be able to decect this using the tryLock() method in java.nio.FileChannel. If this returns null, that means another process has a lock on the file for some reason. If you're more interested in detecting if the file is held open by this JVM, I'm not sure if this will work. But the API seems to imply an exception will be thrown if the lock is already held elsewhere. So I suppose you could catch it.

Otherwise, I don't know of an API that detects this for you. I would recommend that for code in the same JVM, you should probably try to make sure that anytime you open a file (create a FileOutputStream or whatever), you close() the file inside a finally block. In that case, you'll be sure the file got closed, period, and you won't need to query another method to find out if it's open. There may be some cases where this is not possible to guarantee, but I think they're rare.
 
jonilson cardoso
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks to all of you who kindly answered my question.

J. Cardoso
Application Developer
SCJP
 
Ahmed Adel
Greenhorn
Posts: 9
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

// TO CHECK WHETHER A FILE IS OPENED
// OR NOT (not for .txt files)

// the file we want to check
String fileName = "C:\\Text.xlsx";
File file = new File(fileName);

// try to rename the file with the same name
File sameFileName = new File(fileName);

if(file.renameTo(sameFileName)){
// if the file is renamed
System.out.println("file is closed");
}else{
// if the file didnt accept the renaming operation
System.out.println("file is opened");
}


i know it is long ago, but i didnt find any answers across the internet, so hope this may help any body searching for it
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two problems with that.

1) It's platform-specific. It won't work on Linux, as we're allowed to delete and rename files that other applications are using.

2) It's not atomic. Even if that tells you the file is "not open", so what? By the time your Java app goes to access it, another app might have opened it. So you have to be able to deal with the case of trying (and failing) to access an already opened file anyway. It's like testing if a server is up before trying to connect to it--mostly pointless.
 
Dilip H Pashupathi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
try with MIME type check. if your MIME type says application/octet-stream. It means the file is open.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!