DataOutputStream 和 ObjectOutputStream的共同点是:
1、写出后读取
2、读取顺序和写出一致
数据流操作:
// 写入
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(baos));
dos.writeUTF("编码辛酸泪");
dos.writeInt(18);
dos.writeBoolean(false);
dos.writeChar('a');
dos.flush();
byte[] datas = baos.toByteArray();
System.out.println(datas.length);
// 写出
ByteArrayInputStream bais = new ByteArrayInputStream(datas);
DataInputStream dis = new DataInputStream(new BufferedInputStream(bais));
String msg = dis.readUTF();
int age = dis.readInt();
boolean flag = dis.readBoolean();
char ch = dis.readChar();
System.out.println(ch);
对象流操作:
对象的写入写入又叫序列化和方序列化,需要注意的是,并不是所有的对象都可以序列化,如果要对对象进行序列化,需要实现 Serializable 接口,如果不想让对象中某个属性序列化,可以使用 transient 关键字标识
比如我们自己定义一个类:
class Employee implements Serializable{
private transient String name;
private double salary;
public Employee() {
}
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
序列化到字节数组:
// 写出--> 序列化
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(baos));
// 操作数据类型
oos.writeUTF("编码辛酸泪");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('a');
// 操作对象
oos.writeObject("谁解其中味");
oos.writeObject(new Date());
oos.writeObject(new Employee("马云", 400));
oos.flush();
byte[] datas = baos.toByteArray();
oos.close();
// 读取--> 反序列化
ByteArrayInputStream bais = new ByteArrayInputStream(datas);
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(bais));
String msg = ois.readUTF();
int age = ois.readInt();
boolean flag = ois.readBoolean();
char ch = ois.readChar();
System.out.println(age);
// 对象还原
Object str = ois.readObject();
Object date = ois.readObject();
Object emp = ois.readObject();
if(str instanceof String) {
String strObj = (String) str;
System.out.println(strObj);
}
if(date instanceof Date) {
Date dateObj = (Date) date;
System.out.println(dateObj);
}
if(emp instanceof Employee) {
Employee empObj = (Employee) emp;
System.out.println(empObj.getName() + "-->" + empObj.getSalary());
}
ois.close();
}
序列化到文件:
// 写出--> 序列化
ObjectOutputStream oos = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream("obj.ser")));
// 操作数据类型
oos.writeUTF("编码辛酸泪");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('a');
// 操作对象
oos.writeObject("谁解其中味");
oos.writeObject(new Date());
oos.writeObject(new Employee("马云", 400));
oos.flush();
oos.close();
// 读取--> 反序列化
ObjectInputStream ois = new ObjectInputStream(
new BufferedInputStream(new FileInputStream("obj.ser")));
String msg = ois.readUTF();
int age = ois.readInt();
boolean flag = ois.readBoolean();
char ch = ois.readChar();
System.out.println(age);
// 对象还原
Object str = ois.readObject();
Object date = ois.readObject();
Object emp = ois.readObject();
if(str instanceof String) {
String strObj = (String) str;
System.out.println(strObj);
}
if(date instanceof Date) {
Date dateObj = (Date) date;
System.out.println(dateObj);
}
if(emp instanceof Employee) {
Employee empObj = (Employee) emp;
System.out.println(empObj.getName() + "-->" + empObj.getSalary());
}
ois.close();