InputStream class is the superclass of all the io classes i.e. representing an input stream of bytes. It represents input stream of bytes. Applications that are defining subclass of InputStream must provide method, returning the next byte of input.
A reset() method is invoked which re-positions the stream to the recently marked position.

Declaration :
public abstract class InputStream
extends Object
implements Closeable
Constructor :
- InputStream() : Single Constructor
Methods:
- 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
- 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.
- 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.
- 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.
- 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.
- 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
- 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.
Java Program explaining InputStream Class methods :
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws Exception
{
InputStream geek = null ;
try {
geek = new FileInputStream( "ABC.txt" );
System.out.println( "Char : " +( char )geek.read());
System.out.println( "Char : " +( char )geek.read());
System.out.println( "Char : " +( char )geek.read());
geek.mark( 0 );
geek.skip( 1 );
System.out.println( "skip() method comes to play" );
System.out.println( "mark() method comes to play" );
System.out.println( "Char : " +( char )geek.read());
System.out.println( "Char : " +( char )geek.read());
System.out.println( "Char : " +( char )geek.read());
boolean check = geek.markSupported();
if (geek.markSupported())
{
geek.reset();
System.out.println( "reset() invoked" );
System.out.println( "Char : " +( char )geek.read());
System.out.println( "Char : " +( char )geek.read());
}
else
System.out.println( "reset() method not supported." );
System.out.println( "geek.markSupported() supported" +
" reset() : " +check);
}
catch (Exception excpt)
{
excpt.printStackTrace();
}
finally
{
if (geek!= null )
{
geek.close();
}
}
}
}
|
Note :
This code won’t run on online IDE as no such file is present here.
You can run this code on your System to check the working.
ABC.txt file used in the code has
HelloGeeks
Output :
Char : H
Char : e
Char : l
skip() method comes to play
mark() method comes to play
Char : o
Char : G
Char : e
reset() method not supported.
geek.markSupported() supported reset() : false
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Feb, 2023
Like Article
Save Article