Thursday 28 November 2013

IO package in Java



IO package in Java :

The Java Input/Output (I/O) is a part of java . io package. The java . io  package contains a relatively large number of classes that support  input and output operations. The classes in the package are primarily abstract classes and stream-oriented that define methods and subclasses which allow bytes to be read from and written to files or other input and output sources. The InputStream and OutputStream  are central classes in the package which are used for reading from and writing to byte streams, respectively.

Input and Output - Source and Destination :

The terms "input" and "output" can sometimes be a bit confusing. The input of one part of an application is often the output of another. Is an OutputStream a stream where output is written to, or output comes out from (for you to read)? After all, an InputStream outputs its data to the reading program, doesn't it? Personally, I found this a bit confusing back in the day when I first started out learning about Java IO.

In an attempt to clear out this possible confusion, I have tried to put some different names on input and output to try to link them conceptually to where the input comes from, and where the output goes.

Java's IO package mostly concerns itself with the reading of raw data from a source and writing of raw data to a destination. The most typical sources and destinations of data are these:

   - Files
   - Pipes
   - Network Connections
   - In-memory Buffers (e.g. arrays)
   - System.in, System.out, System.error

 


InputStream :

The InputStream class is used for reading the data such as a byte and array of bytes from an input source. An input source can be a file, a string, or memory that may contain the data. It is an abstract class that defines the programming interface for all input streams that are inherited from it. An input stream is automatically opened when you create it. You cans explicitly close a stream with the close( ) method, or let it be closed implicitly when the object is found as a garbage.

The subclasses inherited from the InputStream class can be seen in a hierarchy manner shown below:

InputStream is inherited from the Object class. Each class of the InputStreams provided by the java.io package is intended for a different purpose.

OutputStream :

The OutputStream class is a sibling to InputStream that is used for writing byte and array of bytes to an output source. Similar to input sources, an output source can be anything such as a file, a string, or memory containing the data. Like an input stream, an output stream is automatically opened when you create it. You can explicitly close an output stream with the close( ) method, or let it be closed implicitly when the object is garbage collected.

The classes inherited from the OutputStream class can be seen in a hierarchy structure shown below:

How Files and Streams Work :

Java uses streams to handle I/O operations through which the data is flowed from one location to another. For example, an InputStream can flow the data from a disk file to the  internal memory and an OutputStream can flow the data from the internal memory to a disk file. The disk-file may be a text file or a binary file. When we work with a text file,  we use a character stream where one character is treated as per byte on disk. When we work with a binary file,  we use a binary stream.

The working process of the I/O streams can be shown in the given diagram.






Java IO Purposes and Features :

The Java IO classes, which mostly consists of streams and readers / writers, are addressing various purposes. That is why there are so many different classes. The purposes addressed are summarized below:

   - File Access,
   - Network Access,
   - Internal Memory Buffer Access,
   - Inter-Thread Communication (Pipes),
   - Buffering,
   - Filtering,
   - Parsing,
   - Reading and Writing Text (Readers / Writers)
   - Reading and Writing Primitive Data (long, int etc.)
   - Reading and Writing Objects



These purposes are nice to know about when reading through the Java IO classes. They make it somewhat easier to understand what the classes are targeting.


IO package in Java.

-- 
Regards,

Preeti Bagad [BE(CS)] 
SW Engineer Cum Blogger

On Line Assistence :
Y! Messenger : PreetiB.A1Soft@yahoo.com

No comments:

Post a Comment