Sharkysoft home

lava.io
Class TemporaryFileOutputStream

java.lang.Object
  |
  +--java.io.OutputStream
        |
        +--java.io.FileOutputStream
              |
              +--lava.io.TemporaryFileOutputStream

public class TemporaryFileOutputStream
extends java.io.FileOutputStream

Writes data to temporary file.

Details: A TemporaryFileOutputStream is a specialized FileOutputStream 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 TemporaryFileOutputStream 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 FileOutputStream, 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.

Author:
Sharky

Constructor Summary
TemporaryFileOutputStream()
          Creates an instance that writes to a randomly chosen temporary file.
TemporaryFileOutputStream(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.
 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.FileOutputStream
finalize, getFD, write, write, write
 
Methods inherited from class java.io.OutputStream
flush
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TemporaryFileOutputStream

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

TemporaryFileOutputStream

public TemporaryFileOutputStream(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

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.FileOutputStream
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