Java I/O From the Command Line

In this section we will learn about the I/O from the command line in Java.

Java I/O From the Command Line

In this section we will learn about the I/O from the command line in Java.

Java I/O From the Command Line

Java I/O From the Command Line

In this section we will learn about the I/O from the command line in Java.

Several times you can see that the programs runs after taking the input from the command line. Java also supports for the command line input and the interaction with the user in the command line environment in two ways :

  1. Using Standard Streams
  2. And using Console

Standard Streams

Various of the Operating Systems have the feature of Standard Streams. In this way an input is taken using the keyboard and output is written to the display, by default. There are three Standard Streams which are supported by the Java. These are as follows :

  • Standard Input : Standard Input can be gotten using System.in
  • Standard Output : Standard Output can be gotten using System.out
  • Standard Error : Standard Error can be gotten using System.err

System.out, System.err are the byte stream because these are defined as PrintStream objects with no character stream feature whereas, the System.in is also a byte stream but to use System.in as a character stream it will be required to wrap System.in into InputStreamReader.

Console

Console is an alternate of Standard Streams. Console has the various of additional features including the Standard Streams features. Basically Console is mostly used where you required a secure password entry. Using the reader and writer method of Console, allows the input output streams which are the character streams.

System.console() is used to retrieve the Console object and it must be retrieved before using this object by a program. While getting the console object it may return this object on available or may return null if the console object is not available. In case of the console object returns null then the console operation can not be done by either of the reason :

  • Operating System is not supported them
  • If the program is launched in noninteractive environment.

In Console the secure password entry can be supported by using the readPassword() method

Methods of Console

Method Name Description
flush() This method is used to flush out the console and said to the buffered output to write without delaying.
format(String fmt, Object... args) This method is used to write out the formatted output, format using the specified string and the arguments, to the output of console.
printf(String format, Object... args) This method writes a formatted string, format using the specified string and the arguments, to the output of console.
reader() This method gets the unique object of type Reader which is associated with the current console.
readLine() This method is used to read a line of text from the console.
readLine(String fmt, Object... args) This method is used to read a line of text from the console.
readPassword() This method is used to read password from the console in an echoing disabled mode.
readPassword(String fmt, Object... args) This method is used to read password from the console in an echoing disabled mode.
writer() This method is used to get the unique object of type PrintWriter which is associated with the current console.