Home » Java programming language

Java LineNumberReader readLine() Method with Example

LineNumberReader Class readLine() method: Here, we are going to learn about the readLine() method of LineNumberReader Class with its syntax and example.
Submitted by Preeti Jain, on April 16, 2020

LineNumberReader Class readLine() method

  • readLine() method is available in java.io package.
  • readLine() method is used to read a line of data from this LineNumberReader stream.
  • readLine() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • readLine() method may throw an exception at the time of the reading line of data.
    IOException: This exception may throw when getting any input/output error.

Syntax:

    public String readLine();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is String, it returns string that holds a line of data excluding any line-termination characters or may return null when this LineNumberReader stream has been reached its end.

Example:

// Java program to demonstrate the example 
// of String readLine() method of 
// LineNumberReader

import java.io.*;

public class ReadLineOfLNR {
 public static void main(String[] args) throws Exception {
  FileReader fr_stm = null;
  LineNumberReader line_r = null;
  int val = 0;

  try {
   // Instantiates FileReader and LineNumberReader
   fr_stm = new FileReader("D:\\includehelp.txt");
   line_r = new LineNumberReader(fr_stm);

   // By using readLine() method is to read
   // a line of text from line_r
   String read_l = line_r.readLine();
   System.out.println("line_r.readLine(): " + read_l);
  } catch (Exception ex) {
   System.out.println(ex.toString());
  } finally {
   // with the help of this block is to
   // free all necessary resources linked
   // with the stream
   if (fr_stm != null) {
    fr_stm.close();

    if (line_r != null) {
     line_r.close();
    }
   }
  }
 }
}

Output

line_r.readLine(): JAVAWORLD



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.