Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Checking if file is open in Java

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to IO...

You might want to do a search - I remember that there was a similar discussion recently...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks to all of you who kindly answered my question.

J. Cardoso
Application Developer
SCJP
 
Greenhorn
Posts: 9
Android Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic