看了毕老师的视频还是没有弄清为什么person对象要实现序列化?
附代码:
package com.hubj.objectDemo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Objext {
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
write();
read();
}
private static void read() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"g:\\aa..txt"));
Person p = (Person) ois.readObject();
System.out.println(p);
}
private static void write() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"g:\\aa..txt"));
oos.writeObject(new Person("zhang", 56));
oos.close();
}
}
------------------------------------------------------------------------------------
package com.hubj.objectDemo;
import java.io.Serializable;
public class Person implements Serializable{
public static final long serialVersionUID = 42L;
private String name;
transient int age;
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return name+":"+age;
}
}
|
|