Java Tutorial - Java Script : Data Streams

Java Tutorial - Java Script :

Data Streams

If you need to work with data that isn’t represented as bytes or characters, you can use data input and data output streams. These streams filter an existing byte stream so that each of the following primitive types can be read or written directly from the stream: boolean, byte, double, float, int, long, and short.A data input stream is created with the DataInputStream(InputStream) constructor. The argument should be an existing input stream such as a buffered input stream or a file input stream. A data output stream requires the DataOutputStream(OutputStream) constructor, which indicates the associated output stream. The following list indicates the read and write methods that apply to data input and output streams, respectively:
·         readBoolean(), writeBoolean(boolean)
·         readByte(), writeByte(integer)
·         readDouble(), writeDouble(double
·         readFloat(), writeFloat(float)
·         readInt(), writeInt(int)
·         readLong(), writeLong(long)
·         readShort(), writeShort(int)
 Each input method returns the primitive data type indicated by the name of the method. For example, the readFloat() method returns a float value. There also are readUnsignedByte() and readUnsignedShort() methods that read in unsigned byte and short values. These are not data types supported by Java, so they are returned as int values.
A data input stream’s different read methods do not all return a value that can be used as an indicator that the end of the stream has been reached As an alternative, you can wait for an EOFException (end-of-file exception) to be thrown when a read method reaches the end of a stream. The loop that reads the data can be enclosed in a try block, and the associated catch statement should handle only EOFException objects. You can call close() on the stream and take care of other cleanup tasks inside the catch block.
This is demonstrated in the next project. Listings 15.5 and 15.6 contain two programs that use data streams. The PrimeWriter application writes the first 400 prime numbers as integers to a file called 400primes.dat. The PrimeReader application reads the integers from this file and displays them.
Most of the PrimeWriter application is taken up with logic to find the first 400 prime numbers. After you have an integer array containing the first 400 primes, it is written to a data output stream in lines 17–31. 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 application, 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 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 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