Java Reference
In-Depth Information
20.5.7. String Character Streams
The StringReader reads its characters from a String and will never block.
It provides a single constructor that takes the string from which to read.
For example, the following program factors numbers read either from
the command line or System.in :
class Factor {
public static void main(String[] args) {
if (args.length == 0) {
factorNumbers(new InputStreamReader(System.in));
} else {
for (String str : args) {
StringReader in = new StringReader(str);
factorNumbers(in);
}
}
}
// ... definition of factorNumbers ...
}
If the command is invoked without parameters, factorNumbers parses
numbers from the standard input stream. When the command line con-
tains some arguments, a StringReader is created for each parameter, and
factorNumbers is invoked on each one. The parameter to factorNumbers is a
stream of characters containing numbers to be parsed; it does not know
whether they come from the command line or from standard input.
StringWriter lets you write results into a buffer that can be retrieved as
a String or StringBuffer object. It adds the following constructors and
methods:
public StringWriter()
Creates a new StringWriter with a default initial buffer size.
 
Search WWH ::




Custom Search