java.se.io.11.序列化与反序列化

来源:互联网 发布:2016年nba西部决赛数据 编辑:程序博客网 时间:2024/06/05 00:23
package com.knock.io;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.Serializable;import org.junit.Test;/** * @date 160802pm * 处理引用类型,保留数据+类型, * 读入时(反序列化) * 写出(序列化) * 序列化对象需要实现serializable接口,空接口 * 不需要序列化的属性使用transient修饰 * 留意顺序 * */public class KlObject {@Testpublic void test(){Employee employee = new Employee("ATuYi",10000);File dest = new File("E:/test/a.txt");ObjectOutputStream oos = null;try {oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));oos.writeObject(employee);} catch (IOException e) {e.printStackTrace();}finally{try {if(null!=oos){oos.close();}} catch (IOException e) {e.printStackTrace();}}}}class Employee implements Serializable{transient String name ;int salary;public Employee() {super();}public Employee(String name, int salary) {super();this.name = name;this.salary = salary;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getSalary() {return salary;}public void setSalary(int salary) {this.salary = salary;}}

0 0
原创粉丝点击