Does file renaming always fail if the file is shared?

Discussion in 'Java' started by Chris, Mar 6, 2007.

  1. Chris

    Chris Guest

    Suppose one process has a file open, and another process attempts to
    rename the file. Is File.rename() always guaranteed to return false in
    that case?

    Does it at least work across all major operating systems?
    Chris, Mar 6, 2007
    #1
    1. Advertising

  2. On Mon, 05 Mar 2007 21:25:24 -0600, Chris wrote:
    > Suppose one process has a file open, and another process attempts to
    > rename the file. Is File.rename() always guaranteed to return false in
    > that case?
    >
    > Does it at least work across all major operating systems?


    On many operating systems, it is possible to rename or even delete a
    file while it is open by another process.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Mar 6, 2007
    #2
    1. Advertising

  3. Hi,

    Chris wrote:
    > Suppose one process has a file open, and another process attempts to
    > rename the file. Is File.rename() always guaranteed to return false in
    > that case?
    >
    > Does it at least work across all major operating systems?


    Depends what you call "all major operating systems". If you think of
    about 90+% of all desktop PCs, then you are right: I suppose, that 90+%
    of all desktop PCs either run with Windows XP, Windows 2000 or Windows
    95. There it 'works' (which means that you *cannot* rename a 'shared' file).

    However, using Linux or Unix, it does *not* work (which means that you
    *can* rename the file :)

    I'm not sure about the other 'exoticals' (like BeOS, Mac, and so on ;-)

    Ciao,
    Ingo
    Ingo R. Homann, Mar 6, 2007
    #3
  4. Chris

    Jason Cavett Guest

    On Mar 6, 3:33 am, "Ingo R. Homann" <> wrote:
    > Hi,
    >
    > Chris wrote:
    > > Suppose one process has a file open, and another process attempts to
    > > rename the file. Is File.rename() always guaranteed to return false in
    > > that case?

    >
    > > Does it at least work across all major operating systems?

    >
    > Depends what you call "all major operating systems". If you think of
    > about 90+% of all desktop PCs, then you are right: I suppose, that 90+%
    > of all desktop PCs either run with Windows XP, Windows 2000 or Windows
    > 95. There it 'works' (which means that you *cannot* rename a 'shared' file).
    >
    > However, using Linux or Unix, it does *not* work (which means that you
    > *can* rename the file :)
    >
    > I'm not sure about the other 'exoticals' (like BeOS, Mac, and so on ;-)
    >
    > Ciao,
    > Ingo


    Windows 95

    :: makes a face ::

    Anyway, that's one question I've always had - shouldn't Java work the
    same way across all OSes? Or does the way Java reacts depend on the
    underlying OS (such as renaming a file via Windows vs. Linux).
    Jason Cavett, Mar 6, 2007
    #4
  5. Chris

    Chris Smith Guest

    Jason Cavett <> wrote:
    > Anyway, that's one question I've always had - shouldn't Java work the
    > same way across all OSes? Or does the way Java reacts depend on the
    > underlying OS (such as renaming a file via Windows vs. Linux).


    Java has to balance portability (working the same across all operating
    systems) with usability (working the way users of the operating system
    would expect). Perhaps it would be possible for Java to invent a scheme
    to prevent itself from deleting an open file on Linux; but Linux users,
    who are by and large knowledgable about how UNIX typically manages a
    filesystem, would not expect it to do so. Using File in creative ways
    is not transparently portable anyway (e.g., permissions issues), it
    makes sense that Java would choose the other goal here.

    --
    Chris Smith
    Chris Smith, Mar 6, 2007
    #5
  6. Hi Jason,

    Jason Cavett wrote:
    > Anyway, that's one question I've always had - shouldn't Java work the
    > same way across all OSes? Or does the way Java reacts depend on the
    > underlying OS (such as renaming a file via Windows vs. Linux).


    Well, as Chris said: It has to be well-balanced.

    To be extreme: Would you want to forbid 'System.getProperty("os.name")'
    because it reacts differently on different OSs? Do you want to generally
    forbid File-IO, because there are some embedded systems that do not have
    something like a Harddisk (not to mention applets or webstart)? Should
    the maximum size of a window that Java can use, be restricted to 80*40
    pixel because that is the only resolution, that *every* hardware (even
    mobile phones) supports? Should we forbid network-support?

    The other extreme: Should sun invest some months of manpower (not to
    mention legal problems) to 'hack' Windows so that Java can rename a file
    that is opened by another program? Or should it hack Unix and Linux so
    that the File can *not* be renamed, although it would be supported by
    the OS?

    I think the balance between the two extremes should be practical. And
    that is what's happening at the moment. :)

    Ciao,
    Ingo
    Ingo R. Homann, Mar 6, 2007
    #6
  7. Chris

    Chris Uppal Guest

    Chris wrote:

    > Suppose one process has a file open, and another process attempts to
    > rename the file. Is File.rename() always guaranteed to return false in
    > that case?


    Nope.

    > Does it at least work across all major operating systems?


    Windows and Unix behave differently in this (and it's part of the standard
    semantics of their respective file-systems -- nothing Java specific).

    -- chris
    Chris Uppal, Mar 6, 2007
    #7
    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. Natan Vivo
    Replies:
    1
    Views:
    630
    Patrick.O.Ige
    Oct 31, 2005
  2. Deryck
    Replies:
    4
    Views:
    508
    derek giroulle
    Jun 22, 2004
  3. Wenjie

    if (f() != FAIL) or if (FAIL != f())?

    Wenjie, Jul 28, 2003, in forum: C Programming
    Replies:
    3
    Views:
    430
    E. Robert Tisdale
    Jul 31, 2003
  4. Douglass Turner
    Replies:
    2
    Views:
    2,016
    Manfred Bartz
    Sep 4, 2003
  5. gd
    Replies:
    4
    Views:
    113
    Tintin
    Feb 6, 2006
Loading...

Share This Page