|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.io.Writer
wt.util.OutputFile
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.
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 |
protected static final Hashtable allInstances
protected String fileName
protected String mergeFileName
protected boolean removeMergeFile
protected WriteMode writeMode
protected String encoding
protected PrintWriter writer
protected File backup
protected boolean keepBackup
protected static final boolean readOnlyWarning
Constructor Detail |
protected OutputFile()
Method Detail |
protected static String backUpName(String fileName)
fileName
- the name of the file, for which a backup filename is needed
String
that is the backup filenamepublic void close()
OutputFile
. If applicable, will delete a temporary backup file.
public void close(boolean success)
OutputFile
.
If applicable, will delete a temporary backup file, or restore it if passed
success=false.
protected boolean restoreBackupFile(File source)
File
object.
source
- the File
which needs to be
restored from a backup File
.
protected boolean createBackupFile(File source)
File
object.
source
- the File
which needs a backup File
.
public static boolean ensureDirectoryExists(String dir_name) throws FileNotFoundException, IOException
dir_name
- the name of the directory that is needed
FileNotFoundException
IOException
public void flush()
public static OutputFile get(String theFileName)
OutputFile
object for the filenamepublic static Enumeration getAllFiles()
Enumeration
of all the OutputFile
objects
that have been successfully requested.
Enumeration
of all the OutputFile
objectspublic String getFileName()
public WriteMode getWriteMode()
WriteMode
setting for this object.
WriteMode
setting for this objectpublic PrintWriter getWriter()
protected boolean mergePreparation(File file)
File
.
This method just returns true, but exists as a means of sub-classes
providing specific merge preparation behavior.
file
- the File
which will be merged.
public boolean open()
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.
WriteMode
public void print(String string)
public void println(String string)
public void setEncoding(String encoding)
encoding
- the encoding to useprotected void setFileName(String newFileName)
OutputFile
.
Only takes effect if file is opened after setting.
newFileName
- the filename to write to.public String getMergeFileName()
public void setMergeFileName(String newMergeFileName)
public void setRemoveMergeFile(boolean newRemoveMergeFile)
newRemoveMergeFile
- true will remove the file, false will leave itpublic void setWriteMode(WriteMode newWriteMode)
WriteMode
for the OutputFile
.
Only takes effect if file is opened after setting.
newWriteMode
- the WriteMode
to usepublic String toString()
public void write(char[] cbuf, int off, int len)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |