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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Does JVM create object of Main class (the class with main())?
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java