Professional Documents
Culture Documents
I-OStream Methods 19-UCS-118
I-OStream Methods 19-UCS-118
19-UCS-118
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.
Methods of InputStream
● mark()
● read()
● close()
● read()
● reset()
● markSupported()
● skip()
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:
Parameters :
Return :
void
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 :
Parameters : —------
Return :
Exception :
close() Parameters :
------
Return :
void
Exception :
-> IOException : If I/O error occurs.
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 :
read() Parameters :
Return :
reads number of bytes and return to the buffer else, -1 i.e. when
end of file is reached.
Exception :
reset() Parameters :
----
Return :
void
Exception :
-> IOException : If I/O error occurs.
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.
markSupported( Syntax :
Parameters :
-------
Return :
Syntax :
skip() Parameters :
Return :
skip bytes.
Exception :
Char : e
Char : l
Char : o
Char : G
Char : e
Syntax :
void close() public void close()throws
IOException
Throws:
IOException
Flushes this output stream and
forces any buffered output
bytes to be written out.
Syntax :
void flush() public void flush()throws
IOException
Throws:
IOException
Writes b.length bytes from the
specified byte array to this output
stream.
int off,
Parameters:
b - the data.
Throws:
IOException
Writes the specified byte to this
output stream.
Parameters:
b - the byte.
Throws:
IOException
JAVA program explaining OutputStream Class methods:
import java.io.*;
class OutputStreamDemo {
OUTPUT :
public
static void main(String args[])throws Exception ABCDEFGHIJ
{
OutputStream os = new
FileOutputStream("file.txt");byte b[] = {65, 66, 67, 68, 69,
70}; os.write(b);
os.flush();
for (int i = 71; i <75 ; i++)
{
os.write(i);
}
os.flush();
os.close();