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
-
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 :
// Java program illustrating the working of InputStream method
// mark(), read(), skip()
// markSupported(), close(), reset()
import
java.io.*;
public
class
NewClass
{
public
static
void
main(String[] args)
throws
Exception
{
InputStream geek =
null
;
try
{
geek =
new
FileInputStream(
"ABC.txt"
);
// read() method : reading and printing Characters
// one by one
System.out.println(
"Char : "
+(
char
)geek.read());
System.out.println(
"Char : "
+(
char
)geek.read());
System.out.println(
"Char : "
+(
char
)geek.read());
// mark() : read limiing the 'geek' input stream
geek.mark(
0
);
// skip() : it results in redaing of 'e' in G'e'eeks
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())
{
// reset() method : repositioning the stram to
// marked positions.
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)
{
// in case of I/O error
excpt.printStackTrace();
}
finally
{
// releasing the resources back to the
// GarbageCollector when closes
if
(geek!=
null
)
{
// Use of close() : closing the file
// and releasing resources
geek.close();
}
}
}
}
Note :
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
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.