com.perdues
Class TextFilter
java.lang.Object
|
+--com.perdues.TextFilter
- public abstract class TextFilter
- extends java.lang.Object
Abstract class that provides a framework for programs
that filter textual input to an output. Subclass it
and define handleLine(String) and a main program to make
a complete command-line application.
Field Summary |
protected java.io.PrintWriter |
w
This variable supports output. |
Method Summary |
void |
filter(java.io.File in,
java.io.File out)
This method opens up a BufferedReader for input from the
given input file and a PrintWriter for output to the given
output file. |
void |
filter(java.lang.String[] args)
Convenience method that calls the filter method with File
arguments. |
void |
handleEOF()
Override this method to define an action to
do at end of input. |
abstract void |
handleLine(java.lang.String line)
Filter calls this on each line of input. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
w
protected java.io.PrintWriter w
- This variable supports output. If the output file is non-null,
it will be open to that file. If the output file is null, this
will be open to System.out.
TextFilter
public TextFilter()
filter
public void filter(java.lang.String[] args)
throws java.io.IOException
- Convenience method that calls the filter method with File
arguments. If there are no arguments or the first argument
is "-", the input is System.in. If there are not two
arguments, the output is System.out. Otherwise input and
output are the files named by the first two elements of the
args array.
filter
public void filter(java.io.File in,
java.io.File out)
throws java.io.IOException
- This method opens up a BufferedReader for input from the
given input file and a PrintWriter for output to the given
output file. If the input file is null, it uses System.in
instead. If the output file is null, this uses System.out
in its place.
This reads lines from input until end of file. On each
line it calls handleLine, passing it the current line.
On end of file or if it catches an exception it closes its input
and output files, except it will not close System.in or System.out.
Since reading of lines is done by BufferedReader.readLine,
there is no detection of an unterminated last input line.
handleLine
public abstract void handleLine(java.lang.String line)
- Filter calls this on each line of input. This
method handles the line, for example writing out
the transformed input.
handleEOF
public void handleEOF()
- Override this method to define an action to
do at end of input. This should not close
the input or output.