Outputstream Vs Inputstream
Outputstream Vs Inputstream
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.
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.
InputStream class
InputStream class is an abstract class. It is the super class of all classes representing an
input stream of bytes.
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.
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.
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.
int available()
It gets the no.of bytes of input available.
void reset()
it reads the input pointer.
void close()
close the source of input.
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.
boolean markSupported()
it returns true if mark() support by stream.
int read()
it returns integer of next available character from input
void reset()
it reads the input pointer.
boolean ready()
if input request will not wait returns true, otherwise false
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.