Unit 6 - Stream Classes
Unit 6 - Stream Classes
Unit 6 - Stream Classes
streams.
To become comfortable with the mechanisms for
InputStream
ObjectInputStream PipedInputStream
FilterInputStream
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 (which represent serialized
Objects) and converts them back into Objects
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
PipedOutputStream
ByteArrayOutputStream FileOutputStream
ObjectOutputStream
FilterOutputStream
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
OutputStream is an abstract class.
Programmers instantiate one of its subclasses
ByteArrayOutputStream:
Any bytes written to this stream will be stored in a byte
array
The resulting byte array can be retrieved using
toByteArray() method.
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
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
Writer
PipedWriter
CharArrayWriter FileWriter StringWriter
FileWriter
Writer Methods
There are 5 main write methods:
void write(int c): Writes a single character.
CharArrayWriter
Similar to ByteArrayOutputStream
Characters written to the stream are stored in a buffer.
The buffer can be retrieved by calling toCharArray() or
toString()
FilterWriter
An abstract class for writing filtered character streams
Creating a Writer Object
OutputStreamWriter
This class acts as a bridge from character streams to byte
streams
OutputStreamWriter takes an OutputStream parameter to its
constructor
Characters written to the OutputStreamWriter are translated
to bytes (based on the encoding) and written to the
underlying OuputStream.
PipedWriter
Similar to PipedOutputStream
Connects to an Instance of PipedReader
A pipe represents a one-way stream through which 2 threads
may communicate
Thread1 writes to a PipedWriter
Thread2 reads from the PipedReader
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
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
FilterStreams Provided with the JSDK
Standard Byte-oriented Filter Streams:
ObjectInputStream, ObjectOutputStream
BufferedInputStream, BufferedOutputStream
DataInputStream, DataOutputStream
PushbackInputStream
File(String pathName)
File(File parent, String child)
The File class also provides several methods which act on the
file system:
createTempFile(), delete(), deleteOnExit(), mkdir(),
mkdirs(), renameTo(), setLastModified(), setReadOnly()