2. Objectives
On completion of this period, you would be able to
learn
• Concept of streams
• Various input and output stream classes
http://improvejava.blogspot.in/ 2
3. Recap
In the previous classes, you have studied about
exception handling mechanism in java
• Types of exceptions in java
http://improvejava.blogspot.in/ 3
4. Concept of Stream
• Java performs I/O through streams
•A stream is an abstraction that either produces or consumes
information
•A stream is linked to an I/O device by Java I/O system
•All streams behave in the same manner, even if the actual
physical device they are linked to differ
•Thus, same I/O classes and methods can be applied to any
type of device
•For example, the same methods that are used to write console
can also be used to write to a disk file
http://improvejava.blogspot.in/ 4
5. Concept of Stream
The following figure gives the relationship of java program with
I/O devices
Key Board Screen
Mouse printer
Java
Memory Memory
Program
Disk Disk
Network Network
Fig. 47.1 Relationship of Java programs with I/O devices
http://improvejava.blogspot.in/ 5
6. Concept of Stream
• The Fig. 47.1 shows in Java all I/O devices may be used with
stream
•For example, the same methods that are used to write console
can also be used to write to a disk file
• java implements streams within class hierarchies defined in
java.io. package
http://improvejava.blogspot.in/ 6
7. Concept of Streams
• Input refers to flow of data into a program
• Output refers to flow of data out of a program as
shown in Fig. 47.1
http://improvejava.blogspot.in/ 7
8. Concept of Streams Contd..
• I/O operations are complex to understand
• But more flexible and powerful
• Developers have focused their attention for non
keyboard oriented data handling
• Developers wanted to perform platform
independent I/O, hence the complexity
http://improvejava.blogspot.in/ 8
9. Various I/O Stream Classes
• java.io package defines I/O in terms of streams
• Two types of Streams
1. Byte streams
• 8 bits, binary data-based
• Examples: InputStream and OutputStream
classes
2. Character streams
• 16 bits, text-based
• Examples: Reader and Writer classes
http://improvejava.blogspot.in/ 9
10. Various I/O Stream Classes
• The following diagrams show the class hierarchies of
the stream classes
• InputStream
• OutputStream
• Reader
• Writer
http://improvejava.blogspot.in/
9CM604.47 10
15. Simple Input and Output Streams
• FileInputStream and FileOutputStream
• Used to read data from or write data to a file on the native
file system
• PipedInputStream and PipedOutputStream
• Implements the input and output components of a pipe
• Pipes are used to channel the output from one program (or
thread or code block) into the input of another
• A PipedInputStream must be connected to a
PipedOutputStream and vice versa
http://improvejava.blogspot.in/ 15
16. Simple Input and Output Streams
• ByteArrayInputStream and ByteArrayOutputStream
• Reads data from or writes data to a byte array in memory
• SequenceInputStream
• Concatenates multiple input streams into one input stream
• StringBufferInputStream
• Allows programs to read from a StringBuffer as if it were
an input stream
http://improvejava.blogspot.in/ 16
17. Filtered Streams
• FilterInputStream and FilterOutputStream are subclasses of
InputStream and OutputStream
• Both are abstract classes
• These classes defines the interface for filtered streams
• Filtered streams process the data as its being read or written
• DataInputStream and DataOutputStream
• Reads or writes primitive Java data types in a machine
independent format
http://improvejava.blogspot.in/ 17
18. Filtered Streams
• BufferedInputStream and BufferedOutputStream
• This is an efficient stream that buffers data while reading
or writing
• LineNumberInputStream
• An input stream that keeps track of line numbers while
reading
• PushbackInputStream
• An input stream with a one-byte pushback buffer
PrintStream
• An output stream with convenient printing methods
http://improvejava.blogspot.in/ 18
19. Summary
• Flow of bits of information either into the program
or from the program to and from
• Various input and output devices
• Stream is a logical entity that either produces or
consumes information
• Two types of Streams
1. Byte streams
2. Character streams
http://improvejava.blogspot.in/ 19
20. Quiz
1. Which of the following is not Byte streams
A. Input Stream
B. Output Stream
C. Reader
D. None
http://improvejava.blogspot.in/ 20
21. Quiz
2. Streams is ordered sequences of data that have a
source or a destination
A. True
B. False
http://improvejava.blogspot.in/ 21
22. Frequently Asked Questions
1. Explain the concept of Streams
2. Explain the various input output stream classes
http://improvejava.blogspot.in/ 22