Page 335 - computer science (868) class 11
P. 335

The FileInputStream Class
                 The FileInputStream class has methods to read bytes from a file. It is meant for reading streams of raw bytes such as
                 image data. The following table shows the description of the commonly used methods of the FileInputStream class.

                                         Method                                   Description
                           close()                              It closes the current file input stream.
                           read()                               It reads a byte of data from this input stream.


                 The DataInputStream Class
                 The DataInputStream class has methods to read primitive data types from an underlying input stream. It is not directly
                 inherited from the InputStream class. It is inherited from the FileInputStream class. The following table shows the
                 description of the commonly used methods of the DataInputStream class.

                                   Method                                    Description

                          read()                     It reads the number of bytes from the input stream.
                          readBoolean()              It reads one input byte and returns true if that byte is nonzero, false if
                                                     that byte is zero.
                          readByte()                 It reads and returns one input byte.
                          readChar()                 It reads two input bytes and returns a char value.
                          readDouble()               It reads eight input bytes and returns a double value.
                          readFloat()                It reads four input bytes and returns a float value.

                          readInt()                  It reads four input bytes and returns an int value.
                          readLong()                 It reads eight input bytes and returns a long value.
                          readShort()                It reads two input bytes and returns a short value.
                          readUTF()                  It reads from the stream in a representation of a Unicode character string.

                 The OutputStream Class
                 The OutputStream class is also abstract in nature. So, we cannot create its objects. But the classes derived from it can
                 write a stream of bytes. Let us learn about two main classes named FileOutputStream and DataOutputStream which
                 inherit the OutputStream class.


                 The FileOutputStream Class
                 The FileOutputStream  class has methods  to write  bytes  to a file.  The most commonly used methods  of the
                 FileOutputStream class are listed in the following table.

                                      Method                                   Description

                           close()                        It closes the current file output stream and releases any system
                                                          resources associated with this stream.

                           write()                        It writes the specified byte to the file output stream. There are
                                                          two overloaded methods for write() method.


                 The DataOutputStream Class
                 The DataOutputStream class has methods to write primitive data types to a file. The most commonly used methods of
                 the DataOutputStream class are listed in the following table:


                                  Method                                        Purpose
                       writeByte()                    It writes a byte to an output stream as a 1-byte value.


                                                                                                                       333
                                                                                                    Basic Input/Output   333
   330   331   332   333   334   335   336   337   338   339   340