Java.io.LineNumberInputStream.reset() Method



Description

The java.io.LineNumberInputStream.reset() repositions this stream to the position at the time the makr method was last called on this input stream.

Declaration

Following is the declaration for java.io.LineNumberInputStream.reset() method −

public void reset()

Parameters

NA

Return Value

The method does not return any value.

Exception

IOException − If an I/O error occurs.

Example

The following example shows the usage of java.io.LineNumberInputStream.reset() method.

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) throws IOException {
      LineNumberInputStream lnis = null;
      FileInputStream fis = null;
      
      try {
         // create new input streams
         fis = new FileInputStream("C:/test.txt");
         lnis = new LineNumberInputStream(fis);
         
         // reads and prints data from input stream
         System.out.println((char)lnis.read());
         System.out.println((char)lnis.read());
         
         // mark invoked at this position
         lnis.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)lnis.read());
         System.out.println((char)lnis.read());
         
         // if this input stream supports mark() an reset()
         if(lnis.markSupported()) {
         
            // reset() repositioned the stream to the mark
            lnis.reset();
            System.out.println("reset() invoked");
            System.out.println((char)lnis.read());
            System.out.println((char)lnis.read());
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases any system resources
         if(fis!=null)
            fis.close();
         if(lnis!=null)
            lnis.close();      
      }
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program −

ABCDE

Let us compile and run the above program, this will produce the following result −

A
B
mark() invoked
C
D
java_io_linenumberinputstream.htm
Advertisements