Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file is already open

Tags:

java

file-io

I need to write a custom batch File renamer. I've got the bulk of it done except I can't figure out how to check if a file is already open. I'm just using the java.io.File package and there is a canWrite() method but that doesn't seem to test if the file is in use by another program. Any ideas on how I can make this work?

like image 704
hello_world_infinity Avatar asked Sep 07 '09 18:09

hello_world_infinity


People also ask

How can I tell if a file has been opened?

Using the open() function is one way to check a particular file is opened or closed. If the open() function opens a previously opened file, then an IOError will be generated. Another way to check a file is opened or closed is to check the values of the closed property of the file handler object.

How do you check if a file has been opened in C?

Check to make sure the file was successfully opened by checking to see if the variable == NULL. If it does, an error has occured. Use the fprintf or fscanf functions to write/read from the file. Usually these function calls are placed in a loop.

How can I tell if a file is in use?

The common managed way to check whether a file is in use is to open the file in a try block. If the file is in use, it will throw an IOException. Another way to check whether a file is in use is to call the CreateFile API. If a file is in use, the handle return is invalid.

How do you check if file is opened or not in C++?

You can check if a file has been correctly opened by calling the member function is_open(): bool is_open(); that returns a bool type value indicating true in case that indeed the object has been correctly associated with an open file or false otherwise.


3 Answers

Using the Apache Commons IO library...

boolean isFileUnlocked = false; try {     org.apache.commons.io.FileUtils.touch(yourFile);     isFileUnlocked = true; } catch (IOException e) {     isFileUnlocked = false; }  if(isFileUnlocked){     // Do stuff you need to do with a file that is NOT locked. } else {     // Do stuff you need to do with a file that IS locked } 
like image 116
JavaMachine31 Avatar answered Sep 25 '22 06:09

JavaMachine31


(The Q&A is about how to deal with Windows "open file" locks ... not how implement this kind of locking portably.)

This whole issue is fraught with portability issues and race conditions:

  • You could try to use FileLock, but it is not necessarily supported for your OS and/or filesystem.
  • It appears that on Windows you may be unable to use FileLock if another application has opened the file in a particular way.
  • Even if you did manage to use FileLock or something else, you've still got the problem that something may come in and open the file between you testing the file and doing the rename.

A simpler though non-portable solution is to just try the rename (or whatever it is you are trying to do) and diagnose the return value and / or any Java exceptions that arise due to opened files.

Notes:

  1. If you use the Files API instead of the File API you will get more information in the event of a failure.

  2. On systems (e.g. Linux) where you are allowed to rename a locked or open file, you won't get any failure result or exceptions. The operation will just succeed. However, on such systems you generally don't need to worry if a file is already open, since the OS doesn't lock files on open.

like image 45
Stephen C Avatar answered Sep 22 '22 06:09

Stephen C


    //  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");
    }
like image 26
Ahmed Adel Ismail Avatar answered Sep 21 '22 06:09

Ahmed Adel Ismail