LineNumberReader read Example
LineNumberReader class read example. This example shows you how to use read method.
LineNumberReader class read example.public int read() throws IOException Read a single character. Line terminators are compressed into single newline ('\n') characters. Whenever a line terminator is read the current line number is incremented.
Here is the code
/**
* @ # Read.java
* A class repersenting use to read method
* of LineNumberReader class in java.io package
* version 28 May 2008
* author Rose India
*/
import java.io.*;
public class Read {
public static void main(String args[]) throws Exception {
// Create a new instance of a LineNumberReader object
// that is reading from a FileReader object.
FileReader fileReader = new FileReader("text.txt");
LineNumberReader reader = new LineNumberReader(fileReader);
// Read from the FileReader.
int charRead;
while ((charRead = reader.read()) >= 0) {
System.out.print((char) charRead);
}
// Close the LineNumberReader and FileReader.
fileReader.close();
reader.close();
}
} |
Output
This is my file.
It contains three lines.
This is the last line. |
|