Various io stream classes .47


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Various io stream classes .47

  1. 1. Various I/O Stream Classes 1
  2. 2. ObjectivesOn completion of this period, you would be able to learn • Concept of streams • Various input and output stream classes 2
  3. 3. RecapIn the previous classes, you have studied about exception handling mechanism in java• Types of exceptions in java 3
  4. 4. Concept of Stream• Java performs I/O through streams•A stream is an abstraction that either produces or consumesinformation•A stream is linked to an I/O device by Java I/O system•All streams behave in the same manner, even if the actualphysical device they are linked to differ•Thus, same I/O classes and methods can be applied to anytype of device•For example, the same methods that are used to write consolecan also be used to write to a disk file 4
  5. 5. Concept of StreamThe following figure gives the relationship of java program withI/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 5
  6. 6. Concept of Stream• The Fig. 47.1 shows in Java all I/O devices may be used withstream•For example, the same methods that are used to write consolecan also be used to write to a disk file• java implements streams within class hierarchies defined package 6
  7. 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 7
  8. 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 8
  9. 9. Various I/O Stream Classes• 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 9
  10. 10. Various I/O Stream Classes• The following diagrams show the class hierarchies of the stream classes • InputStream • OutputStream • Reader • Writer 9CM604.47 10
  11. 11. Hierarchy of InputStream classes Object InputStream FileInputStream SequenceInpuStreamPipedInpuStream ObjectInputStream ByteArrayInputStream StringBufferInputStream FilterInpuStream DataInputStreamBufferedInpuStream PushBackInputSteam 11 Fig. 47.2 DataInput
  12. 12. Hierarchy of OutputStream Classes Object OutputStream FileOutputStream ObjectOutputStreamPipedOutputStream ByteArrayOutputStream FilterOutpuStream DataOutputStreamBufferedOutpuStream PushBackOutputSteam Fig. 47.3 DataOutput 12
  13. 13. Hierarchy of Reader Classes Object ReaderBufferedReader StringReaderCharArrayReader PipeReader InputStreamReader FilterReader Fig. 47.4 FileReader PushBackReader 13
  14. 14. Hierarchy of Writer Classes Object WriterBufferedWriter PrintWriterCharArrayWriter StringWriter FilterWriter PipeWriter FileReader OutputStreamWriter Fig No 47.5 14 FileWriter
  15. 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 15
  16. 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 16
  17. 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 17
  18. 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 18
  19. 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 19
  20. 20. Quiz1. Which of the following is not Byte streams A. Input Stream B. Output Stream C. Reader D. None 20
  21. 21. Quiz2. Streams is ordered sequences of data that have a source or a destination A. True B. False 21
  22. 22. Frequently Asked Questions1. Explain the concept of Streams2. Explain the various input output stream classes 22