标签:举例 data nts style 字段 put team inpu 通过
@Test
public void testOut() {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("Person.txt"));
Person p = new Person(23, "chen", new Pet("2", "dog"));
oos.writeObject(p);
} catch (SecurityException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null) {
oos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void testIn() {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("Person.txt"));
Person p = (Person) ois.readObject();
System.out.println(p);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class Person implements Serializable {
public static final long serialVersionUID = 1L;
Integer age;
String name;
Pet pet;
public Person(Integer age, String name, Pet pet) {
super();
this.age = age;
this.name = name;
this.pet = pet;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + ", pet=" + pet + "]";
}
}
class Pet implements Serializable {
public static final long serialVersionUID = 2L;
String name;
String kind;
public Pet(String name, String kind) {
super();
this.name = name;
this.kind = kind;
}
@Override
public String toString() {
return "Pet [name=" + name + ", kind=" + kind + "]";
}
标签:举例 data nts style 字段 put team inpu 通过
原文地址:http://www.cnblogs.com/chendifan/p/6537309.html