Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Binary I/O classes
Unit-5 I/O programming
Introduction Java supports an hierarchy of input and output stream classes
for binary I/O. Following fig. show lists some of the classes for performing
binary I/O. InputStream is the root for binary input classes, and OutputStream is the root for binary output classes.
Streams The java.io package provides separate classes for reading and writing data
(byte and character data), based on Streams. Stream is a continuous flow of data. In Java, streams for both types of data have been defined: byte stream
classes and character stream classes. Byte stream classes deal with reading and writing of bytes to files,
socket, etc. Character Stream classes deal with reading and writing of characters
to files, socket, etc. The java.io package contains two top level byte stream
abstract classes: java.io.InputStream (for reading bytes) and java.io.OutputStream (for writing bytes).
It also contains two other top level character stream abstractclasses: java.io.Reader (for reading characters) and java.io.Writer (for writing characters).
The subclasses of these classes are actually used for readingand writing data.
methods for InputStream class
methods for OutputStream classOutputStream is an abstract class for streaming the byte output. All these methods under this class are of void type. Various methods defined by this class are as follows
FileInputStream/FileOutputStream FileInputStream/FileOutputStream is for reading/writing bytes
from/to files. All the methods in these classes are inherited from InputStream and
OutputStream. FileInputStream/FileOutputStream does not introduce new
methods. To construct a FileInputStream, use the following constructors, as
shown in Figure 19.6:
Program: FileInputStream
FileOutputStream The FileOutputStream can be used to write the data
to the file using the OutputStream. The constructors are -
Program:FileOutputStream
FilterInputStream/FilterOutputStream
DataInputStream/DataOutputStream
DataInputStream reads bytes from the stream andconverts them into appropriate primitive data typeswhereas the DataOutputStream converts the primitive datatypes into the bytes and then writes these bytes to the stream.
The superclass of DataInputStream class isFilterInputStream and superclass ofDataOutputStream class is FilterOutputStream class.
DataInputStream/DataOutputStream
Program
Program
Program ExplanationNote that in above program, along with other simple read and write methods we haveused readUTF and writeUTF methods. Basically UTF is an encoding scheme that allowssystems to operate with both ASCII and Unicode. Normally all the operating systems useASCII and Java uses Unicode. The writeUTF method converts the string into a series ofBYTES in the UTF-8 format and writes them into a binary stream. The readUTF methodReads a string which is written using the writeUTF method.
BufferedlnputStream/BufferedOutputStream
Program
Program Explanation:The only change is we have added buffers. The creation of buffers is shown by the boldstatements in above program.
Inheritance hierarchy