LineNumberReader类的getLineNumber()方法 (LineNumberReader Class getLineNumber() method)
getLineNumber() method is available in java.io package.
getLineNumber()方法在java.io包中可用。
getLineNumber() method is used to return the present line number in this LineNumberReader stream.
getLineNumber()方法用于返回此LineNumberReader流中的当前行号。
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()方法是一个非静态方法,只能使用类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
getLineNumber() method does not throw an exception at the time of returning the current line number.
getLineNumber()方法在返回当前行号时不会引发异常。
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.
方法的返回类型为int ,它从此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
翻译自: https://www.includehelp.com/java/linenumberreader-getlinenumber-method-with-example.aspx