I/O Fundamentals in JAVA 4

I/O Stream Chaining:-

  • A program rarely uses a single stream. Instead, it chains a series of streams together to process the data.
  • The given below demonstrates an example input stream; in this case, a file stream is buffered for efficiency and then converted into data (Java primitives) items.

This is an input stream chain example

Data Source è FileInputStream => BufferedInputStream => DataInputStream è Program

  • And given below demonstrates an example output stream; in this case, data is written, then buffered, and finally written to a file.

This is an input stream chain example

Program è DataOutputStream => BufferedOutputStream => FileOutputStream è Data Sink

 

Processing Streams:-

  • A processing stream performs some sort of conversion on another stream. Processing Streams are also called as filter streams.
  • The filter input stream is created with the connection to an existing input stream.
  • This is done so that when you try to read from the filter input stream object, it supplies you with characters that originally came from the other stream object.
  • This enables you to convert the raw data into a more usable form for your application.
  • The given below lists the built in processing streams that are included in a java.io package.

 

Type                              Character Streams                 Byte Streams

Buffering                        BufferedReader                       BufferedInputStream

                                           BufferedWriter                        BufferedOuputStream

Filtering                         FilterReader                                FilterInputStream

                                           FilterWriter                                 FilterOuputStream

Converting between       InputStreamReader

bytes and characters      OutputStreamWriter

*Performing object                                                         ObjectInputStream

Serialization                                                                       ObjectOutputStream

Performing                                                                          DataInputStream

Data Conversion                                                                DataOutputStream

Counting                         LineNumberReader              LineNumberInputStream

Peeking ahead              PushbackReader                    PushbackInputStream

Printing                           PrintWriter                                 PrintStream

Note: – The FilterXYZ streams are abstract classes and cannot be used directly. You can subclass them to implement your own processing streams.

  • It is easy to create new processing streams.

Basic Byte Stream Classes:-

The FileInputStream class and FileOutputStream Class:-

  • The FileInputStream class and FileOutputStream Class are node stream classes and, as the name suggests, they use disk files.
  • The constructor for these classes enables you to specify the path of the file to which they are connected.
  • To construct the FileInputStream, the associated file must be exist and be readable. If you construct a FileOutputStream, the output file is overwritten if it already exists.

FileInputStream infile = new FileInputStream(“myfiles11.dat”);

FileOutputStream infile = new FileOutputStream(“myfiles12.dat”);

The BufferedInputStream class and BufferedOutputStream Class:-

  • Use a BufferedInputStream class and BufferedOutputStream class filter streams to increase the efficiency of I/O operations.

The PipeInputStream class and PipeOutputStream Class:-

  • We use piped streams for communicating between threads. A PipedInputStream object in one thread receives its input from a complementary PipedOutputStream object in another thread.
  • The piped streams must have both an input side and an output side to be useful.

The DataInputStream class and DataOutputStream Class:-

  • The DataInputStream class and DataOutputStream class are called filter streams enables reading and writing of java primitive types and some special formats using streams.
  • The following methods are provided for the different primitives,

The methods of DataInputStream class:-

The methods of DataInputStream class are as follows:

  • byte readByte()
  • long readLong()
  • double readDouble()

The methods of DataOutputStream class:-

The methods of DataOutputStream class are as follows:

  • void writeByte(byte)
  • void writeLong(long)
  • void writeDouble(double)
  • The methods of DataInputStream are paired with the methods of DataOutputStream.
  • These streams have methods for reading and writing strings but do not use these methods.
  • They have been deprecated and replaced by readers and writers.

The ObjectInputStream class and ObjectOutputStream Class:-

  • The ObjectInputStream class and ObjectOutputStream class enable reading from and writing Java Objects to streams.
  • Writing an object to a stream primarily involves writing the values of all the fields of the object.
  • If the fields are objects themselves, these objects should also be written to the stream.

Note: – If the fields are declared as transient or static, their values are not written to the stream.

  • Reading an object from the stream involves, reading the object type creating the blank object of that type and filling it with that data that was written.
  • A Persistent storage of an object can be accomplished if files (or other persistent storage) are used as the stream.
  • The Java API provides a standard mechanism that completely automates the process of writing and reading objects from streams.

Note: – If the stream is a network socket stream, the objects are sterilized before sending and desterilized after receiving on another host or process.

For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply