Java: How to determine if a file is in use?

Discussion in 'Java' started by Krick, Aug 27, 2003.

  1. Krick

    Krick Guest

    In Java, I need to be able to check whether a file is in use.

    So far, the only the only method I can come up with is this (in pseudo code)...


    1. make a copy of the original file (block copy the binary contents)
    2. attempt to delete the original file
    3a. if delete fails, file is in use, delete copy
    3b. if delete succeeds, rename copy to original name


    Will this work?
    Am I missing anything?
    What about cross platform compatability?


    ....
    Krick
    Krick, Aug 27, 2003
    #1
    1. Advertising

  2. Re: How to determine if a file is in use?

    "Krick" <> wrote in message
    news:...
    > In Java, I need to be able to check whether a file is in use.
    >
    > So far, the only the only method I can come up with is this (in pseudo

    code)...
    >
    >
    > 1. make a copy of the original file (block copy the binary contents)
    > 2. attempt to delete the original file
    > 3a. if delete fails, file is in use, delete copy
    > 3b. if delete succeeds, rename copy to original name
    >
    >
    > Will this work?


    If you are lucky, and are willing to live with all the side effects.

    - 2. for example, may fail when the file is not in use, and is marked
    read-only, or is protected.
    - Your copying may results in throwing away changes to the file after a
    block that has been copied is subsequently changed, *before* 3b.


    > Am I missing anything?

    Maybe. The approach does not sound too robust especially because detecting
    that a file is or is not in use, does not guarantee that for a subsequent
    operation *immediately* thereafter, the condition is guaranteed.

    Suppose your logic was

    a. Determine if file is in use
    b. If file is in use then do Step A
    c. If not do Step B

    If the intention is that Step A is done only if the file is in use, then you
    cannot be assured of that. Right after a., the file could end up being NOT
    in use.

    > What about cross platform compatability?


    You'll need to test it on platforms that you want your app to compatible
    across.

    >

    Check the file locking support in J2SE 1.4 (It is not universally supported
    on all platforms, though)

    HTH,

    --
    Shripathi Kamath
    NETAPHOR SOFTWARE INC.
    http://www.netaphor.com
    Shripathi Kamath, Aug 27, 2003
    #2
    1. Advertising

  3. Krick wrote:
    > In Java, I need to be able to check whether a file is in use.

    <snip>

    If it's your process that would be using the file then you could look at
    the source for java.util.logging.FileHandler for an example of how they
    ensure two loggers don't open the same file.

    Richard Kuhler
    Richard Kuhler, Aug 27, 2003
    #3
  4. Krick wrote:
    > In Java, I need to be able to check whether a file is in use.
    >
    > So far, the only the only method I can come up with is this (in pseudo code)...
    >
    >
    > 1. make a copy of the original file (block copy the binary contents)
    > 2. attempt to delete the original file
    > 3a. if delete fails, file is in use, delete copy
    > 3b. if delete succeeds, rename copy to original name
    >
    >
    > Will this work?
    > Am I missing anything?
    > What about cross platform compatability?
    >

    UNIX will be perfectly happy to let you delete a file that's in use.
    When the other use of that file completes, the file will disappear.
    David Zimmerman, Aug 28, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spotted Owl Eater

    Determine the File Type of an Uploaded File

    Spotted Owl Eater, Nov 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,842
    Patrice
    Nov 16, 2005
  2. index
    Replies:
    18
    Views:
    2,557
    opalinski from opalpaweb
    May 11, 2006
  3. Ian F. Hood
    Replies:
    7
    Views:
    334
    Prateek
    Dec 14, 2006
  4. Peña, Botp
    Replies:
    1
    Views:
    221
    Robert Klemme
    Jan 24, 2004
  5. Nick Weavers
    Replies:
    4
    Views:
    122
Loading...

Share This Page