Skip to content

Java InputStream Class | java.io | Methods and Example

  • by

Java InputStream Class is the base class (SuperClass) of all io classes representing an input stream of bytes. InputStream is connected to one of the aforementioned data resources and reads data from that source. This is the main purpose of the java InputStream class to make data available from source and manipulation from inside your program.

java InputStream Class, java.io Methods and exampels

About InputStream – java.io.InputStream

InputStream SubClasses

The list of Direct Known Subclasses:

  • AudioInputStream,
  • ByteArrayInputStream,
  • FileInputStream,
  • FilterInputStream,
  • InputStream,
  • ObjectInputStream,
  • PipedInputStream,
  • SequenceInputStream,
  • StringBufferInputStream

All Implemented Interfaces:

  • Closeable,
  • AutoCloseable

Java inputStream example

See the simple example of a Java InputStream class, Where this program used a FileInputStream to read the text file.

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

public class Hello {

    public static void main(String arg[]){
        try {
            InputStream inputstream = new FileInputStream("test.txt");

            int data = inputstream.read();
            while(data != -1) {
                //byte data...
                System.out.println(data);
                // convert into char
                System.out.println((char) data);

                data = inputstream.read();
            }
            inputstream.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }


}

Java InputStream class Methods

MethodSyntaxDescription
read()public abstract int read()Read input Stream next byte of data
mark()public void mark(int arg)Mark the current position. It’s set the read limit.
reset()public void reset()Repositions the input stream to the marked position.
skip()public long skip(long arg)Skip and Discard arg byte in the input stream.
close()public void close()Closed the input stream and release the resource.
markSupported()public boolean markSupported()Check condition whether the input stream supporting the mark() and reset method
read()public int read(byte[] arg)Read a number of bytes of arg.length and method return int.

Examples of Methods in InputStream

See the example of how to use methods of InputStream in your java program. For we have test.txt” file with “FilesStream” data to do all examples with it.

read()

  • abstract int read() – reads the next byte of data from the input stream.
  • int read(byte[] arg) – reads some number of bytes from the input stream and stores them into the buffer array arg.
  • int read(byte[] arg, int off, int len) – reads up to len bytes of data from the input stream into an array of bytes.
import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamExample {

    public static void main(String[] args) throws Exception {
        InputStream inputStream = null;
        try {

            inputStream = new FileInputStream("test.txt");

            // read() method : reading and printing Characters
            // one by one
            System.out.println("Char : " + (char) inputStream.read());
            System.out.println("Char : " + (char) inputStream.read());


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // releasing the resources back to the
            // GarbageCollector when closes
            if (inputStream != null) {
                // Use of close() : closing the file
                // and releasing resources
                inputStream.close();
            }
        }
    }

}

Output:

mark()

void mark(int readlimit) – marks the current position in this input stream.

The mark() sets a mark internally the InputStream which marks the point in the stream to which data has been read so far. The code using the InputStream can then continue reading data from it. 

//mark
inputStream.mark(0);

reset()

void reset() –  repositions this stream to the position at the time the mark method was last called on this input stream.

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

public class InputStreamExample {

    public static void main(String[] args) throws Exception {
        InputStream inputStream = null;
        try {

            inputStream = new FileInputStream("test.txt");

            System.out.println("Char : " + (char) inputStream.read());

            //mark
            inputStream.mark(0);

            boolean check = inputStream.markSupported();
            if (inputStream.markSupported())
            {
                // reset() method : repositioning the stram to
                // marked positions.
                inputStream.reset();
                System.out.println("reset() invoked");
                System.out.println("Char : "+(char)inputStream.read());
            }
            else
                System.out.println("reset() method not supported.");


        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

Output: Char: F
Char : i
reset() method not supported.

skip()

long skip(long n) – skips over and discards n bytes of data from this input stream.

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

public class InputStreamExample {

    public static void main(String[] args) throws Exception {
        InputStream inputStream = null;
        try {

            inputStream = new FileInputStream("test.txt");

            System.out.println("Char : " + (char) inputStream.read());

            //skip
            inputStream.skip(1);
            System.out.println("Char : " + (char) inputStream.read());


        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

Output:

close()

void close() – closes this input stream and releases any system resources associated with the stream.

Closing an InputStream is important, When you are done with a Java InputStream, you must close it. Using a InputStream by calling the InputStream close() method. Here is an example of opening an InputStream, reading all data from it, and then closing it:

inputStream.close();

markSupported()

boolean markSupported() – Check if this input stream supports the mark and reset methods.

boolean check = inputStream.markSupported();
if (inputStream.markSupported())
    {
      inputStream.reset();
               
      System.out.println("Char : "+(char)inputStream.read());
    }

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All  Java Inputstream Class is in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *