Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

JAVA

Input/Output Stream Methods


By

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:

mark() public void mark(int arg)

Parameters :

arg : integer specifying the read limit of the input


Stream

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 :

read() 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.


java.io.InputStream.close() closes the
input stream and releases system
resources associated with this stream
to Garbage Collector.
Syntax :
public void close()

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 :

public int read(byte[] arg)

read() 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.


Java.io.InputStream.reset() is invoked by
mark() method. It repositions the input stream to
the marked position.
Syntax :
public void reset()

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 :

) public boolean markSupported()

Parameters :

-------

Return :

true if input stream supports the mark() and


reset
Java.io.InputStream.skip(long arg)
skips and discards arg bytes in the
input stream.

Syntax :

public long skip(long arg)

skip() 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.*; if (geek.markSupported())
public class NewClass {
{ geek.reset();
public static void main(String[] args) throws Exception System.out.println("reset() invoked");
{ System.out.println("Char : "+(char)geek.read());
InputStream geek = System.out.println("Char : "+(char)geek.read()); }
null; try { else
geek = new System.out.println("reset()
FileInputStream("ABC.txt"); System.out.println("Char : "+ method not supported.");
(char)geek.read()); System.out.println("Char : "+ System.out.println("geek.markSupported() supported"+"
(char)geek.read()); System.out.println("Char : "+ reset() : "+check); }
(char)geek.read()); geek.mark(0);; catch(Exception
geek.skip(1); excpt) {
System.out.println("skip() method comes to play"); excpt.printStackTrace();
System.out.println("mark() method comes to play"); }
System.out.println("Char : "+(char)geek.read()); finally
System.out.println("Char : "+(char)geek.read()); { if (geek!=null){
System.out.println("Char : "+(char)geek.read()); boolean geek.close();
check = geek.markSupported(); } } }}
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


Java.io.OutputStream class in Java

This abstract class is the superclass of all classes representing an


output stream of bytes. An output stream accepts output bytes and
sends them to some sink.
Applications that need to define a subclass of OutputStream must
always provide at least a method that writes one byte of output.
Methods of OutputStream
● Void close()
● Void flush()
● Void write(byte[] b)
● Void write(byte[] b,int off,int len)
● Abstract void write(int b)
Closes this output stream and
releases any system resources
associated with this stream.

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.

void write(byte[] Syntax :


public void write(byte[] b)
b) throws IOException
Parameters:
b - the data.
Throws:
IOException
Writes len bytes from the specified
byte array starting at offset off to this
output stream.

Syntax :public void write(byte[] b,

int off,

void write(byte[] int len)

b,int off,int len)


throws IOException

Parameters:

b - the data.

off - the start offset in the data.

len - the number of bytes to write.

Throws:

IOException
Writes the specified byte to this
output stream.

Abstract void Syntax :

public abstract void write(int b)


write(int b) throws IOException

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();

You might also like