Java.io.InputStream Class in Java
Last Updated :
28 Mar, 2024
Java InputStream class is the superclass of all the io classes i.e. representing an input stream of bytes. It represents an input stream of bytes. Applications that are defining a subclass of the Java InputStream class must provide a method, that returns the next byte of input. A reset() method is invoked which re-positions the stream to the recently marked position.

Declaration of Java InputStream Class
public abstract class InputStream
extends Object
implements Closeable
Constructor of InputStream Class in Java
There is a constructor used with InputStream is mentioned below:
- InputStream(): Single Constructor
Methods of Java InputStream Class
Method | Description |
---|
mark() | marks the current position of the input stream. It sets readlimit i.e. maximum number of bytes that can be read before the mark position becomes invalid.a |
read() | reads next byte of data from the Input Stream |
close() | closes the input stream and releases system resources associated with this stream to Garbage collector. |
read() | reads number of bytes of arg.length from the input stream to the buffer array arg. The bytes read by read() method are returned as an int. |
reset() | invoked by mark() method. It repositions the input stream to the marked position. |
markSupported() | checks whether the input stream is supporting the mark() and reset() method or not. |
skip() | skips and discards arg bytes in the input stream. |
1. mark()
Java.io.InputStream.mark(int arg) marks the current position of the input stream. It sets readlimit i.e. maximum number of bytes that can be read before mark position becomes invalid.
Syntax :public void mark(int arg)
Parameters :
arg : integer specifying the read limit of the input Stream
Return :
void
2. read()
java.io.InputStream.read() reads next byte of data from the Input Stream. The value byte is returned in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
Syntax :public abstract int read()
Parameters :
------
Return :
Reads next data else, -1 i.e. when end of file is reached.
Exception :
-> IOException : If I/O error occurs.
3. close()
java.io.InputStream.close() closes the input stream and releases system resources associated with this stream to Garbage Collector.
Syntax :public void close()
Parameters :
------
Return :
void
Exception :
-> IOException : If I/O error occurs.
4. read() :
Java.io.InputStream.read(byte[] arg) reads number of bytes of arg.length from the input stream to the buffer array arg. The bytes read by read() method are returned as int. If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte.
Syntax : public int read(byte[] arg)
Parameters :
arg : array whose number of bytes to be read
Return :
reads number of bytes and return to the buffer else, -1 i.e. when end of file is reached.
Exception :
-> IOException : If I/O error occurs.
-> NullPointerException : if arg is null.
5. reset() :
Java.io.InputStream.reset() is invoked by mark() method. It repositions the input stream to the marked position.
Syntax :public void reset()
Parameters :
----
Return :
void
Exception :
-> IOException : If I/O error occurs.
6. markSupported() :
Java.io.InputStream.markSupported() method tests if this input stream supports the mark and reset methods. The markSupported method of InputStream returns false by default.
Syntax :public boolean markSupported()
Parameters :
-------
Return :
true if input stream supports the mark() and reset() method else,false
7. skip() :
Java.io.InputStream.skip(long arg) skips and discards arg bytes in the input stream.
Syntax :public long skip(long arg)
Parameters :
arg : no. of bytes to be skipped
Return :
skip bytes.
Exception :
-> IOException : If I/O error occurs.
Similar Reads
Java Tutorial
Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
TCP/IP Model
The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc.An Exception is an unwanted or unexpected event that occurs during the execution of a program (i.e., at runti
10 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Basics of Computer Networking
A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read