com.darwinsys.io
Class FileSaver

java.lang.Object
  extended by com.darwinsys.io.FileSaver

public class FileSaver
extends java.lang.Object

Save a user data file, as safely as we can. The basic algorithm is:

  1. We create a temporary file, in the same directory as the input file so we can safely rename it. Set it to with deleteOnExit(true);
  2. Our client writes the user data to this file. Data format or translation errors, if any, will be thrown during this process, leaving the user's original file intact. Client closes file.
  3. We delete the previous backup file, if one exists;
  4. We rename the user's previous file to filename.bak;
  5. We rename the temporary file to the save file.
This algorithm all but guarantees not to fail for reasons of disk full, permission denied, etc. Alternate algorithms could be employed that would preserve the original file ownership and permissions (e.g., on POSIX filesystems) but they can not then guarantee not to fail due to disk full conditions.

Step 1 is implemented in the constructor. Step 2 you do, by calling getWriter or getOutputStream (not both). Step 3, 4 and 5 are done in finish().

Normal usage is thus:

 try {
        FileSaver saver = new FileSaver(file);
        final Writer writer = saver.getWriter();
        PrintWriter out = new PrintWriter(writer);
        myWriteOutputFile(out);
        out.close();
        saver.finish();
        System.out.println("Saved OK");
 } catch (IOException e) {
        System.out.println("Save FAILED");
 }
 

Objects of this class may be re-used sequentially (for the same file) but are not thread-safe and should not be shared among different threads.

Author:
Extracted and updated by Ian Darwin from an older application, prompted by discussion started by Brendon McLean on a private mailing list.

Constructor Summary
FileSaver(java.io.File input)
           
 
Method Summary
 void finish()
          Close the output file and rename the temp file to the original name.
 java.io.File getFile()
          Return a reference to the contained File object, to promote re-use (File objects are immutable so this is at least moderately safe).
 java.io.OutputStream getOutputStream()
          Return an output file that the client should use to write the client's data to.
 java.io.Writer getWriter()
          Return an output file that the client should use to write the client's data to.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileSaver

public FileSaver(java.io.File input)
          throws java.io.IOException
Throws:
java.io.IOException
Method Detail

getFile

public java.io.File getFile()
Return a reference to the contained File object, to promote re-use (File objects are immutable so this is at least moderately safe). Typical use would be:
 if (fileSaver == null ||
   !(fileSaver.getFile().equals(file))) {
                fileSaver = new FileSaver(file);
 }
 


getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws java.io.IOException
Return an output file that the client should use to write the client's data to.

Returns:
An OutputStream, which should be wrapped in a buffered OutputStream to ensure reasonable performance.
Throws:
java.io.IOException - if the temporary file cannot be written

getWriter

public java.io.Writer getWriter()
                         throws java.io.IOException
Return an output file that the client should use to write the client's data to.

Returns:
A Writer, which should be wrapped in a buffered Writer to ensure reasonable performance.
Throws:
java.io.IOException - if the temporary file cannot be written

finish

public void finish()
            throws java.io.IOException
Close the output file and rename the temp file to the original name.

Throws:
java.io.IOException - If anything goes wrong


Copyright © 1996-2004 Ian F. Darwin. See license.html for usage license.