首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请完成以下程序,首先由一个类Example2_3实现Serializable接口,并有三个成员变量,分别为int型、double型和String型,可以用toString的方法显示这三个成员变量。在main方法中创建这个Example2_3的持久对象,根据
请完成以下程序,首先由一个类Example2_3实现Serializable接口,并有三个成员变量,分别为int型、double型和String型,可以用toString的方法显示这三个成员变量。在main方法中创建这个Example2_3的持久对象,根据
admin
2009-01-15
22
问题
请完成以下程序,首先由一个类Example2_3实现Serializable接口,并有三个成员变量,分别为int型、double型和String型,可以用toString的方法显示这三个成员变量。在main方法中创建这个Example2_3的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为TheSerial.data的文件,并显示成员变量。最后从文件TheSerial.data中读出三个成员变量并显示出来。
注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。
import java.io.*;
class TheSerial implements Serializable
{
private int intValue;
private double doubleValue;
private String string;
TheSerial()
{
intValue = 123;
doubleValue = 12.34;
string = "Serialize Test";
}
public void setDouble(double d)
{
doubleValue = d;
}
public void setInt(int i)
{
intValue = i;
}
public void setString(String s)
{
string = s;
}
public String toString()
{
return("int="+intValue+"double="+doubleValue+" string="+string);
}
}
public class Example2_3
{
public static void main(String argv[])
{
TheSerial e1 = new TheSerial();
TheSerial e2;
try
{
e1.setInt(Integer.parseInt(argv[0]));
e1.setDouble(Double.parseDouble(argv[1]));
e1.setString[argv[2]);
}
catch(Exception e)
{
e1.setString(e.getMessage{));
}
System.out.println(e1);
try
{
FileOutputStream oS = new FileOutputStream("TheSerial.data");
ObjectOutputStream oIS = new ObjectOutputStream(oS);
______;
}
catch(IOException ioException)
{
System.out.println(ioException.getMessage());
}
try
{
FileInputStream iS = new FileInputStream("TheSerial. data");
ObjectInputStream oIS = new ObjectInputStream(iS);
______
System.out.println(e2);
}
catch(IOException ioException)
{
System.out.println(ioException.getMessage());
}
catch(ClassNotFoundException cnfException)
{
System.out.println(cnfException.getMessage());
}
}
}
选项
答案
①oOS.writeObject(e1) ②e2=(TheSerial)oIS.readObject()
解析
本题主要考查串行化相关的方法和实现。解题中首先要掌握串行化的基本过程和反串行化的过程。串行化过程首先要创建一个输出流FileOutputStream,通过该类的实例对文件进行访问,然后创建一个ObJectOutput Stream对象,通过writeObject()方法来实现对象的序列化。第一个空就是使用writeObject()实现序列化。
反序列化过程中用FileInputStream对象建立读取文件的连接,并使用该对象创建一个 ObiectInputStream的实例,这个ObjectInput Stream实例的readObject()方法就可以实现对象的反序列化。第二个空就是使用readObject()实现反序列化。
转载请注明原文地址:https://kaotiyun.com/show/nmnp777K
本试题收录于:
二级Java题库NCRE全国计算机二级分类
0
二级Java
NCRE全国计算机二级
相关试题推荐
为“歌手”表增加一个字段“最后得分”的SQL语句是
插入一条记录到表结构为(职工号,姓名,工资)的表Employee中,正确的SQL命令是
查询尚未归还的图书编号和借书日期,正确的SQL语句是
数据库(DB)、数据库系统(DBS)和数据库管理系统(DBMS)三者之间的关系是
在SQL的SELECT查询的结果中,消除重复记录的方法是
下列程序段执行后,内存变量s1的值是s1="奥运会游泳比赛"s1=stuff(s1,7,4,"田径")?s1
耦合性和内聚性是对模块独立性度量的两个标准。下列叙述中正确的是
在表单(Forml)的INIT事件中编写程序代码,下列语句中不能将表单的背景颜色设置为绿色的是()。
使用如下3个数据库表:职员.dbf:职员号C(3),姓名C(6),性别C(2),组号N(1),职务C(10)客户.dbf:客户号C(4),客户名C(36),地址C(36),所在城市C(36)订单.dbf:订单号C(4),客户名C(4),职员号C(
“图书”表中有字符型字段“图书号”。要求用SQLDELETE命令将图书号以字母“A”开头的图书记录全部删除,正确的命令是()。
随机试题
更年期综合征的主要症状不包括
一严重脑出血病人,急性期间出现呕吐咖啡色胃内容物,应考虑
下列哪些选项中的甲属于犯罪未遂?(2014年试卷二第54题)
投标保证金有效期应当超出投标有效期( )天。
企业对某项固定资产计提减值准备,体现了()信息质量要求。
我国古典文学中一部最杰出的讽刺小说是()。
材料:下面是某教师关于“中国的疆域”一课的教学片段。教师出示谜语:“蓝色星球浮太空,一只雄鸡在其中,南北两分它在北,东西二分它居东,头指世界最大洋,尾靠全球最大陆。”谜底是一地理现象。学生猜测:中国的地理位置。教师:从谜语中你能得到关于中国地理位置
学术诚信
Thewriter’sgeneralattitudetowardstheworldisthat______.Whydowebuildcommonmarketsandjointinstitutions?
Thebigidentity-theftbustlastweekwasjustatasteofwhat’stocome.Here’showtoprotectyourgoodname.HERE’STHES
最新回复
(
0
)