Java InputStream Class | java.io | Methods and Example

Java InputStream Class is base class (SuperClass) of all io classes representing an input stream of bytes. InputStream is connected to one of the aforementioned data resources and read data from that source. This is the main purpose of java InputStream class to make date 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.

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. Its set the read limit.
reset()public void reset()Repositions the input stream to the marked position.
skip()public long skip(long arg)Skip and Descard arg byte in input stream.
close()public void close()Closed the input stream and realease resource.
markSupported()public boolean markSupported()Check condtion wheather the input stream supporting the mark() and reset method
read()public int read(byte[] arg)Read a number of byte of arg.lenght 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.

Output:

mark()

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

The mark() sets a mark internally in 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. 

reset()

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

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.

Output:

close()

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

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

markSupported()

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

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.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.