Page 362 - Cs_withBlue_J_C11_Flipbook
P. 362

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.
                     writeBytes()                   It writes a string to an output stream as a sequence of bytes.
                     writeChar()                    It writes a char to an output stream as a 2-byte value.
                     writeChars()                   It writes a string to an output stream as a sequence of characters.
                     writeFloat()                   It writes float-type data to an output stream.
                     writeInt()                     It writes integer data to an output stream.

                     writeDouble()                  It writes double-type data to an output stream.
                     writeLong()                    It writes long-type data to an output stream.
                     writeShort()                   It writes a short to the underlying output stream as two bytes, high byte first.
                     writeUTF()                     It writes String data to an output stream.


              12.4.2 Character Stream
              The character stream classes are used to create text files. These classes can handle 16-bit Unicode characters. They are
              more advanced than byte stream classes as the byte stream classes can only handle 8-bit bytes and are not compatible
              with the Unicode characters.
              The input and output operations are handled by abstract classes named Reader and Writer respectively belonging
              to java.io package. However, these classes cannot be directly used in the program. Some other classes which are
              inherited from these classes are used to perform different read/write operations. The following diagram shows the
              different classes inherited from the Reader and Writer classes.

                                                         Character Stream Classes






                                       Reader                                                Writer





                BufferedReader       FileReader      InputStreamReader     PrintWriter   BufferedWriter    FileWriter

              Let us learn about them in detail.

              Reader Class
              The Reader class is abstract in nature which means it cannot be instantiated but it has subclasses that override its
              methods and can read 16-bit characters from InputStream.
              Some important classes that are inherited from the Reader class are described in the following table.

                                  Class                                      Description
                      BufferedReader                 It has methods to read characters from the buffer.
                      FileReader                     It has methods to read characters from the file.
                      InputStreamReader              It has methods to convert byte streams to character streams.


                360360  Touchpad Computer Science-XI
   357   358   359   360   361   362   363   364   365   366   367