Java - InputStream Class



Introduction

The Java InputStream class is the superclass of all classes representing an input stream of bytes. Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input.

Class declaration

Following is the declaration for Java.io.InputStream class −

public abstract class InputStream
   extends Object
      implements Closeable

Class constructors

Sr.No. Constructor & Description
1

InputStream()

Single Constructor

Class methods

Sr.No. Method & Description
1 int available()

This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

2 void close()

This method closes this input stream and releases any system resources associated with the stream.

3 void mark(int readlimit)

This method marks the current position in this input stream.

4 boolean markSupported()

This method tests if this input stream supports the mark and reset methods.

5 abstract int read()

This method reads the next byte of data from the input stream.

6 int read(byte[] b)

This method reads some number of bytes from the input stream and stores them into the buffer array b.

7 int read(byte[] b, int off, int len)

This method reads up to len bytes of data from the input stream into an array of bytes.

8 void reset()

This method repositions this stream to the position at the time the mark method was last called on this input stream.

9 long skip(long n)

This method skips over and discards n bytes of data from this input stream.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.Object

Example - Using close() with FileInputStream (Automatic Closure with Try-With-Resources)

The following example shows the usage of Java InputStream close() method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new FileInputStream("example.txt")) {
         int data;
         while ((data = inputStream.read()) != -1) { // Read character by character
            System.out.print((char) data);
         }
         // No need to manually close, try-with-resources handles it
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

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

Hello

Explanation

  • Uses FileInputStream, a subclass of InputStream, to read "example.txt".

  • Reads characters one at a time and prints them.

  • Uses try-with-resources, which automatically calls close() at the end.

Example - Using mark(int readLimit) with BufferedInputStream

The following example shows the usage of Java InputStream mark(int readLimit) method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new BufferedInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Mark supported? " + inputStream.markSupported());

         // Read and print first character
         System.out.print((char) inputStream.read());

         // Mark the current position
         inputStream.mark(5); // Can read up to 5 bytes before mark expires

         // Read next two characters
         System.out.print((char) inputStream.read());
         System.out.print((char) inputStream.read());

         // Reset back to the marked position
         inputStream.reset();

         // Read again from the marked position
         System.out.print((char) inputStream.read());
         System.out.print((char) inputStream.read());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

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

Mark supported? true
Helo
elo

Explanation

  • Uses BufferedInputStream, which supports marking.

  • Marks the position after reading one character.

  • Reads two more characters.

  • Resets to the marked position.

  • Reads again from the marked position.

Example - Checking markSupported() for BufferedInputStream

The following example shows the usage of Java InputStream markSupported() method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new BufferedInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Mark supported? " + inputStream.markSupported());

         inputStream.mark(5); // Mark the position

         System.out.print((char) inputStream.read()); // Read a character
         System.out.print((char) inputStream.read()); // Read another character

         inputStream.reset(); // Reset back to the mark
         System.out.print((char) inputStream.read()); // Read again

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Java")

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

Mark supported? true
Jav

Explanation

  • Uses BufferedInputStream, which supports marking.

  • Calls markSupported(), which returns true.

  • Marks a position, reads characters, and resets to the mark.

Advertisements