Java.io.BufferedReader.readline() Method

Advertisements


Description

The java.io.BufferedReader.readline() method read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Declaration

Following is the declaration for java.io.BufferedReader.readline() method

public String readline()

Parameters

  • NA

Return Value

A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.

Exception

  • IOException -- -- if an I/O error occurs

Example

The following example shows the usage of java.io.BufferedReader.readline() method.

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class BufferedReaderDemo {
   public static void main(String[] args) throws Exception {
      
      String  thisLine = null
      try{
         // open input stream test.txt for reading purpose.
         BufferedReader br = new BufferedReader("c:/test.txt");
         while ((thisLine = br.readLine()) != null) {
            System.out.println(thisLine);
         }       
      }catch(Exception e){
         e.printStackTrace();
      }
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz

Let us compile and run the above program, this will produce the following result:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz



Advertisements
Advertisements