Input and Output in Java
In the previous blog, we learned about the StringBuffer class in Java. If you want to know more about it, visit the StringBuffer class in Java. In this blog, we will go through the Input and Output statements in Java. Before going through the input and output in Java. First, let's understand what is input and output in a programming language. Input is nothing but the data that is sent to the computer for processing and output is nothing but the processed information sent out by the computer. Now coming to Java, input in Java is the data that is given to the program/code and the output is the data that we receive from the program in the form of result. In order to make the program more interesting we use input and output. To make input /output operations faster, Java uses the concept of Stream.
Reading Input in Java
In Java, printing output is a very easy task. We just need to call System.out.println and the output is displayed on the screen. But it is not the case with input, in order to read the input, we need to construct the Scanner class first that is attached to the System.in. In order to use the Scanner class we must import java. util. Scanner package. The statement is as follows:
Once the Scanner class is imported, we need to create an object of the Scanner class as follows:
In the above statement, the Scanner object is created with the argument System.in. Once the object is created, we can use that object for reading the input. There are various ways of reading input. For example, if the input has spaces between the lines, then we use nextLine. For example
e.g
To read a single line from an input we use next()
e.g
Suppose if the input is of different datatypes, then that input can be read using the following methods:
nextInt() - nextInt() is used when we want to read Integer.
e.g
nextFloat() - nextFloat() can be used when we want to read the floating point values.
e.g
e.g
Output
As we have seen earlier that the Scanner class is defined in the java.util package, so the import statement is included at the beginning of the program. We include the import statement only when we want to use the classes that are not defined in java.lang package.
Console class
There are times when we want to enter the password for some security purpose, then in that case the Scanner class cannot be used, because the input which we use in the Scanner class is plaintext which can be visible to anyone, due to which it can create a security breach. In order to solve this problem Java 6 has introduced a Console class for this purpose. Following is the example:
e.g
To avoid a security breach, the Password is returned in an array of characters rather than the string. Once the processing of the password is done, we must override the array elements with the filler value. But the drawback of the Console class is that processing the input with the Console object is not as convenient as the Scanner object, because when we use the Console object we can only read one line at a time, which is not the case with Scanner object.
Formatting Output in Java
Unlike Input, displaying output is an easy task to do. In order to display the output or print the output on the console we just need to use System.out.prinln(). Following are the methods of print().
print()
print() method is used for printing the text on the console. It is the method of the PrintStream class which is overloaded. It takes a string as a parameter. After the string is printed, the cursor remains on the same line. It even works when you don't parse any parameters.
Syntax
println()
println is a modified version of print(). It is also used for printing the text on the console. It works like print(), but the only difference is that after printing the statement, the cursor jumps to the next line.
Syntax
printf()
printf() is used when we want to print the formatted string to the console.
Syntax
e.g
Output
File Input and Output in Java
To read from the file we need to construct a Scanner object like the following
In the above statement UTF_8, character encoding is used. It is commonly used for files on the Internet. We must know the character encoding when we read a txt file. If we ignore the character encoding then it takes the default encoding. It is not good because the program may behave differently.
If the file contains a backslash then we must eliminate it by adding extra backslashes like the following
Just like we use Scanner for reading from the text file, we use PrintWriter for writing to the file. We construct the PrintWriter object in the following way
Here if the file doesn’t exist then it created the file.
e.g