Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from
InputStream
andOutputStream
.There are many byte stream classes. To demonstrate how byte streams work, we’ll focus on the file I/O byte streams,FileInputStream
andFileOutputStream
. Other kinds of byte streams are used in much the same way; they differ mainly in the way they are constructed.Using Byte Streams
We’ll explore
FileInputStream
andFileOutputStream
by examining an example program namedCopyBytes
, which uses byte streams to copyxanadu.txt
, one byte at a time./* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Sun Microsystems nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyBytes { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("xanadu.txt"); out = new FileOutputStream("outagain.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }
CopyBytes
spends most of its time in a simple loop that reads the input stream and writes the output stream, one byte at a time, as shown in the following figure.Notice that Simple byte stream input and output.
read()
returns anint
value. If the input is a stream of bytes, why doesn’tread()
return abyte
value? Using aint
as a return type allowsread()
to use -1 to indicate that it has reached the end of the stream.Always Close Streams
Closing a stream when it’s no longer needed is very important — so important that
CopyBytes
uses afinally
block to guarantee that both streams will be closed even if an error occurs. This practice helps avoid serious resource leaks.One possible error is thatCopyBytes
was unable to open one or both files. When that happens, the stream variable corresponding to the file never changes from its initialnull
value. That’s whyCopyBytes
makes sure that each stream variable contains an object reference before invokingclose
.When Not to Use Byte Streams
CopyBytes
seems like a normal program, but it actually represents a kind of low-level I/O that you should avoid. Sincexanadu.txt
contains character data, the best approach is to use character streams, as discussed in the next section. There are also streams for more complicated data types. Byte streams should only be used for the most primitive I/O.So why talk about byte streams? Because all other stream types are built on byte streams.
Byte Streams