public class PrintWriterWithHistory extends PrintWriter
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.Modifier and Type | Class and Description |
---|---|
protected static class |
PrintWriterWithHistory.NullWriter
This is an inner class that is used for "null" destinations, such as
in the default constructor for PrintWriterWithHistory.
|
out
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
boolean |
getNormalizeLineEndings()
Returns whether this object's history has normalized unix-style
line endings, or the raw line endings generated by printing.
|
void |
println()
Terminate the current line by writing the line separator string.
|
void |
setNormalizeLineEndings(boolean value)
Set whether this object's history will have normalized unix-style
line endings, or the raw line endings generated by printing.
|
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.
|
append, append, append, checkError, clearError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, setError, write, write
public PrintWriterWithHistory()
System.out
instead,
use this:
new PrintWriterWithHistory(System.out);
public PrintWriterWithHistory(Writer out)
out
- A character-output streampublic PrintWriterWithHistory(Writer out, boolean autoFlush)
out
- A character-output streamautoFlush
- A boolean; if true, the println,
printf, or format methods will
flush the output bufferpublic PrintWriterWithHistory(OutputStream out)
PrintWriter
by forcing automatic
line flushing on instead of off, since students most often use it to
pass in something like System.out
.out
- An output streamOutputStreamWriter.OutputStreamWriter(java.io.OutputStream)
public PrintWriterWithHistory(OutputStream out, boolean autoFlush)
out
- An output streamautoFlush
- A boolean; if true, the println,
printf, or format methods will
flush the output bufferOutputStreamWriter.OutputStreamWriter(java.io.OutputStream)
public PrintWriterWithHistory(String fileName) throws FileNotFoundException
OutputStreamWriter
,
which will encode characters using the default charset for this
instance of the Java virtual machine.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.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 fileSecurityException
- If a security manager is present and checkWrite(fileName)
denies write
access to the filepublic PrintWriterWithHistory(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException
OutputStreamWriter
, which will encode characters using the provided
charset.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 charsetFileNotFoundException
- 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 fileSecurityException
- If a security manager is present and checkWrite(fileName)
denies write
access to the fileUnsupportedEncodingException
- If the named charset is not supportedpublic PrintWriterWithHistory(File file) throws FileNotFoundException
OutputStreamWriter
,
which will encode characters using the default charset for this
instance of the Java virtual machine.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.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 fileSecurityException
- If a security manager is present and checkWrite(file.getPath())
denies write access to the filepublic PrintWriterWithHistory(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException
OutputStreamWriter
, which will encode characters using the provided
charset.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 charsetFileNotFoundException
- 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 fileSecurityException
- If a security manager is present and checkWrite(file.getPath())
denies write access to the fileUnsupportedEncodingException
- If the named charset is not supportedpublic String getHistory()
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.public void clearHistory()
getHistory()
.public void setNormalizeLineEndings(boolean value)
value
- If true, the retrieved history will have all line
endings automatically standardized to unix-style
("\n") line endings; if fase, the retrieved history
will be presented without any modifications, using
whatever (platform-specific) line endings were printed
to this object.public boolean getNormalizeLineEndings()
public void write(int c)
write
in class PrintWriter
c
- int specifying a character to be written.public void write(char[] buf, int off, int len)
write
in class PrintWriter
buf
- Array of charactersoff
- Offset from which to start writing characterslen
- Number of characters to writepublic void write(String s, int off, int len)
write
in class PrintWriter
s
- A Stringoff
- Offset from which to start writing characterslen
- Number of characters to writepublic void println()
line.separator
, and is not necessarily a single newline
character ('\n'
).println
in class PrintWriter