Java.io.InputStream Class in Java
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.
public abstract class InputStream extends Object implements Closeable
- InputStream() : Single Constructor
- 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.
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.
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.
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.
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.
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.
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.
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 :
This code won’t run on online IDE as no suc file is present here.
You can run this code on your System to check the working.
ABC.txt file used in the code has
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
This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.