0% found this document useful (0 votes)
41 views12 pages

Outputstream Vs Inputstream

Java uses streams for input and output operations. There are two main types of streams - byte streams that handle bytes of data and character streams that handle Unicode characters. The key abstract classes for byte streams are InputStream and OutputStream, while the key abstract classes for character streams are Reader and Writer. These abstract classes have various concrete subclasses that handle different I/O devices and operations. Java also has three standard streams - System.in for standard input, System.out for standard output, and System.err for standard error output.

Uploaded by

MamataMaharana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views12 pages

Outputstream Vs Inputstream

Java uses streams for input and output operations. There are two main types of streams - byte streams that handle bytes of data and character streams that handle Unicode characters. The key abstract classes for byte streams are InputStream and OutputStream, while the key abstract classes for character streams are Reader and Writer. These abstract classes have various concrete subclasses that handle different I/O devices and operations. Java also has three standard streams - System.in for standard input, System.out for standard output, and System.err for standard error output.

Uploaded by

MamataMaharana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Java I/O (Input and Output) is used to process the input and produce the output.

Java uses the concept of stream to make I/O operation fast. The java.io package contains
all the classes required for input and output operations.

We can perform file handling in java by Java I/O API.

OutputStream vs InputStream
The explanation of OutputStream and InputStream classes are given below:

OutputStream
Java application uses an output stream to write data to a destination, it may be a file, an
array, peripheral device or socket.

InputStream
Java application uses an input stream to read data from a source, it may be a file, an array,
peripheral device or socket.
Let's understand working of Java OutputStream and InputStream by the figure given below.

OutputStream class
OutputStream class is an abstract class. It is the super class of all classes representing an
output stream of bytes. An output stream accepts output bytes and sends them to some
sink.

Useful methods of OutputStream


Method Description

1) public void write(int)throws is used to write a byte to the current output


IOException stream.

2) public void write(byte[])throws is used to write an array of byte to the current


IOException output stream.

3) public void flush()throws flushes the current output stream.


IOException

4) public void close()throws is used to close the current output stream.


IOException
OutputStream Hierarchy

InputStream class
InputStream class is an abstract class. It is the super class of all classes representing an
input stream of bytes.

Useful methods of InputStream


Method Description

1) public abstract int reads the next byte of data from the input stream.
read()throws IOException It returns -1 at the end of file.

2) public int available()throws returns an estimate of the number of bytes that can
IOException be read from the current input stream.

3) public void close()throws is used to close the current input stream.


IOException
InputStream Hierarchy

STREAM CLASSES
The java.io package contains Stream classes which provide capabilities for processing all types of
data.
Java performs I/O through Streams. A Stream is linked to a physical layer by java I/O system to
make input and output operation in java.
In general, a stream means continuous flow of data. Streams are clean way to deal with input/output
without having every part of your code understand the physical. All these streams represent an input
source and an output destination. The stream in the java.io package supports many data such as
primitives, Object, localized characters, etc.

These classes may be categorized into two groups based on their data type handling capabilities:-
1. Byte Stream
2. Charater Stream

The InputStream & OutputStream class providing support for handling I/O operations on bytes are
type of byte stream.

The Reader & Writer class providing support for handling I/O operations on characters are type of
char stream. Character stream uses Unicode and therefore can be internationalized.

BYTE STREAM AND CHARACTER STREAM


ByteStream classes are to provide an surrounding to handle byte-oriented data or I/O.
ByteStream classes are having 2 abstract class InputStream & OutputStream.
These two abstract classes have several concrete classes that handle various devices such as disk
files, network connection etc.

InputStream
- It is an abstract class in which defines an interface known as Closable interface.
- Methods in this class will throw an IOException.

OutputStream
- It is an abstract class in which defines an interface known as Closable & Flushable interface.
- Methods in this class returns void and throws IOException.

methods by InputStream abstract class


int read()
it returns integer of next available input of bye

int read(byte buffer[ ])


it read up to the length of byte in buffer

int read(byte buffer[ ], int offset, int numBytes)


it read up to the length of byte in buffer starting form offset

int available()
It gets the no.of bytes of input available.

void reset()
it reads the input pointer.

long skip(long numBytes)


it returns the bytes ignored.

void close()
close the source of input.

methods by OutputStream abstract class


void write(int b)
it writes a single byte in output.

void write(byte buffer[ ])


it write a full array of bytes to an output stream

void write(byte buffer[ ], int offset, int numBytes)


it writes a range of numBytes from array buffer starting at buffer offset.

void close()
close the source of output

void flush()
it marks a point to input stream that will stay valid until numBytes are read.
Java Character streams are used to perform input and output for 16-bit unicode. Though there are
many classes related to character streams but the most frequently used classes are , Reader and
Writer.
Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here
major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a
time.

Reader
- It is an abstract class used to input character.
- It implements 2 interfaces Closable & Readable.
- Methods in this class throws IO Exception(Except markSupported()).

Writer
- Writer is an abstract class used to output character.
- It implements 3 interfaces Closable, Flushable & Appendable.

methods of Reader abstract class


abstract void close()
close the source of input.

void mark(int numChars)


it marks a point to input stream that will stay valid until numChars are read.

boolean markSupported()
it returns true if mark() support by stream.

int read()
it returns integer of next available character from input

int read(char buffer[])


it read up to the length of byte in buffer

abstract read(byte buffer[], int offset, int numBytes)


it read up to the length of character in buffer starting form offset

void reset()
it reads the input pointer.

long skip(long numBytes)


it returns the character ignored.

boolean ready()
if input request will not wait returns true, otherwise false

methods of Writer abstract class


write append(char ch) 
It append the 'ch' character at last of output stream

write append(charSequence chars)


append the chars at end of output stream.

write append(charSequence chars, int begin, int end)


append the characters specified in the range.

abstract void close()


close the output stream

abstract void flush()


it flush the output buffer.

void write(char buffer[])


writes array of character to output stream.

abstract void write(char buffer[], int offset, int numChars)


it writes a range of characters from buffer starting form offset

void write(String str)


it writes the str to output stream

void write(String str, int offset, int numChars)


it writes a range of character from the string str starting at offset.
STANDARD SREAMS
Java provides following three standard streams

Standard Input:
This is used to feed the data to user's program and usually a keyboard is used as standard input
stream and represented as System.in.

Standard Output:
This is used to output the data produced by the user's program and usually a computer screen is
used to standard output stream and represented as System.out.

Standard Error:
This is used to output the error data produced by the user's program and usually a computer screen
is used to standard error stream and represented as System.err.

You might also like