9.ObjectOutputStream:什么是对象输入输出流???包装流有什么特点???对象输入输出流常用方法有哪些???
学习:第7遍
1.什么是对象输入输出流???
如果希望将Java对象写入到IO流中,或从IO流中读取Java对象,则要使用对象输入输出流,称为对象的序列化和反序列化
ObjectInputStream 对象输入流:用来读取对象,即反序列化
ObjectOutputStream 对象输出流:用来写入对象,即序列化
2.包装流有什么特点???
ObjectInputStream和ObjectOutputStream属于包装流 用于对节点流进行功能扩展 / 包装 在创建包装流,需要传入要操作的节点流对象 当关闭流时,只需要关闭包装流,节点流也会被关闭
3.对象输入输出流常用方法有哪些???
创建:必须基于某个节点流,进行包装 oos = new ObjectOutputStream(new FileOutputStream(“c:/aaa/user.data”))
方法:oos.writeObject(u1) oos.writeObject(users) users是list集合 作用:写入对象
方法:User u1 = (User) ois.readObject() List<User> list = (List<User>) ois.readObject() 作用:读取顺序和写入顺序一致
public class Test{
public static void main(String
[] args
) {
test01();
test02();
}
public static void test01() {
User u1
= new User(1001, "tom", 18);
u1
.setAddress(new Address("江苏", "南京"));
User u2
= new User(1002, "jack", 21);
u2
.setAddress(new Address("江苏", "扬州"));
List
<User> users
= Arrays
.asList(u1
, u2
);
ObjectOutputStream oos
= null
;
try {
oos
= new ObjectOutputStream(new FileOutputStream("c:/aaa/user.data"));
oos
.writeObject(users
);
oos
.flush();
System
.out
.println("写入对象成功");
} catch (FileNotFoundException e
) {
e
.printStackTrace();
} catch (IOException e
) {
e
.printStackTrace();
} finally {
if (oos
!= null
) {
try {
oos
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
public static void test02() {
ObjectInputStream ois
= null
;
try {
ois
= new ObjectInputStream(new FileInputStream("user.data"));
List
<User> list
= (List
<User>) ois
.readObject();
System
.out
.println(list
);
} catch (FileNotFoundException e
) {
e
.printStackTrace();
} catch (IOException e
) {
e
.printStackTrace();
} catch (ClassNotFoundException e
) {
e
.printStackTrace();
} finally {
if (ois
!= null
) {
try {
ois
.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
}
}
class User implements Serializable {
private static final long serialVersionUID
= 9112680227154158529L
;
private Integer id
;
private String name
;
private transient Integer age
;
private Address address
;
public User() {
super();
}
public User(Integer id
, String name
, Integer age
) {
super();
this.id
= id
;
this.name
= name
;
this.age
= age
;
}
public Integer
getId() {
return id
;
}
public void setId(Integer id
) {
this.id
= id
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public Integer
getAge() {
return age
;
}
public void setAge(Integer age
) {
this.age
= age
;
}
public Address
getAddress() {
return address
;
}
public void setAddress(Address address
) {
this.address
= address
;
}
@Override
public String
toString() {
return "User [id=" + id
+ ", name=" + name
+ ", address=" + address
+ "]";
}
}
class Address implements Serializable{
private static final long serialVersionUID
= -501257177877596419L
;
private String province
;
private String city
;
public String
getProvince() {
return province
;
}
public void setProvince(String province
) {
this.province
= province
;
}
public String
getCity() {
return city
;
}
public void setCity(String city
) {
this.city
= city
;
}
public Address(String province
, String city
) {
super();
this.province
= province
;
this.city
= city
;
}
public Address() {
super();
}
@Override
public String
toString() {
return "Address [province=" + province
+ ", city=" + city
+ "]";
}
}