testserialization.java
来自「java课件及例程有一些PPT及一些例程」· Java 代码 · 共 40 行
JAVA
40 行
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 + =
减小字号Ctrl + -
显示快捷键?