Input Output Stream in Java

Stream is nothing but sequence of bytes which makes input and output operations feasible and faster. It is called stream because it facilitates a continuous flow of data. An Input/Output stream is an input source or output destination which represents various types of sources. Java uses the concept of stream to make I/O operation fast. All the classes required for I/O operation are given in java.iopackage


In java, I/O is used to receive input data and produce output by processing the input provided.

Example for File Class:

There are two types of streams, which are Character Stream and Byte stream.

  • The Java platform stores character values using Unicode conventions. Character I/O stream automatically translates this internal format to and from the local character set. In Western locales, the local character set is usually an 8-bit superset of ASCII.

  • For most applications, I/O with character stream is no more complicated than I/O with binary streams. Input and output done with stream classes automatically translates to and from the local character set.

  • A program that uses character stream in place of byte streams automatically adapts to the local character set and is ready for internationalisation - all without extra effort by the programmer.

  • All character stream classes are descended from Reader and Writer. As with byte streams, there are character stream classes that specialise in file I/O: FileReader and FileWriter.

  • Classes under this hierarchy are used for reading and writing characters from file, which are text files.

  • Now, we have a lot of classes in this hierarchy of reader and writer. Every class has some more features. In the above example we copy characters by characters. For that we use BufferedReader and PrintWriter classes as shown below:

  • Programs use byte streams to perform input and output of 8-bytes. All byte stream classes are descended from InputStream and OutputStream classes.

  • There are many byte stream classes. To demonstrate how byte streams work, we'll focus on the file I/O byte streams, File Input Stream and File Output Stream. Other kind of byte streams are used much in the same way; the difference is mainly in the way they are constructed.

  • These classes are used for binary data. We can use these classes for text files as well.

  • Character streams are wrapper for binary streams. This means that the character stream uses the binary stream internally.

  • Java uses OutputStream and InputStream for Writing data to destination and Reading data from source, respectively.

  • OutputStream and InputStream are abstract classes. So we can't use them directly

    • Output Stream class: OutputStream class is an abstract class. It is the superclass of all classes representing an output stream of bytes. Like FileOutputStream, ObjectOutputStream, etc.

    • Input Stream class: InputStream class is an abstract class. It is the superclass of all classes representing an input stream of bytes, like File Input Stream, Object Input Stream, etc.

input streams and output streams in java

  • The abstract superclass InputStream declares an abstract method read() to read one data-byte from the input source.

  • The read() method :

    • Returns the input byte read as an int in the range of 0 to 255, or

    • Returns -1 if "end of stream" condition is detected, or

    • Throws an IOException if it encounters an I/O error

  • The read () method returns an int instead of a byte, because it uses -1 to indicate end-of- stream.

  • The read () method blocks until a byte is available, an I/O error occurs, or the "end-of- stream" is detected. The term "block" means that the method (and the program) will be suspended. The program will resume only when the method returns.


  • Similar to the InputStream, the abstract superclass OutputStream declares an abstract method write() to write a data-byte to the output sink.

  • write() : Takes an int as the input parameter

  • The least-significant byte of the int argument is written out; the upper three bytes are discarded.

  • It throws an IOException if an I/O error occurs (for example, if output stream has been closed).

  • InputStream and OutputStream are abstract classes that cannot be instantiated. You need to choose an appropriate concrete subclass to establish a connection to a physical device. For example, you can instantiate a FileInputStream or FileOutputStream to establish a stream to a physical disk file.

  • We can use FileInputStream and FileOutputStream for File related operations


Example
How to Read File? [keep file in the d drive notes folder- write as a javabykiran.com in file]


Example
How to Write to File?


Example
How to copy data from one file to another?


  • There are some important classes that you would need to remember: BufferedInputStream and BufferedOutputStream

  • A BufferedInputStream adds functionality to another input stream, namely, the ability to buffer the input and to support the mark and reset methods.

  • When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time.

  • The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

  • The BufferedOutput Stream implements a BufferedOutputStream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.


The file reader and file writer read and write information from text files.

  • These are the convenience classes for reading and writing character-oriented data.

  • So, it's good to avoid FileInputStream and FileOutputStream if you have to read and write the textual information.

  • The constructors of this classes assume that the default character encoding and the default byte-buffer size are appropriate.
    For example: Copying data from one file to another file using FileReader and FileWriter.


There are many ways to read from the keyboard. By using the following classes we can read the data from keyboard:

  • InputStreamReader
  • BufferedReader
  • Scanner
  • Console
  • By using InputStreamReader and Buffered Reader –

Example:


Example:
Addition of two numbers by scanner class in 1.5 JDK:


Example:
By using Console class

  • This class can be used to read data from multiple streams.

  • A SequenceInputStream represents the logical concatenation of other input streams.

  • It starts out with an ordered collection of input streams and reads from the first one until end of file is reached, where on it reads from the second one, and so on, until the end of file is reached on the last of the contained input streams.


Example:
Reading two files using SequenceInputStream –