📄 testserialization.java
字号:
import java.io.*;
public class TestSerialization{
public static void main(String args[]){
try{
Human h1 = new Human("Henry",24,6500.00);
System.out.println("h1的信息为:"+h1);
FileOutputStream s1 = new FileOutputStream("2.txt");
ObjectOutputStream os = new ObjectOutputStream(s1);
os.writeObject(h1);
os.flush();
os.close();
}catch(Exception e){}
try{
Human h2;
FileInputStream s2 = new FileInputStream("2.txt");
ObjectInputStream is = new ObjectInputStream(s2);
h2 = (Human)is.readObject();
is.close();
System.out.println("h2的信息为:"+h2);
System.out.println("h2的name为:"+h2.name);
System.out.println("h2的age为:"+h2.age);
System.out.println("h2的wage为:"+h2.wage);
}catch(Exception e){}
}
}
class Human implements Serializable{
String name;
int age;
double wage;
Human(String name, int age, double wage){
this.name = name;
this.age = age;
this.wage = wage;
}
public String toString(){
return("name = "+name+",age = "+age+",wage = "+wage);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -