Stream Based IO in Java
Java programs perform Input Output through streams. A stream is an abstraction that either produces or consumes information. A stream is simply a sequence of bytes that flows into or out of our program. It’s an abstract representation of an input or output device.
A stream is linked to a physical device by the Java IO stream.
All streams behave in the same manner even if the actual physical devices to which they are linked differ.
Thus, the same IO classes and method can be applied to any type of decvice.
This means that an input stream can abstract many different kinds of input:from a disk file, a keyboard or a network socket.
Java implements stream within class hierarchies define in the java.io package.
Types Of Streams:
1. Byte or Binary Stream.
2. Character Stream.
Byte/Binary Stream :
Byte stream provide a convenient means for handling IO of bytes.
Byte streams are used for example, when reading and writing binary data.
Byte streams are defined by using two class hierarchies:
At the top are two abstract classjava.io.InputStream and
java.io.OutputStream.
Each of these abstract classes has several concrete sub classes, that handle the differences between various devices.
The abstract classes InputStream and OutputStream have several key methods, that the other stream classes implement.
Two of the most important methods are read() and write(), which respectively read and write bytes of data.
int read() throws IOException
Returns the next byte available from the stream as type int. If the end of the stream is reached, the method will return the value -1.int read(byte [] array) throws IOException
This method reads bytes from the stream into successive elements of array. The maximum of array.length bytes will be read.void write(int data) throws IOException
void write(byte [] array) throws IOException
Character Stream :
-
Character streams provide a convenient means for handling IO of characters.
They use Unicode and therefore internationalized. Also, in some cases character stream are more efficient than byte stream.
Character streams are defined by using two class hierarchies.
At the top are two abstract classjava.io.Reader and
java.io.Writer.
The abstract classes Reader and Writer several key methods, that the other stream classes implement.
Two of the most important methods are read() and write().
Methods for performing character based reading operation:
int read( ) throws IOException
int read(char [] array) throws IOException
Methods for performing character based writing operation:
void write(int data) throws IOException
void write(char [] array) throws IOException
Standard Stream :
In java, 3 standard stream objects are implicitly available.
1. System.out : standard output stream,
Type: PrintStream,
Represents: system's console,
2. System.in : standard input stream,
Type: InputStream,
Represents: system's keyboard,
3. System.err : standard error stream,
Type: PrintStream,
Represents: system's console,
Next topic is file-handling
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in