More on I/O Streams

IO

Different versions of the Core Java texts have the "Streams and Files" chapter in different volumes. (Newer editions have it in Volume 1.) They, too, note that I/O is "not particularly exciting, but without the ability to read and write data, your programs are severely limited."
In general, don't forget that the text is a valuable source of information and examples.

OutputStream

write()

flush(): "because you do not know what an output stream is connected to, you might be required to "flush" your output through buffered cache to get it to be written (in a timely matter, or at all). OutputStream's version of this method does nothing..."

BufferedOutputStream and PrintStream override this method... later

close(): "just like for an InputStream you should explicitly close an OutputStream so that it can release any resources it may have reserved in your behalf."

Opening a FileOutputStream

Same as for FileInput

The following is an example program that asks the user for a list of names and phone numbers. Each name and number is added to a file at a prespecified location.

To indicate that the entire list has been entered, the user specifies "Done" at the "Name?" prompt.

Code:

To test this, be careful that you do not have a file called "phone.numbers", the program will either create a file called "phone.numbers" or writeover the one that is there.

The program will create an output file that can be displayed onscreen or printed.

898-5998, keuneke

456-0945, sam

344-5643, molly

FilterOutputStream

This 'abstract' class simply provides a 'pass-through' for all of the standard methods of OutputStream. It holds inside itself another stream, by definition one further 'down' the chain of filters, to which it forwards all method calls. It implements nothing new but allows itself to be nested:

OutputStream s = new FileOutputStream("foo");

FilterOutputStream s1 = new FilterOutputStream(s);

FilterOutputStream s2 = new FilterOutputStream(s1);

FilterOutputStream s3 = new FilterOutputStream(s2);

...s3.write(123) ...

Whenever a write is performed on the filtered stream s3, it passes along the request to s2. Then s2 does the same to s1, and finally s is asked to output the bytes. Subclasses of FilterOutputStream of course, do some nontrivial processing of the bytes as they flow past. This chain can be tightly nested"

s3 = new FilterOutputStream(new FilterOutputStream(new FilterOutputStream(s)));

BufferedOutputStream

(For characters, see BufferedWriter. Also, for primitive data types, remember Scanner ( API)

Output efficiency is increased by storing values to be written in a buffer and actually writing them only when the buffer fills up or when flush() is called.

FromJava in 21 days, page 395:

"BufferedOutputStream is one of the most valuable of all streams. All it does is implement the full complement of OutputStream's methods, but it does so by using a buffered array of bytes that act as a cache for writing. This decouples the rate and the size of the "chunks" you're writing from the more regular large block sizes in which streams are most efficiently written (to peripheral devices, files in the file system, or the network, for example).

BufferedOutputStream is one of two classes in the Java library to implement flush(), which pushes the bytes you've written through the buffer and out the other side. Because buffering is soo valuable, you might wish that every output stream could somehow be buffered. Fortunately, you can surround any output stream in such a way to achieve just that:"

Unlike normal FileOutput streams, every write command to the buffer does not correspond to a write on disk. Unless you actually fill the buffer before your program is completed, you need to explicitly flush() it

Always close in reverse order - i.e., close DataOutputStream before BufferedOutputStream before FileOutputStream because otherwise it can't flush (where would it go!?)

again...close dos then bos then fos

some methods for DataOutputStreams:

RandomAccessFile

Often you may not want to read a file from beginning to end. You may want to access a text file as a database, where you jump around reading one record, then another, then another, etc. - each in different parts of the file.

Here is a brief example that appends a string to an existing file:

See also the SUN tutorial for more on Random Access Files and their example of retrieving material from a ZIP file