SlideShare ist ein Scribd-Unternehmen logo
1 von 41
Chapter 19 Binary I/O




Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                     rights reserved.
                                                                                               1
Motivations
Data stored in a text file is represented in human-readable
form. Data stored in a binary file is represented in binary
form. You cannot read binary files. They are designed to
be read by programs. For example, Java source programs
are stored in text files and can be read by a text editor, but
Java classes are stored in binary files and are read by the
JVM. The advantage of binary files is that they are more
efficient to process than text files.




           Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                rights reserved.
                                                                                                          2
Objectives
   To discover how I/O is processed in Java (§19.2).
   To distinguish between text I/O and binary I/O (§19.3).
   To read and write bytes using FileInputStream and
    FileOutputStream (§19.4.1).
   To read and write primitive values and strings using
    DataInputStream/DataOutputStream (§19.4.3).
   To store and restore objects using ObjectOutputStream and
    ObjectInputStream, and to understand how objects are serialized
    and what kind of objects can be serialized (§19.6).
   To implement the Serializable interface to make objects
    serializable (§19.6.1).
   To serialize arrays (§19.6.2).
   To read and write the same file using the RandomAccessFile class
    (§19.7).
              Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                   rights reserved.
                                                                                                             3
How is I/O Handled in Java?
A File object encapsulates the properties of a file or a path, but does not
contain the methods for reading/writing data from/to a file. In order to
perform I/O, you need to create objects using appropriate Java I/O classes.
    Scanner input = new Scanner(new File("temp.txt"));
    System.out.println(input.nextLine());

                      Program
                                                       Input stream
                           Input object
                         created from an               01011…1001                     File
                            input class

                          Output object
                         created from an              11001…1011
                                                                                     File
                           output class
                                                      Output stream



    PrintWriter output = new PrintWriter("temp.txt");
    output.println("Java 101");
    output.close();
              Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                   rights reserved.
                                                                                                             4
Text File vs. Binary File
   Data stored in a text file are represented in human-readable
    form. Data stored in a binary file are represented in binary form.
    You cannot read binary files. Binary files are designed to be
    read by programs. For example, the Java source programs are
    stored in text files and can be read by a text editor, but the Java
    classes are stored in binary files and are read by the JVM. The
    advantage of binary files is that they are more efficient to
    process than text files.

   Although it is not technically precise and correct, you can
    imagine that a text file consists of a sequence of characters and a
    binary file consists of a sequence of bits. For example, the
    decimal integer 199 is stored as the sequence of three
    characters: '1', '9', '9' in a text file and the same integer is stored
    as a byte-type value C7 in a binary file, because decimal 199
    equals to hex C7.
               Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                    rights reserved.
                                                                                                              5
Binary I/O
Text I/O requires encoding and decoding. The JVM converts a
Unicode to a file specific encoding when writing a character and
coverts a file specific encoding to a Unicode when reading a
character. Binary I/O does not require conversions. When you write
a byte to a file, the original byte is copied into the file. When you
read a byte from a file, the exact byte in the file is returned.
                       Text I/O program

                           The Unicode of             Encoding/                      The encoding of the character
                (a)                                                                       is stored in the file
                           the character              Decoding
                       e.g. "199"                                                    00110001 00111001 00111001
                       ,
                                                                                       0x31      0x39       0x39

                       Binary I/O program

                (b)     A byte is read/written           The same byte in the file

                       e.g. 199                                00110111
                       ,
                                                                 0xC7




            Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                 rights reserved.
                                                                                                                     6
Binary I/O Classes
                                                FileInputStream
                                                                                             DataInputStream
           InputStream                         FilterInputStream
                                                                                            BufferedInputStream
                                               ObjectInputStream
Object
                                              FileOutputStream                               BufferedOutputStream

         OutputStream                          FilterOutputStream                           DataOutputStream

                                              ObjectOutputStream                             PrintStream




         Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                              rights reserved.
                                                                                                                  7
InputStream
                                                       The value returned is a byte as an int type.
    java.io.InputStream

+read(): int                      Reads the next byte of data from the input stream. The value byte is returned as
                                   an int value in the range 0 to 255. If no byte is available because the end of
                                   the stream has been reached, the value –1 is returned.
+read(b: byte[]): int             Reads up to b.length bytes into array b from the input stream and returns the
                                   actual number of bytes read. Returns -1 at the end of the stream.
+read(b: byte[], off: int,        Reads bytes from the input stream and stores into b[off], b[off+1], …,
 len: int): int                    b[off+len-1]. The actual number of bytes read is returned. Returns -1 at the
                                   end of the stream.
+available(): int                 Returns the number of bytes that can be read from the input stream.
+close(): void                    Closes this input stream and releases any system resources associated with the
                                   stream.
+skip(n: long): long              Skips over and discards n bytes of data from this input stream. The actual
                                   number of bytes skipped is returned.
+markSupported(): boolean Tests if this input stream supports the mark and reset methods.
+mark(readlimit: int): void Marks the current position in this input stream.
+reset(): void                    Repositions this stream to the position at the time the mark method was last
                                   called on this input stream.
                        Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                             rights reserved.
                                                                                                                       8
OutputStream

                                                     The value is a byte as an int type.

 java.io.OutputStream

+write(int b): void           Writes the specified byte to this output stream. The parameter b is an int value.
                               (byte)b is written to the output stream.
+write(b: byte[]): void       Writes all the bytes in array b to the output stream.
+write(b: byte[], off: int, Writes b[off], b[off+1], …, b[off+len-1] into the output stream.
 len: int): void
+close(): void                Closes this input stream and releases any system resources associated with the
                               stream.
+flush(): void                Flushes this output stream and forces any buffered output bytes to be written out.




                      Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                           rights reserved.
                                                                                                                     9
FileInputStream/FileOutputStream
                                                   FileInputStream
                                                                                                DataInputStream
              InputStream                         FilterInputStream
                                                                                               BufferedInputStream
                                                  ObjectInputStream
 Object
                                                 FileOutputStream                               BufferedOutputStream

            OutputStream                          FilterOutputStream                           DataOutputStream

                                                 ObjectOutputStream                             PrintStream


FileInputStream/FileOutputStream
associates a binary input/output stream with
an external file. All the methods in
FileInputStream/FileOuptputStream are
inherited from its superclasses.
            Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                 rights reserved.
                                                                                                                     10
FileInputStream
To construct a FileInputStream, use the following
constructors:
   public FileInputStream(String filename)
   public FileInputStream(File file)

   A java.io.FileNotFoundException would occur if you attempt to
create a FileInputStream with a nonexistent file.




           Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                rights reserved.
                                                                                                          11
FileOutputStream
To construct a FileOutputStream, use the following constructors:

    public FileOutputStream(String filename)
    public FileOutputStream(File file)
    public FileOutputStream(String filename, boolean append)
    public FileOutputStream(File file, boolean append)


If the file does not exist, a new file would be created. If the file already
exists, the first two constructors would delete the current contents in
the file. To retain the current content and append new data into the
file, use the last two constructors by passing true to the append
parameter.

                                                                TestFileStream                                Run

               Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                    rights reserved.
                                                                                                                    12
FilterInputStream/FilterOutputStream
                                                        FileInputStream
                                                                                                     DataInputStream
                   InputStream                         FilterInputStream
                                                                                                    BufferedInputStream
                                                       ObjectInputStream
 Object
                                                      FileOutputStream                               BufferedOutputStream

                 OutputStream                          FilterOutputStream                           DataOutputStream

                                                      ObjectOutputStream                             PrintStream


Filter streams are streams that filter bytes for some purpose. The basic byte input
stream provides a read method that can only be used for reading bytes. If you want to
read integers, doubles, or strings, you need a filter class to wrap the byte input stream.
Using a filter class enables you to read integers, doubles, and strings instead of bytes
and characters. FilterInputStream and FilterOutputStream are the base classes for
filtering data. When you need to process primitive numeric types, use DatInputStream
and DataOutputStream to filter bytes.
                 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                      rights reserved.
                                                                                                                          13
DataInputStream/DataOutputStream
                                                 DataInputStream reads bytes from the stream
                                                 and converts them into appropriate primitive
                                                 type values or strings.

                                                 FileInputStream
                                                                                              DataInputStream
            InputStream                         FilterInputStream
                                                                                              BufferedInputStream
                                                 ObjectInputStream
Object
                                                FileOutputStream                              BufferedOutputStream

          OutputStream                          FilterOutputStream                            DataOutputStream

                                                ObjectOutputStream                            PrintStream


                       DataOutputStream converts primitive type values
                       or strings into bytes and output the bytes to the
                       stream.
         Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                              rights reserved.
                                                                                                                 14
DataInputStream
                                   DataInputStream extends FilterInputStream
                                   and implements the DataInput interface.
   InputStream                           java.io.DataInput

                                   +readBoolean(): boolean Reads a Boolean from the input stream.
                                   +readByte(): byte       Reads a byte from the input stream.
 FilterInputStream
                                   +readChar(): char                       Reads a character from the input stream.
                                   +readFloat(): float                     Reads a float from the input stream.
                                   +readDouble(): float                    Reads a double from the input stream.
 DataInputStream                   +readInt(): int                         Reads an int from the input stream.
+DataInputStream(                  +readLong(): long                       Reads a long from the input stream.
  in: InputStream)                 +readShort(): short                     Reads a short from the input stream.
                                   +readLine(): String                     Reads a line of characters from input.
                                   +readUTF(): String                      Reads a string in UTF format.


               Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                    rights reserved.
                                                                                                                 15
DataOutputStream
DataOutputStream extends FilterOutputStream and implements the
DataOutput interface.
       OutputStream                         java.io.DataOutput

                                   +writeBoolean(b: Boolean): void Writes a Boolean to the output stream.
                                   +writeByte(v: int): void        Writes to the output stream the eight low-order bits
    FilterOutputStream                                              of the argument v.
                                   +writeBytes(s: String): void    Writes the lower byte of the characters in a string to
                                                                    the output stream.
                                   +writeChar(c: char): void       Writes a character (composed of two bytes) to the
     DataOutputStream                                               output stream.
   +DataOutputStream(              +writeChars(s: String): void    Writes every character in the string s, to the output
     out: OutputStream)                                             stream, in order, two bytes per character.
                                   +writeFloat(v: float): void     Writes a float value to the output stream.
                                   +writeDouble(v: float): void                Writes a double value to the output stream.
                                   +writeInt(v: int): void                     Writes an int value to the output stream.
                                   +writeLong(v: long): void                   Writes a long value to the output stream.
                                   +writeShort(v: short): void                 Writes a short value to the output stream.
                                   +writeUTF(s: String): void                  Writes two bytes of length information to the output
                                                                                stream, followed by the UTF representation of
                                                                                every character in the string s.

                   Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                        rights reserved.
                                                                                                                            16
Characters and Strings in Binary I/O
A Unicode consists of two bytes. The writeChar(char c) method
writes the Unicode of character c to the output. The
writeChars(String s) method writes the Unicode for each character in
the string s to the output.

Why UTF-8? What is UTF-8?

UTF-8 is a coding scheme that allows systems to operate with both
ASCII and Unicode efficiently. Most operating systems use ASCII.
Java uses Unicode. The ASCII character set is a subset of the
Unicode character set. Since most applications need only the ASCII
character set, it is a waste to represent an 8-bit ASCII character as a
16-bit Unicode character. The UTF-8 is an alternative scheme that
stores a character using 1, 2, or 3 bytes. ASCII values (less than
0x7F) are coded in one byte. Unicode values less than 0x7FF are
coded in two bytes. Other Unicode values are coded in three bytes.
             Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                  rights reserved.
                                                                                                            17
Using DataInputStream/DataOutputStream
Data streams are used as wrappers on existing input and output
streams to filter data in the original stream. They are created using the
following constructors:
   public DataInputStream(InputStream instream)
   public DataOutputStream(OutputStream outstream)


The statements given below create data streams. The first statement
creates an input stream for file in.dat; the second statement creates an
output stream for file out.dat.
   DataInputStream infile =
    new DataInputStream(new FileInputStream("in.dat"));
   DataOutputStream outfile =
    new DataOutputStream(new FileOutputStream("out.dat"));

                                                             TestDataStream                                  Run
              Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                   rights reserved.
                                                                                                                   18
Order and Format
CAUTION: You have to read the data in the same order and same
format in which they are stored. For example, since names are written
in UTF-8 using writeUTF, you must read names using readUTF.



                            Checking End of File
 TIP: If you keep reading data at the end of a stream, an EOFException
 would occur. So how do you check the end of a file? You can use
 input.available() to check it. input.available() == 0 indicates that it is
 the end of a file.


             Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                  rights reserved.
                                                                                                            19
BufferedInputStream/
BufferedOutputStream                                                          Using buffers to speed up I/O

                                                     FileInputStream
                                                                                                  DataInputStream
                InputStream                         FilterInputStream
                                                                                                  BufferedInputStream
                                                     ObjectInputStream
  Object
                                                    FileOutputStream                              BufferedOutputStream

              OutputStream                          FilterOutputStream                            DataOutputStream

                                                    ObjectOutputStream                            PrintStream


BufferedInputStream/BufferedOutputStream does not contain new
methods. All the methods BufferedInputStream/BufferedOutputStream are
inherited from the InputStream/OutputStream classes.
             Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                  rights reserved.
                                                                                                                     20
Constructing
BufferedInputStream/BufferedOutputStream
 // Create a BufferedInputStream
 public BufferedInputStream(InputStream in)
 public BufferedInputStream(InputStream in, int bufferSize)

 // Create a BufferedOutputStream
 public BufferedOutputStream(OutputStream out)
 public BufferedOutputStream(OutputStreamr out, int bufferSize)




          Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                               rights reserved.
                                                                                                         21
Case Studies: Copy File
This case study develops a program that copies files. The user needs
to provide a source file and a target file as command-line arguments
using the following command:


       java Copy source target



The program copies a source file to a target file and displays the
number of bytes in the file. If the source does not exist, tell the user
the file is not found. If the target file already exists, tell the user the
file already exists.
                                                                                   Copy                      Run

              Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                   rights reserved.
                                                                                                                   22
Optional
                                         Object I/O
              DataInputStream/DataOutputStream enables you to perform I/O for
              primitive type values and strings.
              ObjectInputStream/ObjectOutputStream enables you to perform I/O
              for objects in addition for primitive type values and strings.

                                                       FileInputStream
                                                                                                    DataInputStream
                  InputStream                         FilterInputStream
                                                                                                    BufferedInputStream
                                                       ObjectInputStream
     Object
                                                      FileOutputStream                              BufferedOutputStream

                OutputStream                          FilterOutputStream                            DataOutputStream

                                                      ObjectOutputStream                            PrintStream



               Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                    rights reserved.
                                                                                                                       23
ObjectInputStream
ObjectInputStream extends InputStream and
implements ObjectInput and ObjectStreamConstants.


         java.io.InputStream                                           ObjectStreamConstants


                                                                            java.io.DataInput


      java.io.ObjectInputStream                                            java.io.ObjectInput

+ObjectInputStream(in: InputStream)                                   +readObject(): Object               Reads an object.




           Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                rights reserved.
                                                                                                                 24
ObjectOutputStream
 ObjectOutputStream extends OutputStream and
 implements ObjectOutput and ObjectStreamConstants.


         java.io.OutputStream                                       ObjectStreamConstants


                                                                       java.io.DataOutput


      java.io.ObjectOutputStream                                     java.io.ObjectOutput

+ObjectOutputStream(out: OutputStream)                        +writeObject(o: Object): void Writes an object.




               Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                    rights reserved.
                                                                                                              25
Using Object Streams
You may wrap an ObjectInputStream/ObjectOutputStream on any
InputStream/OutputStream using the following constructors:

   // Create an ObjectInputStream
   public ObjectInputStream(InputStream in)

   // Create an ObjectOutputStream
   public ObjectOutputStream(OutputStream out)


                                      TestObjectOutputStream                                               Run

                                        TestObjectInputStream                                              Run

            Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                 rights reserved.
                                                                                                                 26
The Serializable Interface
Not all objects can be written to an output stream. Objects that can be
written to an object stream is said to be serializable. A serializable
object is an instance of the java.io.Serializable interface. So the class
of a serializable object must implement Serializable.

The Serializable interface is a marker interface. It has no methods, so
you don't need to add additional code in your class that implements
Serializable.

Implementing this interface enables the Java serialization mechanism
to automate the process of storing the objects and arrays.


             Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                  rights reserved.
                                                                                                            27
The transient Keyword
If an object is an instance of Serializable, but it contains
non-serializable instance data fields, can the object be
serialized? The answer is no. To enable the object to be
serialized, you can use the transient keyword to mark these
data fields to tell the JVM to ignore these fields when
writing the object to an object stream.




           Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                rights reserved.
                                                                                                          28
The transient Keyword, cont.
Consider the following class:

public class Foo implements java.io.Serializable {
  private int v1;
  private static double v2;
  private transient A v3 = new A();
}
class A { } // A is not serializable


When an object of the Foo class is serialized, only variable v1 is
serialized. Variable v2 is not serialized because it is a static variable,
and variable v3 is not serialized because it is marked transient. If v3
were not marked transient, a java.io.NotSerializableException would
occur.

             Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                  rights reserved.
                                                                                                            29
Serializing Arrays
An array is serializable if all its elements are serializable.
So an entire array can be saved using writeObject into a file
and later restored using readObject. Listing 16.12 stores an
array of five int values an array of three strings, and an
array of two JButton objects, and reads them back to
display on the console.


                               TestObjectStreamForArray                                                   Run




           Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                rights reserved.
                                                                                                                30
Random Access Files
All of the streams you have used so far are known as
read-only or write-only streams. The external files of
these streams are sequential files that cannot be updated
without creating a new file. It is often necessary to
modify files or to insert new records into files. Java
provides the RandomAccessFile class to allow a file to be
read from and write to at random locations.




          Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                               rights reserved.
                                                                                                         31
RandomAccessFile
      DataInput                       DataInput


      java.io.RandomAccessFile

+RandomAccessFile(file: File, mode:            Creates a RandomAccessFile stream with the specified File object and
 String)                                        mode.
+RandomAccessFile(name: String,                Creates a RandomAccessFile stream with the specified file name
 mode: String)                                  string and mode.
+close(): void                                 Closes the stream and releases the resource associated with the stream.
+getFilePointer(): long                        Returns the offset, in bytes, from the beginning of the file to where the
                                                next read or write occurs.
+length(): long                                Returns the length of this file.
+read(): int                                   Reads a byte of data from this file and returns –1 an the end of stream.
+read(b: byte[]): int                          Reads up to b.length bytes of data from this file into an array of bytes.
+read(b: byte[], off: int, len: int) : int     Reads up to len bytes of data from this file into an array of bytes.
+seek(long pos): void                          Sets the offset (in bytes specified in pos) from the beginning of the
                                                stream to where the next read or write occurs.
+setLength(newLength: long): void              Sets a new length of this file.
+skipBytes(int n): int                         Skips over n bytes of input discarding the skipped bytes.
+write(b: byte[]): void                        Writes b.length bytes from the specified byte array to this file, starting
+write(byte b[], int off, int len)              at the current file pointer.
+write(b: byte[], off: int, len: int):         Writes len bytes from the specified byte array starting at offset off to
 void                                           this file.
                  Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                       rights reserved.
                                                                                                                       32
File Pointer
A random access file consists of a sequence of bytes. There is a
special marker called file pointer that is positioned at one of these
bytes. A read or write operation takes place at the location of the file
pointer. When a file is opened, the file pointer sets at the beginning of
the file. When you read or write data to the file, the file pointer moves
forward to the next data. For example, if you read an int value using
readInt(), the JVM reads four bytes from the file pointer and now the
file pointer is four bytes ahead of the previous location.
                  file pointer


file    byte byte …      byte byte byte byte byte              …       byte byte byte byte byte              (A) Before readInt()


                                                file pointer


file   byte byte …       byte byte byte byte byte              …       byte byte byte byte byte              (B) Before readInt()


              Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                   rights reserved.
                                                                                                                         33
RandomAccessFile Methods
Many methods in RandomAccessFile are the same as
those in DataInputStream and DataOutputStream.
For example, readInt(), readLong(),
writeDouble(), readLine(), writeInt(), and
writeLong() can be used in data input stream or data
output stream as well as in RandomAccessFile
streams.




         Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                              rights reserved.
                                                                                                        34
RandomAccessFile Methods, cont.
   void seek(long pos) throws IOException;
    Sets the offset from the beginning of the
    RandomAccessFile stream to where the next read
    or write occurs.

   long getFilePointer() IOException;
    Returns the current offset, in bytes, from the
    beginning of the file to where the next read
    or write occurs.



            Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                 rights reserved.
                                                                                                           35
RandomAccessFile Methods, cont.
   long length()IOException
    Returns the length of the file.

   final void writeChar(int v) throws
    IOException
    Writes a character to the file as a two-byte Unicode,
    with the high byte written first.

   final void writeChars(String s)
    throws IOException
    Writes a string to the file as a sequence of
    characters.
          Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                               rights reserved.
                                                                                                         36
RandomAccessFile Constructor
RandomAccessFile raf =
  new RandomAccessFile("test.dat", "rw");
  //allows read and write

RandomAccessFile raf =
  new RandomAccessFile("test.dat", "r");
  //read only




       Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                            rights reserved.
                                                                                                      37
A Short Example on
         RandomAccessFile



TestRandomAccessFile

               Run




   Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                        rights reserved.
                                                                                                  38
Companion
Website
            Case Studies: Address Book
Now let us use RandomAccessFile to create a useful
project for storing and viewing and address book. The user
interface of the program is shown in Figure 16.24. The
Add button stores a new address to the end of the file. The
First, Next, Previous, and Last buttons retrieve the first,
next, previous, and last addresses from the file,
respectively.




             Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                  rights reserved.
                                                                                                            39
Companion
Website          Fixed Length String I/O
   Random access files are often used to process files of records. For
   convenience, fixed-length records are used in random access files
   so that a record can be located easily. A record consists of a fixed
   number of fields. A field can be a string or a primitive data type. A
   string in a fixed-length record has a maximum size. If a string is
   smaller than the maximum size, the rest of the string is padded with
   blanks.

file        Record 1       Record 2                Record n                            Student 1          Student 2   Student n
                                                                         e.g.,


            Field1 Field 2 … Field k                                                   name street city state zip




                                                              FixedLengthStringIO
                  Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                       rights reserved.
                                                                                                                      40
Companion
Website     Address Implementation
 The rest of the work can be summarized in the following steps:

    Create the user interface.

    Add a record to the file.

    Read a record from the file.

   Write the code to implement the button actions.

                                                                              AddressBook

                                                                                         Run
             Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All
                                                  rights reserved.
                                                                                                            41

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (13)

CMIS and Interoperability - AIIM 2009
CMIS and Interoperability - AIIM 2009CMIS and Interoperability - AIIM 2009
CMIS and Interoperability - AIIM 2009
 
Java Programming Guide Quick Reference
Java Programming Guide Quick ReferenceJava Programming Guide Quick Reference
Java Programming Guide Quick Reference
 
Oop
OopOop
Oop
 
85305524 i-t-case-study
85305524 i-t-case-study85305524 i-t-case-study
85305524 i-t-case-study
 
Team G
Team GTeam G
Team G
 
CORBA
CORBACORBA
CORBA
 
Memory models in c#
Memory models in c#Memory models in c#
Memory models in c#
 
Middleware1
Middleware1Middleware1
Middleware1
 
Core java questions
Core java questionsCore java questions
Core java questions
 
Corba concepts & corba architecture
Corba concepts & corba architectureCorba concepts & corba architecture
Corba concepts & corba architecture
 
CORBA Basic and Deployment of CORBA
CORBA Basic and Deployment of CORBACORBA Basic and Deployment of CORBA
CORBA Basic and Deployment of CORBA
 
Corba
CorbaCorba
Corba
 
Common Object Request Broker Architecture - CORBA
Common Object Request Broker Architecture - CORBACommon Object Request Broker Architecture - CORBA
Common Object Request Broker Architecture - CORBA
 

Andere mochten auch (20)

JavaYDL7
JavaYDL7JavaYDL7
JavaYDL7
 
JavaYDL3
JavaYDL3JavaYDL3
JavaYDL3
 
JavaYDL11
JavaYDL11JavaYDL11
JavaYDL11
 
JavaYDL4
JavaYDL4JavaYDL4
JavaYDL4
 
JavaYDL12
JavaYDL12JavaYDL12
JavaYDL12
 
JavaYDL6
JavaYDL6JavaYDL6
JavaYDL6
 
JavaYDL2
JavaYDL2JavaYDL2
JavaYDL2
 
JavaYDL1
JavaYDL1JavaYDL1
JavaYDL1
 
JavaYDL9
JavaYDL9JavaYDL9
JavaYDL9
 
11slide
11slide11slide
11slide
 
JavaYDL5
JavaYDL5JavaYDL5
JavaYDL5
 
History of languages'
History of languages'History of languages'
History of languages'
 
JavaYDL8
JavaYDL8JavaYDL8
JavaYDL8
 
10slide
10slide10slide
10slide
 
JavaOne 2013: Java 8 - The Good Parts
JavaOne 2013: Java 8 - The Good PartsJavaOne 2013: Java 8 - The Good Parts
JavaOne 2013: Java 8 - The Good Parts
 
History of java'
History of java'History of java'
History of java'
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Language
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 

Ähnlich wie JavaYDL19

BINARY IOObjectivesCHAPTER 19■ To discover how IO .docx
BINARY IOObjectivesCHAPTER 19■ To discover how IO .docxBINARY IOObjectivesCHAPTER 19■ To discover how IO .docx
BINARY IOObjectivesCHAPTER 19■ To discover how IO .docxhartrobert670
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Outputphanleson
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output ConceptsVicter Paul
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?kanchanmahajan23
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?kanchanmahajan23
 
Java session08
Java session08Java session08
Java session08Niit Care
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsAnton Keks
 
EclipseCon 2010 - JDT Fundamentals
EclipseCon 2010 - JDT FundamentalsEclipseCon 2010 - JDT Fundamentals
EclipseCon 2010 - JDT Fundamentalsdeepakazad
 
1.26 File Input-Output in JAVA.pptx
1.26 File Input-Output in JAVA.pptx1.26 File Input-Output in JAVA.pptx
1.26 File Input-Output in JAVA.pptxYashrajMohrir
 
Java programming Chapter 4.pptx
Java programming Chapter 4.pptxJava programming Chapter 4.pptx
Java programming Chapter 4.pptxssusera0d3d2
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3sotlsoc
 
File handling in input and output
File handling in input and outputFile handling in input and output
File handling in input and outputpradeepa velmurugan
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and filesMarcello Thiry
 

Ähnlich wie JavaYDL19 (20)

BINARY IOObjectivesCHAPTER 19■ To discover how IO .docx
BINARY IOObjectivesCHAPTER 19■ To discover how IO .docxBINARY IOObjectivesCHAPTER 19■ To discover how IO .docx
BINARY IOObjectivesCHAPTER 19■ To discover how IO .docx
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
 
Java session08
Java session08Java session08
Java session08
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Filehandling
FilehandlingFilehandling
Filehandling
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
EclipseCon 2010 - JDT Fundamentals
EclipseCon 2010 - JDT FundamentalsEclipseCon 2010 - JDT Fundamentals
EclipseCon 2010 - JDT Fundamentals
 
JDT Fundamentals 2010
JDT Fundamentals 2010JDT Fundamentals 2010
JDT Fundamentals 2010
 
1.26 File Input-Output in JAVA.pptx
1.26 File Input-Output in JAVA.pptx1.26 File Input-Output in JAVA.pptx
1.26 File Input-Output in JAVA.pptx
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Java programming Chapter 4.pptx
Java programming Chapter 4.pptxJava programming Chapter 4.pptx
Java programming Chapter 4.pptx
 
Java introduction
Java introductionJava introduction
Java introduction
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
File handling in input and output
File handling in input and outputFile handling in input and output
File handling in input and output
 
Java basic
Java basicJava basic
Java basic
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
 

Mehr von Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

Mehr von Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Kürzlich hochgeladen

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 

Kürzlich hochgeladen (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 

JavaYDL19

  • 1. Chapter 19 Binary I/O Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1
  • 2. Motivations Data stored in a text file is represented in human-readable form. Data stored in a binary file is represented in binary form. You cannot read binary files. They are designed to be read by programs. For example, Java source programs are stored in text files and can be read by a text editor, but Java classes are stored in binary files and are read by the JVM. The advantage of binary files is that they are more efficient to process than text files. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 2
  • 3. Objectives  To discover how I/O is processed in Java (§19.2).  To distinguish between text I/O and binary I/O (§19.3).  To read and write bytes using FileInputStream and FileOutputStream (§19.4.1).  To read and write primitive values and strings using DataInputStream/DataOutputStream (§19.4.3).  To store and restore objects using ObjectOutputStream and ObjectInputStream, and to understand how objects are serialized and what kind of objects can be serialized (§19.6).  To implement the Serializable interface to make objects serializable (§19.6.1).  To serialize arrays (§19.6.2).  To read and write the same file using the RandomAccessFile class (§19.7). Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3
  • 4. How is I/O Handled in Java? A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing data from/to a file. In order to perform I/O, you need to create objects using appropriate Java I/O classes. Scanner input = new Scanner(new File("temp.txt")); System.out.println(input.nextLine()); Program Input stream Input object created from an 01011…1001 File input class Output object created from an 11001…1011 File output class Output stream PrintWriter output = new PrintWriter("temp.txt"); output.println("Java 101"); output.close(); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 4
  • 5. Text File vs. Binary File  Data stored in a text file are represented in human-readable form. Data stored in a binary file are represented in binary form. You cannot read binary files. Binary files are designed to be read by programs. For example, the Java source programs are stored in text files and can be read by a text editor, but the Java classes are stored in binary files and are read by the JVM. The advantage of binary files is that they are more efficient to process than text files.  Although it is not technically precise and correct, you can imagine that a text file consists of a sequence of characters and a binary file consists of a sequence of bits. For example, the decimal integer 199 is stored as the sequence of three characters: '1', '9', '9' in a text file and the same integer is stored as a byte-type value C7 in a binary file, because decimal 199 equals to hex C7. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 5
  • 6. Binary I/O Text I/O requires encoding and decoding. The JVM converts a Unicode to a file specific encoding when writing a character and coverts a file specific encoding to a Unicode when reading a character. Binary I/O does not require conversions. When you write a byte to a file, the original byte is copied into the file. When you read a byte from a file, the exact byte in the file is returned. Text I/O program The Unicode of Encoding/ The encoding of the character (a) is stored in the file the character Decoding e.g. "199" 00110001 00111001 00111001 , 0x31 0x39 0x39 Binary I/O program (b) A byte is read/written The same byte in the file e.g. 199 00110111 , 0xC7 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 6
  • 7. Binary I/O Classes FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object FileOutputStream BufferedOutputStream OutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 7
  • 8. InputStream The value returned is a byte as an int type. java.io.InputStream +read(): int Reads the next byte of data from the input stream. The value byte is returned as an int value in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value –1 is returned. +read(b: byte[]): int Reads up to b.length bytes into array b from the input stream and returns the actual number of bytes read. Returns -1 at the end of the stream. +read(b: byte[], off: int, Reads bytes from the input stream and stores into b[off], b[off+1], …, len: int): int b[off+len-1]. The actual number of bytes read is returned. Returns -1 at the end of the stream. +available(): int Returns the number of bytes that can be read from the input stream. +close(): void Closes this input stream and releases any system resources associated with the stream. +skip(n: long): long Skips over and discards n bytes of data from this input stream. The actual number of bytes skipped is returned. +markSupported(): boolean Tests if this input stream supports the mark and reset methods. +mark(readlimit: int): void Marks the current position in this input stream. +reset(): void Repositions this stream to the position at the time the mark method was last called on this input stream. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 8
  • 9. OutputStream The value is a byte as an int type. java.io.OutputStream +write(int b): void Writes the specified byte to this output stream. The parameter b is an int value. (byte)b is written to the output stream. +write(b: byte[]): void Writes all the bytes in array b to the output stream. +write(b: byte[], off: int, Writes b[off], b[off+1], …, b[off+len-1] into the output stream. len: int): void +close(): void Closes this input stream and releases any system resources associated with the stream. +flush(): void Flushes this output stream and forces any buffered output bytes to be written out. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 9
  • 10. FileInputStream/FileOutputStream FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object FileOutputStream BufferedOutputStream OutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream FileInputStream/FileOutputStream associates a binary input/output stream with an external file. All the methods in FileInputStream/FileOuptputStream are inherited from its superclasses. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 10
  • 11. FileInputStream To construct a FileInputStream, use the following constructors: public FileInputStream(String filename) public FileInputStream(File file) A java.io.FileNotFoundException would occur if you attempt to create a FileInputStream with a nonexistent file. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 11
  • 12. FileOutputStream To construct a FileOutputStream, use the following constructors: public FileOutputStream(String filename) public FileOutputStream(File file) public FileOutputStream(String filename, boolean append) public FileOutputStream(File file, boolean append) If the file does not exist, a new file would be created. If the file already exists, the first two constructors would delete the current contents in the file. To retain the current content and append new data into the file, use the last two constructors by passing true to the append parameter. TestFileStream Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 12
  • 13. FilterInputStream/FilterOutputStream FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object FileOutputStream BufferedOutputStream OutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream Filter streams are streams that filter bytes for some purpose. The basic byte input stream provides a read method that can only be used for reading bytes. If you want to read integers, doubles, or strings, you need a filter class to wrap the byte input stream. Using a filter class enables you to read integers, doubles, and strings instead of bytes and characters. FilterInputStream and FilterOutputStream are the base classes for filtering data. When you need to process primitive numeric types, use DatInputStream and DataOutputStream to filter bytes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 13
  • 14. DataInputStream/DataOutputStream DataInputStream reads bytes from the stream and converts them into appropriate primitive type values or strings. FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object FileOutputStream BufferedOutputStream OutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream DataOutputStream converts primitive type values or strings into bytes and output the bytes to the stream. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 14
  • 15. DataInputStream DataInputStream extends FilterInputStream and implements the DataInput interface. InputStream java.io.DataInput +readBoolean(): boolean Reads a Boolean from the input stream. +readByte(): byte Reads a byte from the input stream. FilterInputStream +readChar(): char Reads a character from the input stream. +readFloat(): float Reads a float from the input stream. +readDouble(): float Reads a double from the input stream. DataInputStream +readInt(): int Reads an int from the input stream. +DataInputStream( +readLong(): long Reads a long from the input stream. in: InputStream) +readShort(): short Reads a short from the input stream. +readLine(): String Reads a line of characters from input. +readUTF(): String Reads a string in UTF format. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 15
  • 16. DataOutputStream DataOutputStream extends FilterOutputStream and implements the DataOutput interface. OutputStream java.io.DataOutput +writeBoolean(b: Boolean): void Writes a Boolean to the output stream. +writeByte(v: int): void Writes to the output stream the eight low-order bits FilterOutputStream of the argument v. +writeBytes(s: String): void Writes the lower byte of the characters in a string to the output stream. +writeChar(c: char): void Writes a character (composed of two bytes) to the DataOutputStream output stream. +DataOutputStream( +writeChars(s: String): void Writes every character in the string s, to the output out: OutputStream) stream, in order, two bytes per character. +writeFloat(v: float): void Writes a float value to the output stream. +writeDouble(v: float): void Writes a double value to the output stream. +writeInt(v: int): void Writes an int value to the output stream. +writeLong(v: long): void Writes a long value to the output stream. +writeShort(v: short): void Writes a short value to the output stream. +writeUTF(s: String): void Writes two bytes of length information to the output stream, followed by the UTF representation of every character in the string s. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 16
  • 17. Characters and Strings in Binary I/O A Unicode consists of two bytes. The writeChar(char c) method writes the Unicode of character c to the output. The writeChars(String s) method writes the Unicode for each character in the string s to the output. Why UTF-8? What is UTF-8? UTF-8 is a coding scheme that allows systems to operate with both ASCII and Unicode efficiently. Most operating systems use ASCII. Java uses Unicode. The ASCII character set is a subset of the Unicode character set. Since most applications need only the ASCII character set, it is a waste to represent an 8-bit ASCII character as a 16-bit Unicode character. The UTF-8 is an alternative scheme that stores a character using 1, 2, or 3 bytes. ASCII values (less than 0x7F) are coded in one byte. Unicode values less than 0x7FF are coded in two bytes. Other Unicode values are coded in three bytes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 17
  • 18. Using DataInputStream/DataOutputStream Data streams are used as wrappers on existing input and output streams to filter data in the original stream. They are created using the following constructors: public DataInputStream(InputStream instream) public DataOutputStream(OutputStream outstream) The statements given below create data streams. The first statement creates an input stream for file in.dat; the second statement creates an output stream for file out.dat. DataInputStream infile = new DataInputStream(new FileInputStream("in.dat")); DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat")); TestDataStream Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 18
  • 19. Order and Format CAUTION: You have to read the data in the same order and same format in which they are stored. For example, since names are written in UTF-8 using writeUTF, you must read names using readUTF. Checking End of File TIP: If you keep reading data at the end of a stream, an EOFException would occur. So how do you check the end of a file? You can use input.available() to check it. input.available() == 0 indicates that it is the end of a file. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 19
  • 20. BufferedInputStream/ BufferedOutputStream Using buffers to speed up I/O FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object FileOutputStream BufferedOutputStream OutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream BufferedInputStream/BufferedOutputStream does not contain new methods. All the methods BufferedInputStream/BufferedOutputStream are inherited from the InputStream/OutputStream classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 20
  • 21. Constructing BufferedInputStream/BufferedOutputStream // Create a BufferedInputStream public BufferedInputStream(InputStream in) public BufferedInputStream(InputStream in, int bufferSize) // Create a BufferedOutputStream public BufferedOutputStream(OutputStream out) public BufferedOutputStream(OutputStreamr out, int bufferSize) Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 21
  • 22. Case Studies: Copy File This case study develops a program that copies files. The user needs to provide a source file and a target file as command-line arguments using the following command: java Copy source target The program copies a source file to a target file and displays the number of bytes in the file. If the source does not exist, tell the user the file is not found. If the target file already exists, tell the user the file already exists. Copy Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 22
  • 23. Optional Object I/O DataInputStream/DataOutputStream enables you to perform I/O for primitive type values and strings. ObjectInputStream/ObjectOutputStream enables you to perform I/O for objects in addition for primitive type values and strings. FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object FileOutputStream BufferedOutputStream OutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 23
  • 24. ObjectInputStream ObjectInputStream extends InputStream and implements ObjectInput and ObjectStreamConstants. java.io.InputStream ObjectStreamConstants java.io.DataInput java.io.ObjectInputStream java.io.ObjectInput +ObjectInputStream(in: InputStream) +readObject(): Object Reads an object. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 24
  • 25. ObjectOutputStream ObjectOutputStream extends OutputStream and implements ObjectOutput and ObjectStreamConstants. java.io.OutputStream ObjectStreamConstants java.io.DataOutput java.io.ObjectOutputStream java.io.ObjectOutput +ObjectOutputStream(out: OutputStream) +writeObject(o: Object): void Writes an object. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 25
  • 26. Using Object Streams You may wrap an ObjectInputStream/ObjectOutputStream on any InputStream/OutputStream using the following constructors: // Create an ObjectInputStream public ObjectInputStream(InputStream in) // Create an ObjectOutputStream public ObjectOutputStream(OutputStream out) TestObjectOutputStream Run TestObjectInputStream Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 26
  • 27. The Serializable Interface Not all objects can be written to an output stream. Objects that can be written to an object stream is said to be serializable. A serializable object is an instance of the java.io.Serializable interface. So the class of a serializable object must implement Serializable. The Serializable interface is a marker interface. It has no methods, so you don't need to add additional code in your class that implements Serializable. Implementing this interface enables the Java serialization mechanism to automate the process of storing the objects and arrays. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 27
  • 28. The transient Keyword If an object is an instance of Serializable, but it contains non-serializable instance data fields, can the object be serialized? The answer is no. To enable the object to be serialized, you can use the transient keyword to mark these data fields to tell the JVM to ignore these fields when writing the object to an object stream. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 28
  • 29. The transient Keyword, cont. Consider the following class: public class Foo implements java.io.Serializable { private int v1; private static double v2; private transient A v3 = new A(); } class A { } // A is not serializable When an object of the Foo class is serialized, only variable v1 is serialized. Variable v2 is not serialized because it is a static variable, and variable v3 is not serialized because it is marked transient. If v3 were not marked transient, a java.io.NotSerializableException would occur. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 29
  • 30. Serializing Arrays An array is serializable if all its elements are serializable. So an entire array can be saved using writeObject into a file and later restored using readObject. Listing 16.12 stores an array of five int values an array of three strings, and an array of two JButton objects, and reads them back to display on the console. TestObjectStreamForArray Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 30
  • 31. Random Access Files All of the streams you have used so far are known as read-only or write-only streams. The external files of these streams are sequential files that cannot be updated without creating a new file. It is often necessary to modify files or to insert new records into files. Java provides the RandomAccessFile class to allow a file to be read from and write to at random locations. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 31
  • 32. RandomAccessFile DataInput DataInput java.io.RandomAccessFile +RandomAccessFile(file: File, mode: Creates a RandomAccessFile stream with the specified File object and String) mode. +RandomAccessFile(name: String, Creates a RandomAccessFile stream with the specified file name mode: String) string and mode. +close(): void Closes the stream and releases the resource associated with the stream. +getFilePointer(): long Returns the offset, in bytes, from the beginning of the file to where the next read or write occurs. +length(): long Returns the length of this file. +read(): int Reads a byte of data from this file and returns –1 an the end of stream. +read(b: byte[]): int Reads up to b.length bytes of data from this file into an array of bytes. +read(b: byte[], off: int, len: int) : int Reads up to len bytes of data from this file into an array of bytes. +seek(long pos): void Sets the offset (in bytes specified in pos) from the beginning of the stream to where the next read or write occurs. +setLength(newLength: long): void Sets a new length of this file. +skipBytes(int n): int Skips over n bytes of input discarding the skipped bytes. +write(b: byte[]): void Writes b.length bytes from the specified byte array to this file, starting +write(byte b[], int off, int len) at the current file pointer. +write(b: byte[], off: int, len: int): Writes len bytes from the specified byte array starting at offset off to void this file. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 32
  • 33. File Pointer A random access file consists of a sequence of bytes. There is a special marker called file pointer that is positioned at one of these bytes. A read or write operation takes place at the location of the file pointer. When a file is opened, the file pointer sets at the beginning of the file. When you read or write data to the file, the file pointer moves forward to the next data. For example, if you read an int value using readInt(), the JVM reads four bytes from the file pointer and now the file pointer is four bytes ahead of the previous location. file pointer file byte byte … byte byte byte byte byte … byte byte byte byte byte (A) Before readInt() file pointer file byte byte … byte byte byte byte byte … byte byte byte byte byte (B) Before readInt() Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 33
  • 34. RandomAccessFile Methods Many methods in RandomAccessFile are the same as those in DataInputStream and DataOutputStream. For example, readInt(), readLong(), writeDouble(), readLine(), writeInt(), and writeLong() can be used in data input stream or data output stream as well as in RandomAccessFile streams. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 34
  • 35. RandomAccessFile Methods, cont.  void seek(long pos) throws IOException; Sets the offset from the beginning of the RandomAccessFile stream to where the next read or write occurs.  long getFilePointer() IOException; Returns the current offset, in bytes, from the beginning of the file to where the next read or write occurs. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 35
  • 36. RandomAccessFile Methods, cont.  long length()IOException Returns the length of the file.  final void writeChar(int v) throws IOException Writes a character to the file as a two-byte Unicode, with the high byte written first.  final void writeChars(String s) throws IOException Writes a string to the file as a sequence of characters. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 36
  • 37. RandomAccessFile Constructor RandomAccessFile raf = new RandomAccessFile("test.dat", "rw"); //allows read and write RandomAccessFile raf = new RandomAccessFile("test.dat", "r"); //read only Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 37
  • 38. A Short Example on RandomAccessFile TestRandomAccessFile Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 38
  • 39. Companion Website Case Studies: Address Book Now let us use RandomAccessFile to create a useful project for storing and viewing and address book. The user interface of the program is shown in Figure 16.24. The Add button stores a new address to the end of the file. The First, Next, Previous, and Last buttons retrieve the first, next, previous, and last addresses from the file, respectively. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 39
  • 40. Companion Website Fixed Length String I/O Random access files are often used to process files of records. For convenience, fixed-length records are used in random access files so that a record can be located easily. A record consists of a fixed number of fields. A field can be a string or a primitive data type. A string in a fixed-length record has a maximum size. If a string is smaller than the maximum size, the rest of the string is padded with blanks. file Record 1 Record 2 Record n Student 1 Student 2 Student n e.g., Field1 Field 2 … Field k name street city state zip FixedLengthStringIO Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 40
  • 41. Companion Website Address Implementation The rest of the work can be summarized in the following steps: Create the user interface. Add a record to the file. Read a record from the file. Write the code to implement the button actions. AddressBook Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 41