Table of contents
1.
Introduction
2.
What is readLine() Method?
2.1.
Syntax
3.
Example
3.1.
Java
3.2.
Explanation
4.
Using readLine() with Files
4.1.
Java
4.2.
Explanation
5.
Handling Exceptions with readLine()
5.1.
Java
6.
Frequently Asked Questions
6.1.
What does readLine() return when the end of the stream is reached?
6.2.
Is readLine() the only method to read input in Java?
6.3.
Can I use readLine() to read input in a graphical user interface (GUI) application?
7.
Conclusion
Last Updated: Aug 14, 2024
Easy

Readline in Java

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When working in Java, reading input from the user is a common task. Whether you're creating a simple console application or handling input files, understanding how to read input efficiently is crucial for mastering any programming language. One of the most commonly used methods for reading input in Java is the readLine() method.

Readline in Java

In this article, we will explore the readLine() method in Java, its syntax, usage, and provide examples to help you understand how it works.

What is readLine() Method?

The readLine() method in Java is a part of the BufferedReader class. It is used to read a line of text from an input stream. The method reads characters from the input stream and returns them as a string, up to the point where a newline character (\n) is encountered.

Syntax

The syntax of the readLine() method is straightforward:

String line = bufferedReader.readLine();


Here:

  • bufferedReader is an instance of the BufferedReader class.
     
  • The method returns a String containing the line of text read from the input stream.

Example

Let’s start with a simple example where we read a line of text from the user using the readLine() method:

  • Java

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:

  1. We create a BufferedReader object that wraps an InputStreamReader to read input from the console.
     
  2. The readLine() method reads a line of text entered by the user.
     
  3. 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

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:

  1. We specify the path to a text file using a String.
     
  2. We create a BufferedReader object to read from the file.
     
  3. The while loop reads each line of the file until the end is reached (readLine() returns null).
     
  4. 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

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.

Live masterclass