Home Java Example Java Io Command Line Standard Input In Java

Related Tutorials


Share on Google+Share on Google+

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.

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.


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


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


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);
         catch (IOException e)
                System.out.println("Error reading from user");
             // Second type to read standard input
                 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)


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

Download Source Code


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 22, 2012

Related Tutorials

Discuss: Command Line Standard Input In Java  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image