Input and Output In Java Programming Language

Input and Output operations in java

Input and Output (I/O) is an essential part of any programming language. Java provides a wide range of libraries to perform input and output operations. In Java, Input and Output operations can be performed using various classes that are provided by Java. These classes are used to read and write data from different sources such as files, network connections, standard input/output devices, etc.

What is Input in Java?

In Java, the input can be taken from different sources such as a user, file, or network. The standard input device in Java is the keyboard. Java provides various classes for taking input from different sources.

The most commonly used classes for taking input in Java are:

  1. Scanner class
  2. BufferedReader class
  3. Console class

Let’s see how to take input from the user using the Scanner class.

Scanner Class in Java

The Scanner class is used to read input from the user. It provides various methods to read different types of data such as integers, strings, etc.

Here is an example code to take input from the user using the Scanner class

import java.util.Scanner;
public class InputExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your name:");
        String name = sc.nextLine();
        System.out.println("Your name is: " + name);
    }
}

Output:

Enter your name:
John
Your name is: John

Explanation:

In the above example code, we have imported the Scanner class from the java.util package. Then we created an object of the Scanner class and passed the System.in object as an argument to the Scanner constructor.

The System.in object is the standard input device in Java. Then we used the nextLine() method of the Scanner class to read the input string entered by the user. The input string is stored in the name variable.

Finally, we have printed the value of the name variable using the System.out.println() method.

BufferedReader Class in Java:

The BufferedReader class is used to read input from the user. It provides a method called readLine() to read a line of text from the input stream.

Here is an example code to take input from the user using the BufferedReader class

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputExample {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter your name:");
        String name = br.readLine();
        System.out.println("Your name is: " + name);
    }
}

Output:

Enter your name:
John
Your name is: John

Explanation:

In the above example code, we have imported the BufferedReader class from the java.io package. Then we created an object of the BufferedReader class and passed the InputStreamReader object as an argument to the BufferedReader constructor.

The InputStreamReader object is used to read the input stream from the standard input device. Then we used the readLine() method of the BufferedReader class to read the input string entered by the user. The input string is stored in the name variable.

Finally, we have printed the value of the name variable using the System.out.println() method.

Console Class in Java

The Console class is used to read input from the user and provide a way to interact with the console. It provides various methods to read different types of data such as integers, strings, etc.

Here is an example code to take input from the user using the Console class

import java.io.Console;
public class InputExample {
    public static void main(String[] args) {
        Console console = System.console();
        System.out.println("Enter your name:");
        String name = console.readLine();
        System.out.println("Your name is: " + name);
    }
}

Output:

Enter your name:
John
Your name is: John

Explanation:

In the above example code, we have created an object of the Console class using the System.console() method. Then we used the readLine() method of the Console class to read the input string entered by the user. The input string is stored in the name variable.

Finally, we have printed the value of the name variable using the System.out.println() method.

here are some common input types in Java and their respective methods to read them from the user:

Input Types in Java

Data typeInput method
IntegernextInt()
LongnextLong()
FloatnextFloat()
DoublenextDouble()
StringnextLine()

Here’s an example code that demonstrates how to read input of different types from the user using the Scanner class:

import java.util.Scanner;

public class InputTypesExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter an integer:");
        int num1 = scanner.nextInt();

        System.out.println("Enter a long:");
        long num2 = scanner.nextLong();

        System.out.println("Enter a float:");
        float num3 = scanner.nextFloat();

        System.out.println("Enter a double:");
        double num4 = scanner.nextDouble();

        System.out.println("Enter a string:");
        String str = scanner.nextLine();

        System.out.println("You entered:");
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
        System.out.println("num3 = " + num3);
        System.out.println("num4 = " + num4);
        System.out.println("str = " + str);
    }
}

Output:

Enter an integer:
42
Enter a long:
1234567890
Enter a float:
3.14
Enter a double:
3.14159265359
Enter a string:
Hello, World!
You entered:
num1 = 42
num2 = 1234567890
num3 = 3.14
num4 = 3.14159265359
str = Hello, World!

Explanation:

In the above example code, we have used the Scanner class to read input of different types from the user. We have used the nextInt(), nextLong(), nextFloat(), nextDouble(), and nextLine() methods to read input of integer, long, float, double, and string types, respectively.

The input values are stored in the num1, num2, num3, num4, and str variables. Finally, we have printed the values of these variables using the System.out.println() method.

What is Output in Java?

In Java, the output can be displayed on the console or written to a file or network. The standard output device in Java is the console. Java provides various classes for displaying output on the console or writing to a file or network.

The most commonly used classes for displaying output on the console in Java are:

  1. System.out.println()
  2. System.out.print()
  3. System.out.printf()

Let’s see how to display output on the console using the System.out.println() method.

System.out.println() Method in Java

The System.out.println() method is used to display output on the console. It adds a newline character after the output.

Here is an example code to display output on the console using the System.out.println() method

public class OutputExample {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Output:

Hello, World!

Explanation:

In the above example code, we have used the System.out.println() method to display the string “Hello, World!” on the console.

System.out.print() Method in Java

The System.out.print() method is used to display output on the console. It does not add a newline character after the output.

Here is an example code to display output on the console using the System.out.print() method

public class OutputExample {
    public static void main(String[] args) {
        System.out.print("Hello, ");
        System.out.print("World!");
    }
}

Output:

Hello, World!

Explanation:

In the above example code, we have used the System.out.print() method twice to display the string “Hello, ” and “World!” on the console. The output of both statements is concatenated and displayed on the same line.

System.out.printf() Method in Java:

The System.out.printf() method is used to display output on the console in a formatted way. It provides various placeholders to format the output.

Here is an example code to display output on the console using the System.out.printf() method

public class OutputExample {
    public static void main(String[] args) {
        int age = 25;
        double height = 1.75;
        System.out.printf("I am %d years old and %.2f meters tall.", age, height);
}
}

Output:

I am 25 years old and 1.75 meters tall.

Explanation:

In the above example code, we have used the System.out.printf() method to display the formatted string “I am 25 years old and 1.75 meters tall.” on the console. The %d and %.2f placeholders are used to format the age and height variables, respectively.

%d – is a placeholder for integers and

%f – is a placeholder for floating-point numbers

In Java, Input and Output are essential concepts used to read data from the user and display output on the console or write to a file or network. Java provides various classes and methods to handle input and output operations. The Console class is used to read input from the user, while the System.out.println(), System.out.print(), and System.out.printf() methods are used to display output on the console.

FAQ’s

What is Input in Java?

Input refers to the process of reading data from a source such as a console, file, or network. In Java, the Scanner class is commonly used to read input from the console.

What is Output in Java?

Output refers to the process of displaying data on the console, file, or network. In Java, the System.out.println() method is commonly used to display output on the console.

What is the difference between System.out.print() and System.out.println() methods?

The System.out.print() method prints the given string without adding a new line at the end, while the System.out.println() method prints the given string and adds a new line at the end.

What is formatting in Java?

Formatting is the process of arranging data in a particular way for better readability and display. In Java, the String.format() method and the System.out.printf() method are commonly used to format output.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileInputExample {
    public static void main(String[] args) {
        try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I write output to a file in Java?

You can write output to a file in Java using the FileWriter and BufferedWriter classes. Here’s an example code:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileOutputExample {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(“output.txt”))) {
writer.write(“Hello, World!”);
} catch (IOException e) {
e.printStackTrace();
}
}
}