Chapter 6
Chapter 6
0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0
Unicode Character
J O h n Field 4 Characters
Keyboard Screen
Mouse Printer
Java
Memory Program Memory
Disk Disk
-------------------------------------
-------------------------------------
------------------------------------- Stream
-------------------------------------
Streams
• Java Streams are classified in two basic types
• Input Stream
• Output Stream
• An Input Stream extracts data from the
source (file) and sends it to the program.
Input
Reads
Source Program
Stream
Ouput
Writes
Program Destination
Stream
Stream Classes
• The java.io package contains number of stream
classes for processing all type of data.
• These classes are categorized in two groups
• Byte Stream classes
• Character Stream Classes
• Byte Stream classes handle I/O operations
on Bytes.
• Character Stream classes manage I/O
operations on Characters.
Java Stream Classes
Java Stream
Classes
int read()
Reads a single character. Returns it as
integer
int read(byte[] buffer)
Reads bytes and places them into buffer
returns the number of bytes read
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:
Byte-Oriented Output Stream
Classes
OutputStream
PipedOutputStrea
ByteArrayOutput FileOutputStream
Stream m
ObjectOutputStream FilterOutputStream
DataOutput
OutputStream Methods
Writing:
write() method writes 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:
void write(int data)
Writes a single character
Note: even though data is an integer, data must be set
such that:
0 <= data <= 255
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.
• Will be covered later in chapter.
import java.io.*;
public class CopyFile {
public void copyFile( String inputFilename, String outputFilename)
{ try {
FileInputStream fpin = new FileInputStream(inputFilename);
FileOutputStream fpout = new FileOutputStream(outputfilename);
byte buffer = new byte[8192];
int length = 0;
while ((length = fpin.read(buffer, 0, buffer.length)) > 0)
{
fpout.write(buffer, 0, length); }
fpout.flush();
fpout.close();
fpin.close();
}
catch (IOException x) {}}}
Character Stream Classes
Reader
InputStreamReader
BufferedReader
FilterReader
FileReader
LineNumberReader PushbackReader
Reader class Methods
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
Reader class Methods
• There are 3 main read methods:
int read()
Reads a single character. Returns it as integer
PipedReader
Similar to PipedInputStream
Connects to an Instance of PipedWriter
Creating a Reader Object
StringReader
Provides a character stream where the data is
obtained from a String.
Writer
FileWriter
Writer Methods
There are 5 main write methods:
void write(int c) :Writes a single character.
void write(char[] buffer) :Writes an array of chars.
void write(char[] buffer, int offset, int length) :
Writes a portion of an array of characters starting at
offset length indicates characters to write.
void write(String aString) :Writes aString to stream.
void write(String aString, int offset, int length)
Writes a portion of a String to the stream
starts at offset , length indicates characters to
write.
Creating a Writer Object
Writer is abstract. Programmers
instantiate one of its subclasses.
BufferedWriter :Writes text to the character
stream
Provides buffering to provide efficient writing of
characters, arrays and lines
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
Creating a Writer
Object
OutputStreamWriter
This class acts as a bridge from character streams to byte
streams
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
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
System.out and System.err are PrintWriters