LineNumberReader reset Example

LineNumberReader class reset example. This example shows you how to use reset method.


LineNumberReader class reset example.public void reset() throws IOException Reset the stream to the most recent mark.


/**
 * @ # Reset.java
 * A class repersenting use to reset method 
 * of LineNumberReader class in java.io package
 * version 28 May 2008
 * author Rose India 
 */
import java.io.*;

public class Reset {

    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((intmyFile.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
 

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US