LineNumberReader mark Example
LineNumberReader class mark example. This example shows you how to use mark method.
LineNumberReader class mark example. public void mark(int ) throws IOException Mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point, and will also reset the line number appropriately.
Here is the code
/**
* @ # Mark.java
* A class repersenting use to mark method
* of LineNumberReader class in java.io package
* version 28 May 2008
* author Rose India
*/
import java.io.*;
public class Mark {
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 one line from the FileReader.
String lineRead = "";
lineRead = reader.readLine();
System.out.println(lineRead);
// Mark the position in the file.
reader.mark((int) myFile.length());
// Read the rest of the file.
while ((lineRead = reader.readLine()) != null) {
System.out.println(lineRead);
}
// Now reset the reader and re-read from the
// FileReader from the marked position on.
reader.reset();
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.
It contains three lines.
This is the last line. |
|