com.claritysys.io
Class DatedFileWriter

java.lang.Object
  extended by java.io.Writer
      extended by com.claritysys.io.DatedFileWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class DatedFileWriter
extends java.io.Writer

Provides an easy way to write log files which follow a dated naming scheme, such as 'batch_19990527'.

Once you create one, you can wrap it in a BufferedWriter with a PrintWriter above that, like this:

 PrintWriter logWriter = new PrintWriter (new BufferedWriter (new DatedFileWriter ("log_YYYYmmdd")));
 
Whenever the writer is flushed, the DatedFileWriter will format the current system time into the file name format, open the file of that name, write the text to it and flush it to disk.

If the file name has changed since the last write, the file is closed and the new file (such as when the date rolls over) is created.

Version:
$Revision: 2348 $ $Date: 2004-12-01 16:12:21 -0800 (Wed, 01 Dec 2004) $
See Also:
BufferedWriter, PrintWriter, SimpleDateFormat

Nested Class Summary
static class DatedFileWriter.Test
          For testing.
 
Field Summary
protected  boolean append
          True if we are to open files in append mode.
protected  java.text.SimpleDateFormat dateFormat
          The SimpleDateFormat makes it easy to produce the pattern string, the actual name pattern is passed right to it.
protected  java.io.FileWriter fileWriter
          Null if not open, else used to access the underlying file.
protected  java.lang.String lastFileName
          The name of the file we last wrote to or null if we haven't written to one yet.
protected  java.lang.String namePattern
          The original name pattern passed in.
protected  java.lang.String parentPath
          The parent directory to create files in.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
DatedFileWriter(java.lang.String namePattern)
          Create a new DatedFileWriter using the current directory and the given file name pattern.
DatedFileWriter(java.lang.String parentPath, java.lang.String namePattern, boolean append)
          Create a new DatedFileWriter using the given parent directory and the given file name pattern.
 
Method Summary
 void close()
          Write the given sub-array to the file.
 void flush()
          Write the given sub-array to the file.
 void write(char[] cbuf, int off, int len)
          Write the given sub-array to the file.
 
Methods inherited from class java.io.Writer
append, append, append, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lastFileName

protected java.lang.String lastFileName
The name of the file we last wrote to or null if we haven't written to one yet.


namePattern

protected java.lang.String namePattern
The original name pattern passed in.


dateFormat

protected java.text.SimpleDateFormat dateFormat
The SimpleDateFormat makes it easy to produce the pattern string, the actual name pattern is passed right to it.

See Also:
SimpleDateFormat

fileWriter

protected java.io.FileWriter fileWriter
Null if not open, else used to access the underlying file. Note: I've noticed that if a file is deleted or renamed while open no exception is thrown at least on Solaris 2.6. Perhaps this should periodically (writes > 30,000 millis apart?) close and reopen the FileWriter. And maybe a OS person could explain this strange behavior and how to avoid it!


append

protected boolean append
True if we are to open files in append mode.


parentPath

protected java.lang.String parentPath
The parent directory to create files in. If null, use current directory.

Constructor Detail

DatedFileWriter

public DatedFileWriter(java.lang.String parentPath,
                       java.lang.String namePattern,
                       boolean append)
Create a new DatedFileWriter using the given parent directory and the given file name pattern.

Parameters:
parentPath - The parent directory to create files in. If null, use current directory.
namePattern - A String formatted for direct use by SimpleDateFormat to make the file name.
append - Set to true to append to a pre-existing log, false to overwrite.

DatedFileWriter

public DatedFileWriter(java.lang.String namePattern)
Create a new DatedFileWriter using the current directory and the given file name pattern.

Use the current directory, appending to the file if it exists.

Parameters:
namePattern - A String formatted for direct use by SimpleDateFormat to make the file name.
Method Detail

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws java.io.IOException
Write the given sub-array to the file.

Specified by:
write in class java.io.Writer
Parameters:
cbuf - The character array to write chars from.
off - The offset into the char array to begin writing from.
len - The number of characters to write.
Throws:
java.io.IOException - If an error occurs during the write.

close

public void close()
           throws java.io.IOException
Write the given sub-array to the file.

Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Writer
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Write the given sub-array to the file.

Specified by:
flush in interface java.io.Flushable
Specified by:
flush in class java.io.Writer
Throws:
java.io.IOException


Copyright ? 2002 Clarity Systems Group, LLC. All Rights Reserved.