Java-裝飾流-物件流 - 序列化和反序列化
阿新 • • 發佈:2018-11-09
ObjectInputStream(反序列化) & ObjectOutputStream(序列化)
1.先寫出後讀取
2.讀取的順序必須保持一致
3.不是所有的物件都能序列化,要加上serializable接口才行
當不想對物件中的某個屬性序列化時,在屬性中新增transient就行啦~
[eg]:private transient String name;
4.序列化有另外一個名稱叫做持久化
(1)序列化到位元組資料(不需要關閉流)
package cn.lesson.Burrfed;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* 資料流
* 1.先寫出後讀取
* 2.讀取和寫出的順序一致
* 3.不是所有的物件都可以序列化,必須要使用serializable介面
* @author MacBook
*
*/
public class DataTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//選擇流
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
ObjectOutputStream dos = new ObjectOutputStream(new BufferedOutputStream(baos));
//寫出
dos.writeUTF("啦啦啦啦");
dos.writeInt(12);
dos.writeBoolean(false);
dos.writeChar('a');
//物件
dos.writeObject("誰解其中味");
dos.writeObject(new Date());
Employee emp = new Employee("馬雲", 400);
dos.writeObject(emp);
dos.flush();
byte[] datas = baos.toByteArray();
System.out.println(datas.length);
//讀取
//選擇流
ObjectInputStream dis =
new ObjectInputStream(
new BufferedInputStream(
new ByteArrayInputStream(datas)));
String msg = dis.readUTF();
int age = dis.readInt();
boolean i = dis.readBoolean();
char ch = dis.readChar();
Object str = dis.readObject();
Object date = dis.readObject();
Object employee = dis.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(employee instanceof Employee) {
Employee empObj = (Employee)employee;
System.out.println(empObj.getName()+"-->"+empObj.getSalary());
}
System.out.println(ch);
}
}
/**
* javabean 封裝資料
* @author MacBook
*
*/
class Employee implements java.io.Serializable{
private String name;
private double salary;
public Employee(String name,double salary){
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;
}
}
(2)序列化到檔案中
package cn.lesson.Burrfed;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* 資料流
* 1.先寫出後讀取
* 2.讀取和寫出的順序一致
* 3.不是所有的物件都可以序列化,必須要使用serializable介面
* @author MacBook
*
*/
public class DataTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//選擇流
ObjectOutputStream dos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
//寫出
dos.writeUTF("啦啦啦啦");
dos.writeInt(12);
dos.writeBoolean(false);
dos.writeChar('a');
//物件
dos.writeObject("誰解其中味");
dos.writeObject(new Date());
Employee emp = new Employee("馬雲", 400);
dos.writeObject(emp);
dos.flush();
dos.close();
//讀取
//選擇流
ObjectInputStream dis =
new ObjectInputStream(
new BufferedInputStream(
new FileInputStream("obj.txt")));
String msg = dis.readUTF();
int age = dis.readInt();
boolean i = dis.readBoolean();
char ch = dis.readChar();
Object str = dis.readObject();
Object date = dis.readObject();
Object employee = dis.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(employee instanceof Employee) {
Employee empObj = (Employee)employee;
System.out.println(empObj.getName()+"-->"+empObj.getSalary());
}
dis.close();
}
}
/**
* javabean 封裝資料
* @author MacBook
*
*/
class Employee implements java.io.Serializable{
private transient String name;
private double salary;
public Employee(String name,double salary){
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;
}
}