public class DatedFileWriter
extends java.io.Writer
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.
BufferedWriter
,
PrintWriter
,
SimpleDateFormat
Modifier and Type | Class and Description |
---|---|
static class |
DatedFileWriter.Test
For testing.
|
Modifier and Type | Field and Description |
---|---|
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.
|
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
protected java.lang.String lastFileName
protected java.lang.String namePattern
protected java.text.SimpleDateFormat dateFormat
SimpleDateFormat
protected java.io.FileWriter fileWriter
protected boolean append
protected java.lang.String parentPath
public DatedFileWriter(java.lang.String parentPath, java.lang.String namePattern, boolean append)
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.public DatedFileWriter(java.lang.String namePattern)
Use the current directory, appending to the file if it exists.
namePattern
- A String formatted for direct use by SimpleDateFormat to make the file name.public void write(char[] cbuf, int off, int len) throws java.io.IOException
write
in class java.io.Writer
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.java.io.IOException
- If an error occurs during the write.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.Writer
java.io.IOException
public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.Writer
java.io.IOException
Copyright ? 2002 Clarity Systems Group, LLC. All Rights Reserved.