Using Java BufferedReader Class
Using Java BufferedReader Class
The BufferedReader class, available in the java.io package, provides more efficient input reading
compared to Scanner, especially for reading strings. It can be used in combination with
InputStreamReader and System.in to read user input. Here's an example of using BufferedReader to
read a line of text:
InputExample.java
1. import java.io.BufferedReader;
2. import java.io.IOException;
3. import java.io.InputStreamReader;
10. }
11. }
Output:
Java allows you to pass command-line arguments to your program during its execution. These
arguments can be accessed using the args parameter in the main method. Command-line arguments
are useful when you want to provide inputs before running the program. Here's an example:
InputExample.java
3. if (args.length > 0) {
6. } else {
8. }
9. }
10. }
Output:
If no command line argument is provided, the program displays the message "No input provided".
such as manufacturing. For example, if the assignment is executed without arguments, the result
would be:
The DataInputStream class, part of the java.io package, allows you to read different data types from
the input stream. It provides methods like readInt(), readDouble(), readLine(), etc., to read specific
data types. Here's an example of using DataInputStream to read an integer:
InputExample.java
1. import java.io.DataInputStream;
2. import java.io.IOException;
3. import java.io.InputStream;
11. }
12. }
Output:
Enter an integer:
You entered:
The Console class, available in Java 6 and later, provides methods for reading input from the console.
It is especially useful for simple text-based input/output interactions. Here's an example of using the
Console class to read a line of text:
InputExample.java
1. public class InputExample {
4. if (console != null) {
7. } else {
9. }
10. }
11. }
Output:
The program you provide attempts to read input from the console using System.console(). The
output of the program will depend on whether the program is running in an environment that
supports console input. When the program is running in console mode (executing the program
directly from the command line), it will display a claim line of text. The user will enter text and press
Enter to print "You entered: " followed by the text entered.