LineNumberReader readLine Example
LineNumberReader class readLine example. This example shows you how to use readLine method.
LineNumberReader class readLine example.public String readLine() throws IOException Read a line of text. Whenever a line terminator is read the current line number is incremented.
Here is the code
/**
* @ # ReadLine.java
* A class repersenting use to readLine method
* of LineNumberReader class in java.io package
* version 28 May 2008
* author Rose India
*/
import java.io.*;
public class ReadLine {
public static void main(String args[]) throws Exception {
// Create a new instance of a LineNumberReader object
// that is reading from a FileReader object.
File myFile = new File("text.txt");
FileReader fileReader = new FileReader(myFile);
LineNumberReader reader = new LineNumberReader(fileReader);
// Read from the FileReader.
String lineRead = "";
while ((lineRead = reader.readLine()) != null) {
System.out.println(lineRead);
}
// Close the LineNumberReader and FileReader.
fileReader.close();
reader.close();
}
} |
Output
This is my file.
It contains three lines.
This is the last line. |
|