java.io.BufferedReader readLine()
Description :
This java tutorial shows how to use the readLine() method of BufferedReader class of java.io package. This method returns a line of String which has been read by this BufferedReader.
Method Syntax :
public String readLine() throws IOException
Parameter Input :
DataType | Parameter | Description |
---|---|---|
N/A | N/A | N/A |
Method Returns :
This method simply returns a line of String which the BufferedReader had read.
Compatibility Version :
Requires Java 1.5 and up
Exception :
IOException
– the readLine() method throw the IOException if I/O error occurs.
Discussion :
The readLine method reads a line of text and then it is return as String data type. This method considers the following as a new line
- line feed (‘\n’)
- carriage return (‘\r’)
- carriage return followed immediately by a linefeed.
Java Code Example :
This java example source code demonstrates the use of nextLine() method of BufferedReader class. Basically it gets the user’s console input per line.
package com.javatutorialhq.java.examples; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /* * Java example source code that uses the readLine() method of * BufferedReader class * This example java program reads a user input */ public class BufferedReaderReadLine { public static void main(String[] args) { System.out.print("What is your name? "); // declare the BufferedReader Class // used the InputStreamReader to read the console input BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); String readVal; // catch the possible IOException by the readLine() method try { // assign the return value of the readLine() method to a variable readVal = reader.readLine(); // print the text read by the BufferedReader System.out.println("String read from console input:" + readVal); // close the BufferedReader object reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
Sample Output :
Running the readLine() method example source code of BufferedReader class will give you the following output:
What is your name? Albert Einstein String read from console input:Albert Einstein
Exception Scenario :
N/A
Similar Method :
- N/A
Suggested Reading List :
References :