Java IO - Console Input and Output


Java console IO is done using class, System.in, java.util.Scanner, java.io.Console and java command line arguments, learn how to use these Classes and functions to work java with console.

We: Do you know Java IO ?
you: yes, I know about byte and character streams.
We: Oh, okay, do you know about reading and writing on console ?
You: we know about writing but not about the reading from console.
We: okay, In this tutorial we are going to introduce you with something more than print and println.

The Standard streams are the part of Java language specification to its core. Java uses three streams to interact with your command line interpreter. System.in, System.out and System.err. These are the streams which works with the console to provide you the command line interaction. You must have used System.out in our first ever program in Java, but you might not know about the System.in.

All three are defined as a part of PrintStream, which by nature is a Byte Stream but on a top level System.out and System.err have a character support but you will have to manually wrap the System.in into InputStreamReader to use the character stream properties. But the suggestion is to use the buffer properties so you should also wrap it in BufferedReader it would make the program more efficient.

Check out the following program, and Please pay attention to the program, there is another thing you will notice in the way we print the output.


import java.io.*;
class ConsoleIO{
	public static void main(String[] args) throws IOException{
		BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("Please enter any input: ");
		String s = is.readLine();
		System.out.format("you entered: %s",s);
	}
}

You see, first we have wrapped System.in inside InputstreamReader and this to BufferedReader, this way we can use the properties of character stream and buffer. The output will be like following.

Please enter any input: hello world
you entered: hello world

Now you see, we have used print() which is an overloaded method that will output the argument passed to the console. There is another method println() which will print the output and move to the next line. The format work somewhat like the printf() function you might have used in 'C' programming language. Well if you don't know C not to worry. The format is used to format the string. you define the format specifier which is replaced by the corresponding argument.

%d for integer
%f for float
%s for string
%c for character etc.

well that is just one of the way in which you can interact with the console. There is more to it. Next we are going to demonstrate you the working of main(String[] args). After all, its not useless.

So to get you started, the String[] args is a string type array, which you can provide to the main method when you run the program. Check out this program. This type of parameter is known as command line arguments.


class ConsoleIO{
	public static void main(String[] args){
		if(args.length>0){
			for(String s : args){
				System.out.println(s);
			}
		}
		else{
			System.out.println("You did not passed any command line arguments");
		}
	}
}

To demonstrate the working we have run it in two test cases, as you may see below.

$ javac ConsoleIO.java 
$ java ConsoleIO 
You did not passed any command line arguments
$ java ConsoleIO hello world
hello
world

There we have used a special function of Java language, you can see it as overloaded for loop, in which you can Iterate over the arrays. When you want to pass some initial arguments to the program.

Beside the Command line argument there is yet another way to take the input, that is Console. you can find this class in java.io package. Check out the following program to see the working.


import java.io.Console;

class ConsoleIO{
	public static void main(String[] args){
		Console c = System.console();
		String s = c.readLine("Enter a string: ");
		System.out.println("the string you entered is: "+s);
	}	
}

$ java ConsoleIO
Enter a string: hello world
the string you entered is: hello world

This class also provide you with more methods which also include the Password input. you can check out complete details of the class using command
javap java.io.Console

Note: there is another class known as Scanner which is found in java.util.Scanner, you can also use this class to take input from console however it is very less use. For the efficient program and better execution time the First method, that is System.in is suggested.