Home »
Java programming language
Java LineNumberReader getLineNumber() Method with Example
LineNumberReader Class getLineNumber() method: Here, we are going to learn about the getLineNumber() method of LineNumberReader Class with its syntax and example.
Submitted by Preeti Jain, on April 16, 2020
LineNumberReader Class getLineNumber() method
- getLineNumber() method is available in java.io package.
- getLineNumber() method is used to return the present line number in this LineNumberReader stream.
- getLineNumber() 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.
- getLineNumber() method does not throw an exception at the time of returning the current line number.
Syntax:
public int getLineNumber();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is int, it returns the present line number from this LineNumberReader.
Example:
// Java program to demonstrate the example
// of int getLineNumber() method of
// LineNumberReader
import java.io.*;
public class GetLineNumberOfLNR {
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 getLineNumber() method is to
// return the current line number in this
// stream line_r
int line_num = line_r.getLineNumber();
System.out.println("line_r.getLineNumber(): " + line_num);
} 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.getLineNumber(): 0