< Prev
Next >



Stream Classes



In Java, Stream is a channel or a path along which data flows between source and destination. There are two kinds of streams - input stream and output stream. Input stream is a path through which data is read or fetched from a source(file, memory, or a console). While, output stream is a path which is used to write some data to a destination(file, memory or a console).

Based on the type of data that we want to read, Java has two Stream classes - Byte Stream Classes and Character Stream Classes





Byte Stream Classes


Byte Stream Classes are used to read bytes from an input stream and write bytes to an output stream.

Byte Stream Classes are in divided in two groups - Please see the diagram below -


Let's learn a little bit more about the two abstract classes InputStream and OutputStream




InputStream


InputStream class is a base class of all the classes that are used to read bytes from a file, memory or console. InputStream is an abstract class and hence we can't create its object but we can use its subclasses for reading bytes from the input stream. We will discuss subclasses of InputStream in the next few articles with examples.





Methods of InputStream class.


These methods are inherited by InputStream subclasses.
Methods Description
int available() This method returns the number of bytes that can be read from the input stream.
abstract int read() This method reads the next byte out of the input stream.
int read(byte[] b) This method reads a chunk of bytes from the input stream and store them in its byte array, b.
close() This method closes this output stream and also frees any resources connected with this output stream.





OutputStream


OutputStream class is a base class of all the classes that are used to write bytes to a file, memory or console. OutputStream is an abstract class and hence we can't create its object but we can use its subclasses for writing bytes to the output stream. In the diagram below we have shown the hierarchy of OutputStream class and some of its important subclasses that are used to write bytes.





Methods of OutputStream class.


Methods of OutputStream class provide support for writing bytes to the output stream. As this is an abstract class. Hence, some undefined abstract methods are defined in the subclasses of OutputStream.
Methods Description
flush() This method flushes the output steam by forcing out buffered bytes to be written out.
abstract write(int c) This method writes byte(contained in an int) to the output stream.
write(byte[] b) This method writes a whole byte array(b) to the output.
close() This method closes this output stream and also frees any resources connected with this output stream.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< StringBuilder vs StringBuffer
What is File?>
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2