package cn.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* 序列化:对象流 写到字节数组
* 写出后读取,读取与写出保持顺序一致,不是所有对象都可以序列化,需实现Serializable接口
* @author Chill Lyn
*
*/
public class TestObjectStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 序列化:写出
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(baos));
// 操作数据类型+数据
oos.writeBoolean(false);
oos.writeChar('a');
oos.writeInt(0);
oos.writeUTF("我爱你");
oos.writeObject("我还爱你");
oos.writeObject(new Drink("Cola", 3, 2));
oos.writeObject(new Date());
oos.flush();
byte[] datas = baos.toByteArray();
// 反序列化:读取
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
// 顺序与写出顺序一致
boolean flag = ois.readBoolean();
char ch = ois.readChar();
int i = ois.readInt();
String str = ois.readUTF();
Object str2 = ois.readObject();
Object drink = ois.readObject();
Object date = ois.readObject();
if (str2 instanceof String) {
String strObj = (String) str2;
System.out.println(strObj);
}
if (drink instanceof Drink) {
Drink drinkObj = (Drink) drink;
System.out.println(drinkObj.getName() + "-->" + drinkObj.getPrice() + "-->" + drinkObj.getCost());
}
if (date instanceof Date) {
Date dateObj = (Date) date;
System.out.println(dateObj);
}
System.out.println(ch);
}
}
class Drink implements java.io.Serializable {
private String name;
private double price;
private transient double cost; // 该数据不需要序列化
public Drink() {
}
public Drink(String name, double price, double cost) {
super();
this.name = name;
this.price = price;
this.cost = cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
}
package cn.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* 对象流 写到文件
* 写出后读取,读取与写出保持顺序一致,不是所有对象都可以序列化,需实现Serializable接口
* @author Chill Lyn
*
*/
public class TestObjectStream2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 序列化:写出
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("obj.txt")));
// 操作数据类型+数据
oos.writeBoolean(false);
oos.writeChar('a');
oos.writeInt(0);
oos.writeUTF("我爱你");
oos.writeObject("我还爱你");
oos.writeObject(new Drink("Cola", 3, 2));
oos.writeObject(new Date());
oos.flush();
oos.close();
// 反序列化:读取
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("obj.txt")));
// 顺序与写出顺序一致
boolean flag = ois.readBoolean();
char ch = ois.readChar();
int i = ois.readInt();
String str = ois.readUTF();
Object str2 = ois.readObject();
Object drink = ois.readObject();
Object date = ois.readObject();
if (str2 instanceof String) {
String strObj = (String) str2;
System.out.println(strObj);
}
if (drink instanceof Drink) {
Drink drinkObj = (Drink) drink;
System.out.println(drinkObj.getName() + "-->" + drinkObj.getPrice() + "-->" + drinkObj.getCost());
}
if (date instanceof Date) {
Date dateObj = (Date) date;
System.out.println(dateObj);
}
System.out.println(ch);
ois.close();
}
}
备注
1.可以看作数据流的升级版,可以操作引用类型数据。