various io stream classes .47

22
http:// improvejava.blogspot.in/ 1 Various I/O Stream Classes

Upload: myrajendra

Post on 16-Apr-2017

1.043 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Various io stream classes .47

http://improvejava.blogspot.in/ 1

Various I/O Stream Classes

Page 2: Various io stream classes .47

http://improvejava.blogspot.in/ 2

Objectives

On completion of this period, you would be able to learn• Concept of streams• Various input and output stream classes

Page 3: Various io stream classes .47

http://improvejava.blogspot.in/ 3

Recap

In the previous classes, you have studied about exception handling mechanism in java

• Types of exceptions in java

Page 4: Various io stream classes .47

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

4http://improvejava.blogspot.in/

Page 5: Various io stream classes .47

http://improvejava.blogspot.in/ 5

Concept of Stream

Key Board

Mouse

Memory

Disk

Network

Screen

printer

Memory

Disk

Network

Java Program

The following figure gives the relationship of java program with I/O devices

Fig. 47.1 Relationship of Java programs with I/O devices

Page 6: Various io stream classes .47

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

6http://improvejava.blogspot.in/

Page 7: Various io stream classes .47

http://improvejava.blogspot.in/ 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

Page 8: Various io stream classes .47

http://improvejava.blogspot.in/ 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

Page 9: Various io stream classes .47

http://improvejava.blogspot.in/ 9

Various I/O Stream Classes• java.io package defines I/O in terms of streams

• Two types of Streams1. Byte streams

• 8 bits, binary data-based• Examples: InputStream and OutputStream

classes2. Character streams

• 16 bits, text-based• Examples: Reader and Writer classes

Page 10: Various io stream classes .47

9CM604.47 10

Various I/O Stream Classes• The following diagrams show the class hierarchies of

the stream classes• InputStream• OutputStream• Reader• Writer

10http://improvejava.blogspot.in/

Page 11: Various io stream classes .47

http://improvejava.blogspot.in/11

Object

InputStream

FileInputStream SequenceInpuStream

PipedInpuStream ObjectInputStream

ByteArrayInputStream StringBufferInputStream

FilterInpuStream

BufferedInpuStream PushBackInputSteam

DataInputStream

DataInput

Hierarchy of InputStream classes

Fig. 47.2

Page 12: Various io stream classes .47

http://improvejava.blogspot.in/ 12

Object

OutputStream

FileOutputStream ObjectOutputStream

PipedOutputStream ByteArrayOutputStream

FilterOutpuStream

BufferedOutpuStream PushBackOutputSteam

DataOutputStream

DataOutput

Hierarchy of OutputStream Classes

Fig. 47.3

Page 13: Various io stream classes .47

http://improvejava.blogspot.in/ 13

Object

Reader

BufferedReader StringReader

CharArrayReader PipeReader

InputStreamReader FilterReader

FileReader PushBackReader

Hierarchy of Reader Classes

Fig. 47.4

Page 14: Various io stream classes .47

http://improvejava.blogspot.in/14

Object

Writer

BufferedWriter PrintWriter

CharArrayWriter StringWriter

FilterWriter PipeWriter

FileReader

OutputStreamWriter

FileWriter

Hierarchy of Writer Classes

Fig No 47.5

Page 15: Various io stream classes .47

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

15http://improvejava.blogspot.in/

Page 16: Various io stream classes .47

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

16http://improvejava.blogspot.in/

Page 17: Various io stream classes .47

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

17http://improvejava.blogspot.in/

Page 18: Various io stream classes .47

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

18http://improvejava.blogspot.in/

Page 19: Various io stream classes .47

http://improvejava.blogspot.in/ 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 Streams1. Byte streams2. Character streams

Page 20: Various io stream classes .47

http://improvejava.blogspot.in/ 20

Quiz

1. Which of the following is not Byte streams

A. Input StreamB. Output StreamC. ReaderD. None

Page 21: Various io stream classes .47

2. Streams is ordered sequences of data that have a source or a destination

A. TrueB. False

http://improvejava.blogspot.in/ 21

Quiz

Page 22: Various io stream classes .47

http://improvejava.blogspot.in/ 22

Frequently Asked Questions

1. Explain the concept of Streams2. Explain the various input output stream classes