wt.util
Class OutputFile

java.lang.Object
  extended byjava.io.Writer
      extended bywt.util.OutputFile
Direct Known Subclasses:
MergableWriterFile

public class OutputFile
extends Writer

OutputFile is a wrapper class for writing output to a file. It provides the ability for multiple user's of this class to request a file by name, and get a "handle" to the same file.

Depending on the WriteMode that is set, the open method will inspect the current situation, and fail or succeed. The open method will also create appropriate backup files.

The default mode is WriteMode.BACKUP, meaning a backup of an existing file will be created, with the last character of its name changed to '~'.

The following is an example of how OutputFile could be used:

    // obtain a handle to a particular output file
    OutputFile myOutputFile = OutputFile.get( "c:\temp\myFile.txt" );
 

// if the file already exists, open() creates a backup named "c:\temp\myFile.tx~" if ( myOutputFile.open() ) { myOutputFile.println( "This is a test." ); myOutPutFile.close(); }

If the file open attempt results in a SecurityException, due to be executed in a browser client, the output writes will be redirected to System.out.

See Also:
WriteMode, PrintWriter

Field Summary
protected static Hashtable allInstances
           
protected  File backup
           
protected  String encoding
           
protected  String fileName
           
protected  boolean keepBackup
           
protected  String mergeFileName
           
protected static boolean readOnlyWarning
           
protected  boolean removeMergeFile
           
protected  WriteMode writeMode
           
protected  PrintWriter writer
           
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
protected OutputFile()
           
 
Method Summary
protected static String backUpName(String fileName)
          Obtain a backup filename, based on another filename.
 void close()
          Close the OutputFile.
 void close(boolean success)
          Close the OutputFile.
protected  boolean createBackupFile(File source)
          Create a backup file, given a source File object.
static boolean ensureDirectoryExists(String dir_name)
          Ensure a directory exists.
 void flush()
          Flush the stream.
static OutputFile get(String theFileName)
          Obtain an instance of an OutputFile, given a filename.
static Enumeration getAllFiles()
          Obtain an Enumeration of all the OutputFile objects that have been successfully requested.
 String getFileName()
           
 String getMergeFileName()
          Gets the filename that will provide input for merging.
 WriteMode getWriteMode()
          Obtain the WriteMode setting for this object.
 PrintWriter getWriter()
           
protected  boolean mergePreparation(File file)
          Perform necessary preparations for merging with the File.
 boolean open()
          Opens the OutputFile, so that print and println can be performed.
 void print(String string)
          Print a String.
 void println(String string)
          Print a String, and then finish the line.
protected  boolean restoreBackupFile(File source)
          Restore a backup file, given a source File object.
 void setEncoding(String encoding)
          Set the encoding for this file.
protected  void setFileName(String newFileName)
          Set the filename for the OutputFile.
 void setMergeFileName(String newMergeFileName)
          Set the filename that will provide input for merging.
 void setRemoveMergeFile(boolean newRemoveMergeFile)
          Set the switch to determine if the merge input file should be removed when done with it.
 void setWriteMode(WriteMode newWriteMode)
          Set the WriteMode for the OutputFile.
 String toString()
           
 void write(char[] cbuf, int off, int len)
          Write a portion of an array of characters.
 
Methods inherited from class java.io.Writer
write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

allInstances

protected static final Hashtable allInstances

fileName

protected String fileName

mergeFileName

protected String mergeFileName

removeMergeFile

protected boolean removeMergeFile

writeMode

protected WriteMode writeMode

encoding

protected String encoding

writer

protected PrintWriter writer

backup

protected File backup

keepBackup

protected boolean keepBackup

readOnlyWarning

protected static final boolean readOnlyWarning
Constructor Detail

OutputFile

protected OutputFile()
Method Detail

backUpName

protected static String backUpName(String fileName)
Obtain a backup filename, based on another filename.

Parameters:
fileName - the name of the file, for which a backup filename is needed
Returns:
the String that is the backup filename

close

public void close()
Close the OutputFile. If applicable, will delete a temporary backup file.


close

public void close(boolean success)
Close the OutputFile. If applicable, will delete a temporary backup file, or restore it if passed success=false.


restoreBackupFile

protected boolean restoreBackupFile(File source)
Restore a backup file, given a source File object.

Parameters:
source - the File which needs to be restored from a backup File.
Returns:
true if it succeeds, otherwise return false

createBackupFile

protected boolean createBackupFile(File source)
Create a backup file, given a source File object.

Parameters:
source - the File which needs a backup File.
Returns:
true if it succeeds, otherwise return false

ensureDirectoryExists

public static boolean ensureDirectoryExists(String dir_name)
                                     throws FileNotFoundException,
                                            IOException
Ensure a directory exists.

Parameters:
dir_name - the name of the directory that is needed
Throws:
FileNotFoundException
IOException

flush

public void flush()
Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.


get

public static OutputFile get(String theFileName)
Obtain an instance of an OutputFile, given a filename. Multiple requests for the same filename, will return the same instance.

Returns:
the OutputFile object for the filename

getAllFiles

public static Enumeration getAllFiles()
Obtain an Enumeration of all the OutputFile objects that have been successfully requested.

Returns:
the Enumeration of all the OutputFile objects

getFileName

public String getFileName()

getWriteMode

public WriteMode getWriteMode()
Obtain the WriteMode setting for this object.

Returns:
the WriteMode setting for this object

getWriter

public PrintWriter getWriter()

mergePreparation

protected boolean mergePreparation(File file)
Perform necessary preparations for merging with the File. This method just returns true, but exists as a means of sub-classes providing specific merge preparation behavior.

Parameters:
file - the File which will be merged.
Returns:
true if it succeeds, otherwise return false

open

public boolean open()
Opens the OutputFile, so that print and println can be performed. The WriteMode is considered to deterimine the exact actions to take.
   WriteMode.BACKUP    = Create a backup of any existing file, before re-writing it.
   WriteMode.MERGE     = Merge into an existing file.
   WriteMode.ORIGINAL  = Only open the file, if it doesn't already exist.
   WriteMode.OVERWRITE = Clobber an existing file.
 
*Assumes source mergeFile and target output file will be in the same path.

Returns:
true if it succeeds, otherwise return false
See Also:
WriteMode

print

public void print(String string)
Print a String.


println

public void println(String string)
Print a String, and then finish the line.


setEncoding

public void setEncoding(String encoding)
Set the encoding for this file. Only takes effect if file is opened after setting.

Parameters:
encoding - the encoding to use

setFileName

protected void setFileName(String newFileName)
Set the filename for the OutputFile. Only takes effect if file is opened after setting.

Parameters:
newFileName - the filename to write to.

getMergeFileName

public String getMergeFileName()
Gets the filename that will provide input for merging.


setMergeFileName

public void setMergeFileName(String newMergeFileName)
Set the filename that will provide input for merging. Only takes effect if file is opened after setting.


setRemoveMergeFile

public void setRemoveMergeFile(boolean newRemoveMergeFile)
Set the switch to determine if the merge input file should be removed when done with it. Only takes effect if file is opened after setting.

Parameters:
newRemoveMergeFile - true will remove the file, false will leave it

setWriteMode

public void setWriteMode(WriteMode newWriteMode)
Set the WriteMode for the OutputFile. Only takes effect if file is opened after setting.

Parameters:
newWriteMode - the WriteMode to use

toString

public String toString()

write

public void write(char[] cbuf,
                  int off,
                  int len)
Write a portion of an array of characters.