Md121 streams

591 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
591
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Md121 streams

  1. 1. I/O Streams
  2. 2. Objectives • Describe the main features of the java.io package • Construct node and processing streams, and use them appropriately • Distinguish readers and writers from streams, and select appropriately between them • Use the Serialization interface to encode the state of an object
  3. 3. I/O Fundamentals A stream can be thought of as a flow of data from a source to a sink. A source stream initiates the flow of data, also called an input stream. A sink stream terminates the flow of data, also called an output stream. Sources and sinks are both node streams. Types of node streams are: files, memory, and pipes between threads or processes.
  4. 4. Program need information from an external resource (USER) or to send out the information to external resource(USER) , Information can be anywhere in the file or disk or the network, information can be of any type object ,char , image ,sound. To get the information program open a stream on information source and read the information sequentially
  5. 5. Similarly a program can send information to external destination by opening a stream to destination and write the information out sequentially. No matter where the data is coming from or going to and no matter what its type, algorithm for reading and writing the data are same
  6. 6. Reading Writing Open a Stream Open a Stream While more information While more information Read information Write information Close Stream Close the Stream
  7. 7. Fundamental Stream Classes Stream classes are divided into two class hierarchies Character Stream provide a convenient means for handling input and output of characters Byte Stream provide a convenient means for handling input and output of bytes
  8. 8. InputStream Methods • The three basic read methods: int read() int read(byte[] buffer) int read(byte[] buffer, int offset, int length)
  9. 9. OutputStream Methods • The three basic write methods: void write(int c) void write(byte[] buffer) void write(byte[] buffer, int offset, int length)
  10. 10. Reader Methods • The three basic read methods: int read() int read(char[] cbuf) int read(char[] cbuf, int offset, int length)
  11. 11. Writer Methods • The basic write methods: void write(int c) void write(char[] cbuf) void write(char[] cbuf, int offset, int length) void write(String string) void write(String string, int offset, int length)
  12. 12. Input Stream Class Hierarchy
  13. 13. Output Stream Class Hierarchy
  14. 14. Reader Class Hierarchy
  15. 15. Writer Class Hierarchy
  16. 16. Used to read from or write to a file on the native file system. FileReader FileWriter FileInputStream FileOutputStream File Pipes are used to channel the output from one thread into the input of another. PipedReader PipedWriter PipedInputStream PipedOutputStream Pipe Read characters from a String StringWriter to write to a String . StringBufferInputStream it reads bytes from a StringBuffer . StringReader StringWriter StringBufferInputStream Read Char from Array Write Char to Array Read Byte from Array Write Byte to Array CharArrayReader CharArrayWriter ByteArrayInputStream ByteArrayOutputStream Memory Description Streams Type of I/O I/O Streams
  17. 17. These input streams each have a pushback buffer. When reading data from a stream, it is sometimes useful to peek at the next few bytes or characters in the stream to decide what to do next. PushbackReader PushbackInputStream Peeking Ahead Keeps track of line numbers while reading. LineNumberReader LineNumberInputStream Counting Read or write primitive data types in a machine-independent format. N/A   DataInputStream DataOutputStream Data Conversion Used to serialize objects. N/A ObjectInputStream ObjectOutputStream Object Serialization Concatenates multiple input streams into one input stream. N/A SequenceInputStream Concatenation
  18. 18. Filter data as it's being read or written. FilterReader FilterWriter FilterInputStream FilterOutputStream Filtering Buffer data while reading or writing,. BufferedReader BufferedWriter BufferedInputStream BufferedOutputStream Buffering Contain convenient printing methods. These are the easiest streams to write to PrintWriter PrintStream Printing
  19. 19. A reader and writer pair that forms the bridge between byte streams and character streams. An InputStreamReader reads bytes from an InputStream and converts them to characters, using the default character encoding or a character encoding specified by name. An OutputStreamWriter converts characters to bytes, using the default character encoding or a character encoding specified by name and then writes those bytes to an OutputStream . InputStreamReader OutputStreamWriter Converting between Bytes and Characters
  20. 20. Serialization Serialization is a process of writing the state of an object to a byte stream ,this is useful when you want to save the state of your program to a persistent storage area such as file,it may be restored by the process of deserialization Only the object’s data are serialized. Data marked with the transient keyword are not serialized. Object that implement serializable interface can be saved and restored by serialization facilities. Serializable interface define no members,it is used to indicate class may be serialized (Example SerializationDemo.java)

×