Re: crash during file writing, how to recover ?

From: Calum (calum.bulk_at_ntlworld.com)
Date: 05/02/04


Date: Sat, 01 May 2004 23:59:52 +0100

Chris Smith wrote:

> Not surprisingly, it's not specified whether File.renameTo results in a
> call to the rename system call. More surprisingly, it's not specified
> whether File.renameTo will succeed if a file already exists by the
> target name. That said, renameTo returns a success flag (which is ugly
> in Java, but nevertheless happens). So it's entirely possible to write:
>
> if (!newFile.renameTo(fileBeingProcessed))
> {
> fileBeingProcessed.delete();
> newFile.renameTo(fileBeingProcessed);
> }

Just to be inconsistent, the Microsoft "rename" function specifically
requires "The new name must not be the name of an existing file or
directory". It makes some sense - I wouldn't expect a function called
"rename" to delete a file, but I can see situations where either
behaviour would be desired.

So it's possible Java will have different behaviour on Windows and
POSIX, though I can't be bothered to check this.

I guess for ultra-safety, the old file could be renamed to something
else, before renaming the new file to the target filename. Of course
then you'd just clutter up the directory with old files, but there are
circumstances where you want to be able to roll back.

Calum



Relevant Pages

  • Re: crash during file writing, how to recover ?
    ... Nevertheless, some clarification about Java: ... that deal with directory management are ... perfect sense because they don't operate on a file descriptor, ... call to the rename system call. ...
    (comp.lang.java)
  • Re: crash during file writing, how to recover ?
    ... Nevertheless, some clarification about Java: ... that deal with directory management are ... perfect sense because they don't operate on a file descriptor, ... call to the rename system call. ...
    (comp.programming)
  • Re: crash during file writing, how to recover ?
    ... Nevertheless, some clarification about Java: ... that deal with directory management are ... perfect sense because they don't operate on a file descriptor, ... call to the rename system call. ...
    (comp.lang.java.programmer)
  • Re: crash during file writing, how to recover ?
    ... Nevertheless, some clarification about Java: ... that deal with directory management are ... perfect sense because they don't operate on a file descriptor, ... call to the rename system call. ...
    (comp.os.linux.development.apps)
  • Re: Newbie question on renaming
    ... > Java, I'm pretty lazy about it: I rename the variable where it's defined, ... int doSomething() { ... life a bit easier in the Java world. ... with less variables in Lisp, and techniques, similar to those described by ...
    (comp.lang.lisp)