Using Standard Input-Output in Java

In Java, standard input and output are important concepts for interacting with the users and handling external data. The standard input is the stream of data that is received by the program, typically from the keyboard or another source. The standard output, on the other hand, is the stream of data that is sent by the program, usually to the console or another destination.

Java provides several classes and methods to work with standard input and output, making it easy to read user input, display output, and handle data exchange. Let's take a look at some of the key classes and methods that can be used for standard input and output in Java:

Reading Input from the User

To read input from the user, the Scanner class is widely used in Java. It provides various methods for reading different types of data from the standard input. Here's an example that demonstrates how to read an integer input from the user:

import java.util.Scanner;

public class ReadInputExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter an integer: ");
        int number = scanner.nextInt();
        
        System.out.println("You entered: " + number);
    }
}

In the above code, we create a Scanner object scanner to read input from the standard input stream System.in. The nextInt() method of the Scanner class is used to read an integer value entered by the user. The entered number is then displayed using the println() method.

Displaying Output

To display output to the console or standard output, we can use the System.out object and its println() method. Here's an example:

public class DisplayOutputExample {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 20;
        int sum = num1 + num2;
        
        System.out.println("The sum is: " + sum);
    }
}

In the above code, the sum of num1 and num2 is calculated and displayed using println(). The output will be shown in the console as The sum is: 30.

Redirecting Input and Output

Java also allows redirecting the standard input and output to files or other destinations. This can be achieved by using the System.setIn() and System.setOut() methods, respectively. Here's an example:

import java.io.*;

public class RedirectIOExample {
    public static void main(String[] args) throws FileNotFoundException {
        File inputFile = new File("input.txt");
        File outputFile = new File("output.txt");
        
        // Redirect standard input
        System.setIn(new FileInputStream(inputFile));
        
        // Redirect standard output
        System.setOut(new PrintStream(outputFile));
        
        // Read input and display output as usual
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();
        System.out.println("You entered: " + number);
    }
}

In this example, we redirect the standard input to input.txt and standard output to output.txt. The program reads an integer from the input.txt file and displays it in the output.txt file.

Conclusion

Standard input and output are essential for handling user interaction and data exchange in Java programs. By using the Scanner class, we can easily read user input, while the System.out object allows us to display output to the console. Additionally, Java provides methods to redirect standard input and output, enabling efficient data processing from files or other destinations. With these capabilities, you can effectively work with standard input and output in your Java programs.


noob to master © copyleft