LineNumberReader read Example
LineNumberReader class read example. This example shows you how to use read method.
LineNumberReader class read example. public int read(char[] cbuf, int , int ) throws IOException Read characters into a portion of an array. Whenever a line terminator is read the current line number is incremented.
Here is the code
/**
* @ # Read1.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 Read1 {
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.
char[] arr = new char[(int) myFile.length()];
reader.read(arr, 0, (int) myFile.length());
System.out.println(arr);
// Close the LineNumberReader and FileReader.
fileReader.close();
reader.close();
}
} |
Output
This is my file.
It contains three lines.
This is the last line. |
|