12 StreamIO
12 StreamIO
12 StreamIO
InputStream
ObjectInputStream PipedInputStream
FilterInputStream
Reading
read() methods will block until data is available to be read
two of the three read() methods return the number of bytes read
-1 is returned if the Stream has ended
throws IOException if an I/O error occurs. This is a checked exception
• There are 3 main read methods:
int read()
Reads a single character. Returns it as integer
ByteArrayInputStream:
Constructor is provided with a byte array.
This byte array contains all the bytes provided by this stream
Useful if the programmer wishes to provide access to a byte array using
the stream interface.
FileInputStream:
Constructor takes a filename, File object or FileDescriptor Object.
Opens a stream to a file.
FilterInputStream:
Provides a basis for filtered input streams
Creating an InputStream
ObjectInputStream
Created from another input stream (such as FileInputStream)
Reads bytes from the stream
PipedInputStream:
Connects to an Instance of PipedOutputStream
A pipe represents a one-way stream through which 2 threads may
communicate
Thread1 writes to a PipedOutputStream
Thread2 reads from the PipedInputStream
SequenceInputStream:
Constructor takes multiple InputStreams
Allows reading. When one stream ends, it continues reading from next
stream in the list
Byte-Oriented Output Stream
Classes
The following is the byte-oriented input stream class
hierarchy:
OutputStream
ObjectOutputStream
FilterOutputStream
Writing:
write() methods write data to the stream. Written data is buffered.
Use flush() to flush any buffered data from the stream.
throws IOException if an I/O error occurs. This is a checked exception
There are 3 main write methods:
flush()
To improve performance, almost all output protocols buffer output.
Data written to a stream is not actually sent until buffering thresholds are
met.
Invoking flush() causes the OutputStream to clear its internal buffers.
close()
Closes stream and releases any system resources.
Creating an OutputStream
FileOutputStream:
Constructor takes a filename, File object, or FileDescriptor object.
Any bytes written to this stream will be written to the underlying file.
Has one constructor which allows for appending to file:
FileOutputStream(String filename, boolean append)
FilterOutputStream:
Provides a basis for Output Filter Streams.
Creating an OutputStream
ObjectOutputStream
Created from another output stream (such as FileOutputStream)
Programmers serialize objects to the stream using the writeObject()
method
More on Serialization later in the Chapter.
PipedOutputStream:
Connects to an Instance of PipedInputStream
A pipe represents a one-way stream through which 2 threads may
communicate
Thread1 writes to a PipedOutputStream
Thread2 reads from the PipedInputStream
Example - Copy a File
import java.io.*;
Reader
StringReader
Provides a character stream where the data is obtained from a String
Creating a Reader Object
LineNumberReader (subclass of BufferedReader)
A stream which keeps track of how many lines there have been
A line is terminated with a linefeed, carriage return or a carriage return
followed immediately by a linefeed.
PushbackReader (subclass of FilterReader)
A stream which allows characters to be pushed back into the stream
after being read
The number of characters which can be pushed back is specified when
instantiated. Default = 1
FileWriter
Writer Methods
There are 5 main write methods:
void write(int c)
Writes a single character.
StringWriter
Characters written to this stream are collected in a StringBuffer.
The StringBuffer can be used to construct a String.
Creating a Writer Object
PrintWriter
Provides print() and println() methods for standard output
both print() and println() are overloaded to take a variety of types
When println is used, the stream will output the appropriate sequence
(either linefeed, carriage return or carriage return/linefeed) for the current
platform
System.out and System.err are PrintWriters
Example:
A programmer creates a FileOuputStream
OutputStreams are byte-oriented, but the programmer wishes to use
character-oriented streams instead.
The programmer knows that the OutputStreamWriter class can convert
between character oriented streams and byte oriented streams
The programmer creates an OuputStreamWriter and passes the
FileOutputStream reference to it
The programmer wishes to improve performance using a BufferedWriter.
The programmer creates a BufferedWriter and passes the
OutputStreamWriter object to the constructor
Filter Streams - Example
import java.io.*;
Programmer
Writes BufferedWrite OutputStream FileOutputStre File
Data r Writer am named
"Test"
Data Buffered in Character Data converted Byte Data written to file
BufferedWriter to byte data
FileWriter Revisited
Remember FileWriter?
A convenience class for writing characters to file
FileWriters assume that the default character encoding and default buffer
size are acceptable
Alternatively, open the file using a FileOutputStream and then pass that
stream to an OutputStreamWriter instance.