Example
Let’s start with a simple example where we read a line of text from the user using the readLine() method:
Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadLineExample {
public static void main(String[] args) {
// Create a BufferedReader object to read input from the console
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
// Prompt the user to enter a line of text
System.out.print("Enter a line of text: ");
String line = reader.readLine();
// Print the line entered by the user
System.out.println("You entered: " + line);
} catch (IOException e) {
e.printStackTrace();
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Enter a line of text: Hello, World!
You entered: Hello, World!
Explanation
In this example:
- We create a BufferedReader object that wraps an InputStreamReader to read input from the console.
- The readLine() method reads a line of text entered by the user.
- The program then prints the text back to the console.
This is a simple yet powerful way to capture user input in Java.
Using readLine() with Files
The readLine() method is also commonly used to read lines of text from a file. Here's how you can use it to read data from a file:
Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileReadLineExample {
public static void main(String[] args) {
// Specify the path to the file
String filePath = "sample.txt";
try {
// Create a BufferedReader object to read from the file
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
// Read lines from the file until the end is reached
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// Close the reader
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output: (Assuming sample.txt contains the following lines)
This is the first line.
This is the second line.
This is the third line.
Explanation
In this example:
- We specify the path to a text file using a String.
- We create a BufferedReader object to read from the file.
- The while loop reads each line of the file until the end is reached (readLine() returns null).
- Each line is printed to the console.
This method is extremely useful for processing text files line by line.
Handling Exceptions with readLine()
The readLine() method can throw an IOException, which must be handled either with a try-catch block or by declaring the exception using the throws keyword.
For example:
Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExceptionHandlingExample {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line of text: ");
String line = reader.readLine();
System.out.println("You entered: " + line);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Enter a line of text: Hello
You entered: Hello
Frequently Asked Questions
What does readLine() return when the end of the stream is reached?
readLine() returns null when it reaches the end of the input stream. This is useful for detecting the end of a file or the end of input from a user.
Is readLine() the only method to read input in Java?
No, Java provides other methods for reading input, such as Scanner, which is easier to use for beginners. However, BufferedReader with readLine() is faster and more efficient for reading large amounts of text.
Can I use readLine() to read input in a graphical user interface (GUI) application?
readLine() is typically used for console-based applications. For GUI applications, you would use components like JTextField or JTextArea to capture user input.
Conclusion
The readLine() method in Java is a powerful tool for reading input, whether from the console or from a file. Understanding how to use this method effectively can greatly improve your ability to handle text-based input in your Java programs. We've explored its syntax, usage, and provided examples to help you get started.
You can also check out our other blogs on Code360.