Академический Документы
Профессиональный Документы
Культура Документы
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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 (a) The Unicode of the character e.g. "199" , Encoding/ Decoding
The encoding of the character is stored in the file 00110001 00111001 00111001 0x31 0x39 0x39
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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. 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. Reads bytes from the input stream and stores into b[off], b[off+1], , b[off+len-1]. The actual number of bytes read is returned. Returns -1 at the end of the stream. Returns the number of bytes that can be read from the input stream. Closes this input stream and releases any system resources associated with the stream. Skips over and discards n bytes of data from this input stream. The actual number of bytes skipped is returned.
+read(b: byte[]): int +read(b: byte[], off: int, len: int): int +available(): int +close(): void +skip(n: long): long
+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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
OutputStream
The value is a byte as an int type.
java.io.OutputStream +write(int b): void +write(b: byte[]): void Writes the specified byte to this output stream. The parameter b is an int value. (byte)b is written to the output stream. 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 +flush(): void Closes this input stream and releases any system resources associated with the stream. Flushes this output stream and forces any buffered output bytes to be written out.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
FileInputStream/FileOutputStream
FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object FileOutputStream OutputStream FilterOutputStream ObjectOutputStream BufferedOutputStream DataOutputStream 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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Run
12
FilterInputStream/FilterOutputStream
FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object FileOutputStream OutputStream FilterOutputStream ObjectOutputStream BufferedOutputStream DataOutputStream 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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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 OutputStream FilterOutputStream ObjectOutputStream BufferedOutputStream DataOutputStream PrintStream
DataOutputStream converts primitive type values or strings into bytes and output the bytes to the stream.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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. +readChar(): char +readFloat(): float +readDouble(): float DataInputStream +DataInputStream( in: InputStream) +readInt(): int +readLong(): long +readShort(): short +readLine(): String +readUTF(): String Reads a character from the input stream. Reads a float from the input stream. Reads a double from the input stream. Reads an int from the input stream. Reads a long from the input stream. Reads a short from the input stream. Reads a line of characters from input. Reads a string in UTF format.
FilterInputStream
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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 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 output stream. +writeChars(s: String): void Writes every character in the string s, to the output stream, in order, two bytes per character. +writeFloat(v: float): void Writes a float value to the output stream. +writeDouble(v: float): void +writeInt(v: int): void +writeLong(v: long): void +writeShort(v: short): void +writeUTF(s: String): void Writes a double value to the output stream. Writes an int value to the output stream. Writes a long value to the output stream. Writes a short value to the output stream. Writes two bytes of length information to the output stream, followed by the UTF representation of every character in the string s.
FilterOutputStream
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
16
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Run
18
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
19
BufferedInputStream/ BufferedOutputStream
FileInputStream InputStream FilterInputStream
DataInputStream BufferedInputStream ObjectInputStream Object FileOutputStream OutputStream FilterOutputStream ObjectOutputStream BufferedOutputStream DataOutputStream 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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
21
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
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Run
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
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
23
ObjectInputStream
ObjectInputStream extends InputStream and implements ObjectInput and ObjectStreamConstants.
java.io.InputStream
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
24
ObjectOutputStream
ObjectOutputStream extends OutputStream and implements ObjectOutput and ObjectStreamConstants.
java.io.OutputStream
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
25
TestObjectOutputStream TestObjectInputStream
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Run
Run
26
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
27
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
28
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
30
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
31
RandomAccessFile
DataInput java.io.RandomAccessFile +RandomAccessFile(file: File, mode: String) +RandomAccessFile(name: String, mode: String) +close(): void +getFilePointer(): long +length(): long +read(): int +read(b: byte[]): int +read(b: byte[], off: int, len: int) : int +seek(long pos): void +setLength(newLength: long): void +skipBytes(int n): int +write(b: byte[]): void +write(byte b[], int off, int len) +write(b: byte[], off: int, len: int): void Creates a RandomAccessFile stream with the specified File object and mode. Creates a RandomAccessFile stream with the specified file name string and mode. Closes the stream and releases the resource associated with the stream. Returns the offset, in bytes, from the beginning of the file to where the next read or write occurs. Returns the length of this file. Reads a byte of data from this file and returns 1 an the end of stream. Reads up to b.length bytes of data from this file into an array of bytes. Reads up to len bytes of data from this file into an array of bytes. Sets the offset (in bytes specified in pos) from the beginning of the stream to where the next read or write occurs. Sets a new length of this file. Skips over n bytes of input discarding the skipped bytes. Writes b.length bytes from the specified byte array to this file, starting at the current file pointer. Writes len bytes from the specified byte array starting at offset off to this file. DataInput
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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
file
byte byte
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
34
Sets the offset from the beginning of the RandomAccessFile stream to where the next read or write occurs.
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
35
long length()IOException
Writes a character to the file as a two-byte Unicode, with the high byte written first.
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
37
TestRandomAccessFile Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
38
Companion Website
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
39
Companion Website
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 e.g., Field1 Field 2 Field k name street city state zip Student 1 Student 2 Student n
FixedLengthStringIO
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
41