Saturday, January 11, 2020

Java IO

Java I/O Package

The I/O package(java.io) in Java defines input/output in terms of streams, file systems, and serialization. The  java.io package defines two types of streams:


  • Byte streams(8-bit character  representation)
  • Character streams (16-bit Unicode character representation)

IO Streams java
IO Streams


Streams are ordered sequences of data that have a source (input streams in byte streams or readers in character streams) or destination(output streams or writers in character streams). These I/O streams keep programmers isolated specific details of the underlying operating system while enabling access to system resources through files and other means.


Byte-Streams

There are two main abstract classes in the class hierarchy representing byte-streams.
  1. InputStream
  2. OutputStream

InputStream Class

The abstract class declares methods to read bytes from a particular source.InputStream is the superclass of most of the classes available in java.io package for input streams. 





OutputStream Class

The abstract class declares methods to write bytes to a particular destination.OutputStream is the superclass of most of the classes available in java.io package for output streams. 


Video Tutorial





Character Streams

    There are two main abstract classes in the class hierarchy representing character-streams.
  1. Reader
  2. Writer
Reader

The Reader is an abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close(). Most subclasses, however, will override some of the methods to provide higher efficiency, additional functionality, or both.

Reader Class hierarchy
Reader Class hierarchy

Writer

An abstract class for writing to character streams. The methods that a subclass of Writer must implement are write(char[], int, int), flush(), and close(). Most subclasses will override some of the methods defined here to provide higher efficiency, functionality or both.

Writer class hierarchy
Writer class hierarchy


Video Tutorial



Java built-in(Standard) IO Streams


In the simplest word, Java provides three in-built IO streams or standard IO streams,

  • System.out
  • System.in
  • System.err

System.out

System is class declared as final, and out is the static member of this class representing output.
System .out is one of the built-in IO streams provided by Java itself. Typically, System.out used to write the output streams to the console.

System.out.println("Enter something");

So in the above line is actually applied to send the output stream to the console.


System.in

System is class declared as final, and in is the static member of the class representing input like out represents the output.
System.in is one of the built-in IO streams provided by Java, like System.out and System.err. Basically, System.in is used to read the input streams from the keyboard.

Scanner scanner=new Scanner(System.in);
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

So in both the lines above System.in is actually applied to retrieve the input stream from the keyboard.

System.err

System.err is the built-in IO streams provided by Java for error output streams. Basically, System.err is used to write the error output streams(error messages) to the like, System.out is used for normal output streams. 

System.err.println("Some exception raised");
 Example,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Test {
     public static void main(String[] args) {

 Scanner scanner=new Scanner(System.in);//using System.in input stream
 System.out.println("Enter something");
 String input=scanner.nextLine();
 System.out.println("You entered "+input);//using System.out output stream
 BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Enter something more");
  try {
      input=br.readLine();
      } catch (IOException e) 
      {
  System.err.println("Some exception raised");//using System.err stream
               e.printStackTrace();
       }
   System.out.println("you entered "+input +" this time");
     }
}