Mark Current Position in Input Stream in Java



In this article, we will learn to mark and reset the current position in a file input stream in Java. Marking the current position in an input stream is a crucial functionality provided by Java's InputStream class. It enables developers to reset the stream to a previously marked position, facilitating efficient data handling, especially in scenarios like re-reading or backtracking within a file.

Key Concepts

Following are the key concepts we need to know to mark the current position in this input stream ?

  • InputStream Class: The InputStream class is an abstract class that represents an input stream of bytes. Subclasses like FileInputStream provide concrete implementations for reading data from files.
  • mark() Method: The mark() method is used to mark the current position in the input stream. Once marked, you can return to this position later using the reset() method.
  • reset() Method: The reset() method repositions the stream to the previously marked location. It works only if mark() was called earlier and the stream supports marking.
  • markSupported() Method: This method checks whether the input stream supports marking. Not all streams support this functionality.

Marking and Resetting a Position in a File Input Stream

The method java.io.InputStream.mark() is used to mark the current position in this input stream. This method requires a single parameter i.e. the bytes that can be read before the position marked is invalidated.

Opening the file input stream ?

i = new FileInputStream("C://JavaProgram//data.txt");

Marking the current position ?

i.mark(0);

 Resetting to the marked position ?

i.reset();

Example

A program that demonstrates this is given as follows ?

import java.io.FileInputStream;
import java.io.InputStream;
public class Demo {
   public static void main(String[] args) throws Exception {
      InputStream i = null;
      try {
         i = new FileInputStream("C://JavaProgram//data.txt");
         System.out.println("Char : "+(char)i.read());
         System.out.println("Char : "+(char)i.read());
         System.out.println("Char : "+(char)i.read());
         i.mark(0);
         System.out.println("Char : "+(char)i.read());
         System.out.println("Char : "+(char)i.read());
         if(i.markSupported()){
            i.reset();
            System.out.println("Char : "+(char)i.read());
            System.out.println("Char : "+(char)i.read());
         }
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Char : D
Char : A
Char : T
Char : A

Time Complexity: The overall time complexity is O(n), where n is the total number of characters read.

Space Complexity: The overall space complexity is O(1) (constant space) since no additional data structures are used that depend on the size of the file.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-30T19:14:42+05:30

456 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements