Sharkysoft home

lava.io
Class TemporaryFileWriter

java.lang.Object
  |
  +--java.io.Writer
        |
        +--java.io.OutputStreamWriter
              |
              +--java.io.FileWriter
                    |
                    +--lava.io.TemporaryFileWriter

public class TemporaryFileWriter
extends java.io.FileWriter

Writes data to temporary file.

Details: A TemporaryFileWriter is a specialized FileWriter that automatically stores its output in a temporary file. This class is useful for situation where data must be saved, but the choice of filename is inconsequential. TemporaryFileWriter always attempts to use a filename that is not already in use.

After a TemporaryFileWriter has been closed, the temporary file can be renamed, possibly overwriting the contents of a destination file. Thus, this class is ideal for applications which must read and rewrite files.

Note: Don't forget that because this class is a subclass of FileWriter, all of the superclass' methods are available in this class.

Note: It appears the people at Javasoft liked this idea. Similar features have now been incorporated into the 1.2 release of Java. (Just remember where it appeared first! :-) This class remains a part of Lava, however, for backword compatibility with existing programs and also to provide continued support for JDK 1.1. Even 1.2 users may find this class to be a more flexible solution.

Version:
2001.04.05
Author:
Sharky

Fields inherited from class java.io.Writer
lock
 
Constructor Summary
TemporaryFileWriter()
          Creates an instance that writes to a randomly chosen temporary file.
TemporaryFileWriter(java.io.File rename_dest)
          Creates an instance that writes to a randomly chosen temporary file in the same directory as the given file.
 
Method Summary
 void close()
          Closes the temporary file and, if a rename destination was specified when this instance was constructed, attempts to rename it.
 void closeAndDelete()
          Closes and deletes temporary file.
 java.io.File closeAndRename(java.io.File new_file)
          Closes the output stream and optionally renames the temporary file.
 java.io.File getFile()
          Returns a File object representing this stream's temporary file.
 
Methods inherited from class java.io.OutputStreamWriter
flush, getEncoding, write, write, write
 
Methods inherited from class java.io.Writer
write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TemporaryFileWriter

public TemporaryFileWriter()
                    throws java.io.IOException
Creates an instance that writes to a randomly chosen temporary file.

TemporaryFileWriter

public TemporaryFileWriter(java.io.File rename_dest)
                    throws java.io.IOException
Creates an instance that writes to a randomly chosen temporary file in the same directory as the given file. If rename_dest is a file, the temporary file will be renamed to that location when it is closed, and the destination, if it exists, will be overwritten. If the given file is a directory, however, then rename_dest will only be used to determine which directory to store the temporary file in, and the caller will be responsible for eventually renaming (or deleting) the file on his own.
Method Detail

closeAndRename

public java.io.File closeAndRename(java.io.File new_file)
                            throws java.io.IOException
Closes the output stream and optionally renames the temporary file. If the destination file already exists, it will be replaced. If no destination file is specified, then the temporary file will not be renamed. In either case, a File object representing the completed temporary file will be returned. It is OK to call this method after the close method, but it is not necessary.

Tip: It may not be possible to rename the temporary file if the destination file is in use. If you are using this class to rewrite a file, make sure you close the source before you close this destination, so that the source file will not be considered "in use."

Parameters:
new_file - a File object representing the new filename for the completed temporary file, or null if the file should not be renamed
Returns:
a File object representing the completed file

closeAndDelete

public void closeAndDelete()
                    throws java.io.IOException
Closes and deletes temporary file.

Details: closeAndDelete closes the output stream and deletes the temporary file.

Since:
2001.04.05

close

public void close()
           throws java.io.IOException
Closes the temporary file and, if a rename destination was specified when this instance was constructed, attempts to rename it. If a file exists by the same name as the rename destination, it will be overwritten.
Overrides:
close in class java.io.OutputStreamWriter
Throws:
java.io.IOException - if an I/O error occurs

getFile

public final java.io.File getFile()
                           throws java.io.IOException
Returns a File object representing this stream's temporary file. This method can only be called after the close or closeAndRename method has been called.
Returns:
the temporary file

Sharkysoft home