Command Line Standard Input In Java

In this section we will discuss about the Java IO Standard Input through Command Line.

Command Line Standard Input In Java

In this section we will discuss about the Java IO Standard Input through Command Line.

Command Line Standard Input In Java

Command Line Standard Input In Java

In this section we will discuss about the Java IO Standard Input through Command Line.

Standard streams, feature of various O/S, are read through standard input in Java. Standard streams are read from the keyboard, file, and between the program which are handled through the command line interpreter. Java provides System.in to access the Standard Input. System.in is a byte stream but this can be wrapped within InputStreamReader to use it as a character stream.

System.in

In System.in, in is a public static final field defined as an object of InputStream that specifies a standard input stream is open and ready for providing input data. These input data can be read from the keyboard input, file input or any other input source depends upon the user or host environment. So, with the System.in we can use methods of InputStream (discussed in the example given below).

public static final InputStream in

Example

Here I am giving a simple example which will demonstrate you about how standard input can be read through command line in Java. In this example I have created a Java class named JavaSystemInExample.java into which I have tried to use the System.in in two ways in first type I have used the read() method of InputStream to read byte and in second type I have wrapped the System.in within InputStreamReader.

Source Code

JavaSystemInExample.java

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class JavaSystemInExample
{
     public static void main(String args[])
      {
            // First type to read standard input
        /* try
             {
                System.out.print("Enter a character  : ");
                 int r = System.in.read();
                 System.out.print("Character entered by you is : "+(char)r);
                 System.out.println();                             
              }
         catch (IOException e)
              {
                System.out.println("Error reading from user");
              }
         */
             // Second type to read standard input
             try
              {
                 System.out.println("Enter a character : ");
                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                 int d = br.read();
                 System.out.println("Character entered by you is : "+(char)d);
                  
              }
           catch(IOException ie)
              {
                  System.out.println(ie);
              }           
      }
}

Output

When you will execute the above code then the output will be as follows :

Download Source Code