问答题

请完成程序,首先由一个类simple实现Serializable接口,并有三个成员变量,分别为int型、double型和String型,可以用toString的方法显示这三个成员变量。在main方法中创建这个simple的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为TheSerial.data的文件中,并显示成员变量。最后从文件TheSerial.data中读出三个成员变量并显示出来。
注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。
import java.io.*;
class TheSerial implements Serializable

private int intvalue;
private double doublevalue;
private String string;
The Serial ()

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 to String()

return("int="+intvalue+" double="+doublevalue+" string="+string);


public class simple

public static void main(String[] args)

The Serial e1=new TheSerial();
TheSerial e2;
try

e1.setInt(Integer.parseInt(args[0]));
e1.setDouble(Double.parseDouble(args[1]));
e1.setString(args[2]);

catch(Exception e)

e1.setString(e.getMessage());

System.out.println(e1);
try

FileOutputStream oS=new FileOutputStream("TheSerial.data");
ObjectOutputStream oOS=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()
热门 试题

问答题
本程序的目的是在屏幕上显示当前目录下的文件信息。文件信息通过表格JTable的实例显示。请更正题中带下划线的部分,使程序能输出正确的结果。 注意:不改变程序的结构,不得增行或删行。 import java.awt.*; import javax.swing.*; import java.util.Date; import javax.swing.table.*; import java.applet.*; import java.io.*; public class advance extends JApplet, JFrame public void init() FileModel fm=new FileModel(); JTable jt=new JTable(); jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); jt.setColumnSelectionAllowed(true); JScrollPane jsp=new JScrollPane(jt); getContentPane().add(jsp, BorderLayout.CENTER); public static void main(String args[]) advance ft=new advance(); ft.init(); JFrame f=new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(ft.getContentPane()); f.setSize(300,400); f.show(); class FileModel extends AbstractTableModel String[] columnName=new String[] 文件名 , 大小 , 最后修改时间 ; Object[][] data; public FileModel() this( . ); public FileModel(String dir) File file=new File(dir); String files[]=file.list(); data=new Object[files.length] [columnName.length]; for(int i=0; i<files.length; i++) File tmp=new File(files[i]); data[i] [0]=tmp.getName(); data[i] [1]=new Long(tmp.length()); data[i] [2]=new Date(tmp.lastModified()); public int getColumnNumber() return columnName.length; public int getRowCount() return data.length; public String getColumnName(int col) return columnName[col]; public Object getValueAt(int row, int col) return data[row] [col]; public Class getColumnClass(int c) return getValueAt(0,c).getClass();