Java文件处理、对象序列化与泛型集合全解析
1. 对象序列化与顺序访问文件数据读取
在对象序列化的应用中,我们既需要掌握如何创建用于顺序访问的文件,也需要了解如何从文件中顺序读取序列化的数据。下面是一个读取顺序访问文件中对象的示例程序。
// Fig. 17.18: ReadSequentialFile.java
// Reading a file of objects sequentially with ObjectInputStream
// and displaying each record.
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import com.deitel.ch17.AccountRecordSerializable;
public class ReadSequentialFile {
private ObjectInputStream input;
// enable user to select file to open
public void openFile() {
try { // open file
input = new ObjectInputStream(
new FileInputStream("clients.ser"));
} catch (IOException ioExc