CS 1705 Library

net.sf.webcat
Class PrintWriterWithHistory

java.lang.Object
  extended by java.io.Writer
      extended by java.io.PrintWriter
          extended by net.sf.webcat.PrintWriterWithHistory
All Implemented Interfaces:
Closeable, Flushable, Appendable

public class PrintWriterWithHistory
extends PrintWriter

An enhanced version of PrintWriter that provides for a history recall function and some other features making I/O testing a bit easier to perform. See the documentation for PrintWriter for more thorough details on what methods are provided.

Version:
$Id: PrintWriterWithHistory.java,v 1.2 2007/09/15 02:04:16 stedwar2 Exp $
Author:
Stephen Edwards

Constructor Summary
PrintWriterWithHistory()
          Create a new PrintWriter with no destination--useful when you just want to record the history (for testing, for example).
PrintWriterWithHistory(File file)
          Creates a new PrintWriter, without automatic line flushing, with the specified file.
PrintWriterWithHistory(File file, String csn)
          Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
PrintWriterWithHistory(OutputStream out)
          Create a new PrintWriter, with automatic line flushing, from an existing OutputStream.
PrintWriterWithHistory(OutputStream out, boolean autoFlush)
          Create a new PrintWriter from an existing OutputStream.
PrintWriterWithHistory(String fileName)
          Creates a new PrintWriter, without automatic line flushing, with the specified file name.
PrintWriterWithHistory(String fileName, String csn)
          Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
PrintWriterWithHistory(Writer out)
          Create a new PrintWriter, without automatic line flushing.
PrintWriterWithHistory(Writer out, boolean autoFlush)
          Create a new PrintWriter.
 
Method Summary
 void clearHistory()
          Reset this object's history to be empty, just as when the object was first created.
 String getHistory()
          Retrieve the text history of what has been sent to this PrintWriter.
 StringBuffer getHistoryBuffer()
          Retrieve the StringBuffer object used to store this object's text history.
 void println()
          Terminate the current line by writing the line separator string.
 void write(char[] buf, int off, int len)
          Write A Portion of an array of characters.
 void write(int c)
          Write a single character.
 void write(String s, int off, int len)
          Write a portion of a string.
 
Methods inherited from class java.io.PrintWriter
append, append, append, checkError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, write, write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrintWriterWithHistory

public PrintWriterWithHistory()
Create a new PrintWriter with no destination--useful when you just want to record the history (for testing, for example). If you want to create a PrintWriter that points to System.out instead, use this:
 new PrintWriterWithHistory(System.out);
 


PrintWriterWithHistory

public PrintWriterWithHistory(Writer out)
Create a new PrintWriter, without automatic line flushing.

Parameters:
out - A character-output stream

PrintWriterWithHistory

public PrintWriterWithHistory(Writer out,
                              boolean autoFlush)
Create a new PrintWriter.

Parameters:
out - A character-output stream
autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer

PrintWriterWithHistory

public PrintWriterWithHistory(OutputStream out)
Create a new PrintWriter, with automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding. It differs from the corresponding constructor in PrintWriter by forcing automatic line flushing on instead of off, since students most often use it to pass in something like System.out.

Parameters:
out - An output stream
See Also:
OutputStreamWriter.OutputStreamWriter(java.io.OutputStream)

PrintWriterWithHistory

public PrintWriterWithHistory(OutputStream out,
                              boolean autoFlush)
Create a new PrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

Parameters:
out - An output stream
autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer
See Also:
OutputStreamWriter.OutputStreamWriter(java.io.OutputStream)

PrintWriterWithHistory

public PrintWriterWithHistory(String fileName)
                       throws FileNotFoundException
Creates a new PrintWriter, without automatic line flushing, with the specified file name. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.

Parameters:
fileName - The name of the file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
Throws:
FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException - If a security manager is present and checkWrite(fileName) denies write access to the file

PrintWriterWithHistory

public PrintWriterWithHistory(String fileName,
                              String csn)
                       throws FileNotFoundException,
                              UnsupportedEncodingException
Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.

Parameters:
fileName - The name of the file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
csn - The name of a supported charset
Throws:
FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException - If a security manager is present and checkWrite(fileName) denies write access to the file
UnsupportedEncodingException - If the named charset is not supported

PrintWriterWithHistory

public PrintWriterWithHistory(File file)
                       throws FileNotFoundException
Creates a new PrintWriter, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.

Parameters:
file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
Throws:
FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException - If a security manager is present and checkWrite(file.getPath()) denies write access to the file

PrintWriterWithHistory

public PrintWriterWithHistory(File file,
                              String csn)
                       throws FileNotFoundException,
                              UnsupportedEncodingException
Creates a new PrintWriter, without automatic line flushing, with the specified file and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.

Parameters:
file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
csn - The name of a supported charset
Throws:
FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException - If a security manager is present and checkWrite(file.getPath()) denies write access to the file
UnsupportedEncodingException - If the named charset is not supported
Method Detail

getHistory

public String getHistory()
Retrieve the text history of what has been sent to this PrintWriter. This will include all text printed through this object. The clearHistory() method resets the history to be empty, just as when the object was first created. Note that newline characters in the history are always represented by '\n', regardless of what value the system line.separator property has.

Returns:
all the text sent to this PrintWriter

clearHistory

public void clearHistory()
Reset this object's history to be empty, just as when the object was first created. You can access the history using getHistory().


getHistoryBuffer

public StringBuffer getHistoryBuffer()
Retrieve the StringBuffer object used to store this object's text history.

Returns:
The history as a string buffer

write

public void write(int c)
Write a single character.

Overrides:
write in class PrintWriter
Parameters:
c - int specifying a character to be written.

write

public void write(char[] buf,
                  int off,
                  int len)
Write A Portion of an array of characters.

Overrides:
write in class PrintWriter
Parameters:
buf - Array of characters
off - Offset from which to start writing characters
len - Number of characters to write

write

public void write(String s,
                  int off,
                  int len)
Write a portion of a string.

Overrides:
write in class PrintWriter
Parameters:
s - A String
off - Offset from which to start writing characters
len - Number of characters to write

println

public void println()
Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

Overrides:
println in class PrintWriter

Last updated: Wed, Apr 1, 2009 • 12:29 AM EDT

Copyright © 2009 Virginia Tech.