Design Patterns Java™ Workbook

(Michael S) #1
Chapter 27. Decorator

while (true)
{
String s = in.readLine();
if (s == null)
{
break;
}
out.write(s + "\n");
}
out.close();
in.close();
}
}


The BufferedReader class provides the ability to read one line at a time from a file. The
ShowFilters program creates a FileWriter object and decorates it with behaviors to
wrap the text at 40 characters and to titlecase the text. Suppose that a file named demo.txt
contains:


The "SPACESHOT" shell hovers
at 100 meters for 2 to 3
minutes, erupting star bursts every 10 seconds that
generate abundant reading-level light for a
typical stadium.


Running this file through the ShowFilters program cleans up the text:



java -classpath \oozinoz\classes com.oozinoz.applica-
tions.ShowFilters demo.txt demo-out.txt
type demo-out.txt



The "Spaceshot" Shell Hovers At 100
Meters For 2 To 3 Minutes, Erupting Star
Bursts Every 10 Seconds That Generate
Abundant Reading-level Light For A
Typical Stadium.


CHALLENGE 27.1


If you want to direct output to System.outinstead of to a file, you can create
a Writer object that directs its output to System.out:

Writer out = new PrintWriter(System.out);
Write a snippet of code to define a Writer object that wraps text at 15 characters,
centers the text, sets the text to random casing, and directs the output to
System.out.

The code for most of the filter classes is simple. For example:

Free download pdf