📄 serialdemo.java
字号:
import java.io.*;
class Day implements Serializable
{
private int year,month,day;
public Day()
{
year=2000;
month=9;
day=21;
}
public Day(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
public int getYear() { return year;}
public int getMonth(){ return month;}
public int getDay() { return day;}
public void outPut() { System.out.println("The date is:"+year+"年"+month+"月"+day+"日");}
}
class Employee implements Serializable
{
public static final int NAMESIZE=40;
public static final int RECORDSIZE=2*NAMESIZE+8+4*3;
private String name;
private double salary;
private Day hireDay;
public Employee(){}
public Employee(String n,double s,Day d)
{
name=n;
salary=s;
hireDay=d;
}
public void print()
{
System.out.println(name+" "+salary+" "+hireYear());}
public void raiseSalary(double byPercent)
{
salary*=1+byPercent/100; }
public int hireYear()
{
return hireDay.getYear();
}
}
class ObjectSer
{
public static void main(String args[])
throws IOException,ClassNotFoundException
{ int i;
Employee[] staff = new Employee[3];
staff[0] = new Employee("Dong",600,new Day(1998,9,1));
staff[1] = new Employee("Wang",1000,new Day(1958,2,15));
staff[2] = new Employee("Meng",750,new Day(1995,3,15));
FileOutputStream fo=new FileOutputStream("data.txt");
ObjectOutputStream so=new ObjectOutputStream(fo);
try
{ for(i=0;i<3;i++) so.writeObject(staff[i]);
so.close();
}
catch(IOException e)
{ System.out.println("Saving Error:"+e);
System.exit(1);
}
for(i=0;i<3;i++) staff[i]=null;
FileInputStream fi=new FileInputStream("data.txt");
ObjectInputStream si= new ObjectInputStream(fi);
try
{
for(i=0;i<3;i++)
staff[i]=(Employee) si.readObject();
si.close();
}
catch(IOException e)
{ System.out.print("Reading Error:"+e);
System.exit(1);
}
for(i=0;i<3;i++)
{
staff[i].print();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -