Input-Output Streams in Java
Stream in Java
Keyboard Screen
Java treats flow of data as
Input stream
stream.
Mouse Printer
Java streams are classified into
two basic types, namely, input
Memory Java Program Memory
stream and output stream.
The [Link] package contains a Disk Disk
large number of stream classes
to support the streams. Output stream
Network Network
Input and output streams
Input Stream
Reads
Source Program
(a) Reading data into a program
Output Stream
Program Source
(b) Writing data to a destination
Java Classes for I-O Streams
I-O stream classes in Java
Java provides [Link] package which contains a large number of stream
classes to process all types of data
Byte stream classes
• Support for handling I/O operations on bytes
Character stream classes
• Supports for handling I/O operations on characters
Taxonomy: Java stream classes
Java Stream
Class
Byte Stream Character
Class Stream Class
Input Stream Output Reader Writer
Class Stream Class Class Class
Memory File Pipe Memory File Pipe
Java Input Stream Classes
Taxonomy: Java stream classes
Java Stream
Class
Byte Stream Character
Class Stream Class
Input Stream Output Reader Writer
Class Stream Class Class Class
Memory File Pipe Memory File Pipe
Java input streams classes
Java input stream classes
InputStream classes is used to read 8-bit bytes and supports a number
of input-related methods
• Reading bytes
• Closing streams
• Marking positions in streams
• Skipping ahead in streams
• Finding the number of bytes in stream
• and many more…
Some input stream methods
Method Description Example:
read( ) Read a byte from the input stream
DataInputStream
read(byte b[ ]) Read an array of bytes into b readShort( ) readDouble( )
readInt( ) readLine( )
read(byte b[ ], int n, int m) Reads m bytes into b starting from nth byte readLong( ) readChar( )
readFloat( ) readBoolean( )
available( ) Gives number of bytes available in the input
readUTF( )
skip(n) Skips over n bytes from the input stream
reset( ) Goes back to the beginning of the stream
close( ) Close the input steam
Example: Use of class InputStream
• Reading bytes
• int read() • Skipping ahead in a stream
• int read (byte b[]) long skip (long n)
• int read (byte b[], int off, int len)
• Marking positions in a stream
void mark (int limit)
• Closing streams void reset()
• void close() boolean markSupported()
• Finding the number of bytes in a stream
• int available()
Java Output Stream Classes
Taxonomy: Java stream classes
Java Stream
Class
Byte Stream Character
Class Stream Class
Input Stream Output Reader Writer
Class Stream Class Class Class
Memory File Pipe Memory File Pipe
Java output stream classes
OutputStream classes is used to write 8-bit bytes and supports a number
of input-related methods
• Writing bytes
• Closing streams
• Flushing streams
• etc.
Java output stream classes
Object
OutputStream
FileOutputStream ObjectOutputStream
PipedOutputStream ByteArrayOutputStream
FilterOutputStream
BufferedOutputStream PushbackOutputStream
DataOutputStream
DataOutput
Some methods in output stream classes
Method Description Example:
write ( ) Write a byte from the input stream DataOutputStream
write (byte b[ ]) Write all bytes in the array b to the writeShort( ) writeDouble( )
output steam writeInt( ) writeLine( )
writeLong( ) writeChar( )
write (byte b[ ], int n, int m) Write m bytes from array b starting
writeFloat( ) WriteBoolean( )
from nth byte
writeUTF( )
close( ) Close the output stream
flush( ) Flushes the output stream
Use of class OutputStream
Writing bytes
• void write (byte b)
• void write (byte b[])
• void write (byte b[], int off, int len)
Closing a stream
• void close()
Clearing a buffer
• void flush()
Character Stream Classes
Taxonomy: Java stream classes
Java Stream
Class
Byte Stream Character
Class Stream Class
Input Stream Output Reader Writer
Class Stream Class Class Class
Memory File Pipe Memory File Pipe
Character stream classes
Character stream classes is used to read and write characters and
supports a number of input-output related methods
Reader stream classes
• To read characters from files.
• In many way, identical to InputStream classes.
Writer stream classes
• To write characters into files.
• In many way, identical to OutputStream classes.
Reader stream classes
Object
Reader
BuffereedReader StringReader
CharArrayReader PipeReader
InputStreamReader FilterReader
File Reader PushbackReader
Writer stream classes
Object
Writer
BuffereedWriter StringReader
CharArrayWriter PipeWriter
FilterWriter PrintWriter
OutputStreamWriter
FileWriter
List of Important tasks and their Classes
Task Character Stream Class Byte Stream Class
Performing input operations Reader InputStream
Buffering input BufferedReader BufferedlnputStream
Keeping track of line numbers LineNumberReader LineNumberlnputStream
Reading from an array CharArrayReader ByteArrayInputStream
Translating byte stream InputStreamReader (none)
into a character stream
Reading from files FileReader FileInputStream
Filtering the input FilterReader FilterlnputStream
Pushing back characters/bytes PushbackReader PushbackInputStream
Reading from a pipe PipedReader PipedInputStream
Reading from a string StringReader StringBufferInputStream
Reading primitive types (none) DataInputStream
Performing output operations Writer OutputStream
Buffering output BufferedWriter BufferedOutputStream
Writing to an array CharArrayWriter ByteArrayOutputStream
Filtering the output FilterWriter FilterOutputStream
Translating character stream OutputStreamWriter (none)
into a byte stream
Writing to a file FileWriter FileOutputStream
Printing values and objects PrintWriter PrintStream
Writing to a pipe PipedWriter PipedOutputStream
Writing to a string StringWriter (none)
Writing primitive types (none) DataOutputStream