This discussion is archived
7 Replies Latest reply: Nov 11, 2011 4:18 PM by EJP RSS

how to find out if the file is locked

Johnny_hunter Newbie
Currently Being Moderated
Hello All:

I've struggled with this for quite a while now. I want to move a file from one directory to another. I can choose to copy/delete or renameTo. Both works when I tested from a console test app.

However, this is part of a big Swing app of mine. When I tried the code in that app, it always returned false, indicating it failed. I can't get more info other than that. It might be locked by another app, but how I can find out?

Using JDK 1.7's new APIs might be helpful, but I can't use JDK 7.

Thanks for your thoughts,
John
  • 1. Re: how to find out if the file is locked
    EJP Guru
    Currently Being Moderated
    Open it and try to lock it with FileChannel.tryLock(). However that may not tell the whole story. If you're running on Windows you can't rename a file if any other process has it open, let alone locked.
  • 2. Re: how to find out if the file is locked
    Johnny_hunter Newbie
    Currently Being Moderated
    my mistake - actually I meant to say the file is opened by another process, not locked. In that case, how can I find out which process opened it? It might help me investigate.

    The app is for Windows.

    Thanks,
    John
  • 3. Re: how to find out if the file is locked
    gimbal2 Guru
    Currently Being Moderated
    Johnny_hunter wrote:
    my mistake - actually I meant to say the file is opened by another process, not locked. In that case, how can I find out which process opened it? It might help me investigate.

    The app is for Windows.

    Thanks,
    John
    This is no longer programming related right? If so, use Microsoft's process explorer.

    http://technet.microsoft.com/en-us/sysinternals/bb896653


    If not: well you are not going to something as operating system specific as that using Java. In fact probably you'll have a hard time doing this using .NET, I did a quick search how to do this with the win32 API and I ended up empty handed :/
  • 4. Re: how to find out if the file is locked
    Johnny_hunter Newbie
    Currently Being Moderated
    thanks gimbal. I tried process explorer before (I did google before I turned to help in here :-)...), it didn't help much then - maybe I should take a 2 ^nd^ look. Anyway.

    I think this still has some relevance to programming, particularly to Swing programming. Please indulge me and let me elaborate:

    - I create a JTable in which a list of files under a certain directory is shown;
    - I selected one of the files and right click to bring up a popup menu;
    - I chose the menu item "Move to folder" to move the select file, the code looks like
     boolean b = new File("C:\\temp\\elephnt.ttf").renameTo(new File("C:\\Users\\John\\Documents\\elephnt.ttf"));
    System.out.println(b);
    Thats where the action fails. My best guess is that the Swing component is holding the file's handle therefore it's qualified as opened by "another process".

    I just don't know how to tackle the issue.

    Thanks,
    John
  • 5. Re: how to find out if the file is locked
    gimbal2 Guru
    Currently Being Moderated
    Did renameTo work at any time for actually moving a file on a Windows system?


    If you simply do a stream copy (inputstream reading, outputstream writing), do you get the same results? Don't forget to create the destination folder if it doesn't exist yet (File.mkdirs()).
  • 6. Re: how to find out if the file is locked
    Johnny_hunter Newbie
    Currently Being Moderated
    Yes. Streaming copy to another folder works. The above renameTo code, when running as a stand along console app, also works. The code didn't work only in the context of the Swing GUI app, in which user is supposed to pick up a file from a list to delete or move.

    John
  • 7. Re: how to find out if the file is locked
    EJP Guru
    Currently Being Moderated
    I think this still has some relevance to programming, particularly to Swing programming
    How can renaming a file have anything to do with Swing programming?
    I chose the menu item "Move to folder" to move the select file, the code looks like
    boolean b = new File("C:\\temp\\elephnt.ttf").renameTo(new File("C:\\Users\\John\\Documents\\elephnt.ttf"));
    How can renaming a file from one constant name to another constant name have anything to do with a 'selected file'?
    The filename must be a variable!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points