Java Reference
In-Depth Information
This application is an example of using more than one filter on a stream. The stream is
developed in a three-step process:
1. A file output stream associated with a file called 400primes.dat is created.
2. A new buffered output stream is associated with the file stream.
3. A new data output stream is associated with the buffered stream.
The writeInt() method of the data stream is used to write the primes to the file.
The PrimeReader application is simpler because it doesn't need to do anything regarding
prime numbers—it just reads integers from a file using a data input stream.
Lines 6-11 of PrimeReader are nearly identical to statements in the PrimeWriter appli-
cation, except that input classes are used instead of output classes.
The try - catch block that handles EOFException objects is in lines 13-20. The work of
loading the data takes place inside the try block.
The while(true) statement creates an endless loop. This isn't a problem; an
EOFException automatically occurs when the end of the stream is encountered at some
point as the data stream is being read. The readInt() method in line 15 reads integers
from the stream.
The last several output lines of the PrimeReader application should resemble the
following:
2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 22
51 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347
2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2
441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 255
7 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677
2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741
Character Streams
After you know how to handle byte streams, you have most of the skills needed to han-
dle character streams as well. Character streams are used to work with any text repre-
sented by the ASCII character set or Unicode, an international character set that includes
ASCII.
Examples of files that you can work with through a character stream are plain text files,
Hypertext Markup Language (HTML) documents, and Java source files.
 
Search WWH ::




Custom Search