Java Reference
In-Depth Information
IndentContLineReader is
=
new
new
IndentContLineReader
(
new
StringReader
(
sampleTxt
));
String aLine
;
// Print Mail/News Header
System
.
out
.
println
(
"----- Message Header -----"
);
while
new
while
((
aLine
=
is
.
readLine
()) !=
null
null
&&
aLine
.
length
() >
0
) {
System
.
out
.
println
(
is
.
getLineNumber
() +
": "
+
aLine
);
}
// Make "is" behave like normal BufferedReader
is
.
setContinuationMode
(
false
false
);
System
.
out
.
println
();
// Print Message Body
System
.
out
.
println
(
"----- Message Body -----"
);
while
while
((
aLine
=
is
.
readLine
()) !=
null
null
) {
System
.
out
.
println
(
is
.
getLineNumber
() +
": "
+
aLine
);
}
is
.
close
();
Each of the
Reader
classes is subclassed from
LineNumberReader
so that you can use
getLineNumber()
. This is a very useful feature when reporting errors back to the user who
prepared an input file; it can save him considerable hunting around in the file if you tell him
the line number on which the error occurred. The
Reader
classes are actually subclassed
class encapsulates the basic functionality for keeping track of lines that need to be joined to-
gether, and for enabling or disabling the continuation processing.
Example 10-8. ContLineReader.java
/**
* Subclass of LineNumberReader, parent of others, to allow reading of
* continued lines using the readLine() method. The other Reader methods
* (readInt()) etc.) must not be used. Must subclass to provide the actual
* implementation of readLine().
*/
public
public abstract
extends
LineNumberReader
{
/** Line number of first line in current (possibly continued) line */
protected
abstract class
class
ContLineReader
ContLineReader
extends
protected
int
int
firstLineNumber
=
0
;
/** True if handling continuations, false if not; false == "PRE" mode */
protected
protected
boolean
boolean
doContinue
=
true
true
;
/** Set the continuation mode */
public
public
void
void
setContinuationMode
(
boolean
boolean
b
) {
doContinue
=
b
;
}