[填空题] 从对象流中读取对象,请在画线处加入代码完成此程序 【10】 。 import java.util.*; import java.io.*; public class UnSeria

9 查阅

[填空题] 从对象流中读取对象,请在画线处加入代码完成此程序 【10】

import java.util.*;

import java.io.*;

public class UnSerializeDate

Date d = null;

UnSerializeDate()

try

FileInputStream f = new FileInputStream("data.ser");

______;

d = (Date) s.readObject();

f.close();

catch(Exception e)

e.printStackTrace();

public static void main(String args[ ])

UnSerializeDate a = new UnSerializeDate();

System.out.println("The date read is :"+a.d.toString());

参考答案:

ObiectInputSlieam s=new ObjectInputStream(f);

题中对象输入流ObectInputStream的对象s是以一个文件输入流为基础构造的。程序中使用readObject()方法从对象流s中读取一个Date类型的对象。读对象时要按照它们写入的顺序读取,因为readObject()返回的是Object类型的对象,所以程序中使用了强制类型转换,将所读取对象的类型转换为Date类型。

计算机考试