序列化
ObjectInputSteam
ObjectOutputSteam
package IO;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import org.junit.Test;
public class testObjectInputStream {
@Test
public void testObjectOutputSteam(){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("object.mat"));
oos.writeObject(new String("我爱中国"));
oos.flush();
oos.writeObject(new person("zo", 18));
oos.flush();
} catch (Exception e) {
}finally {
if (oos != null){
try {
oos.close();
} catch (Exception e2) {
}
}
}
}
@Test
public void ObjectInputStream1(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("object.mat"));
Object object = ois.readObject();
String string = (String) object;
System.out.println(string);
person person = (person)ois.readObject();
System.out.println(person.toString());
} catch (Exception e) {
}finally {
if(ois != null){
try {
ois.close();
} catch (Exception e2) {
}
}
}
}
}
package IO;
import java.io.Serializable;
public class person implements Serializable{
public static final long serialVersionUID = 475463534532L;
private String name;
private int age;
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() {
return "person [name=" + name + ", age=" + age + "]";
}
}