IO流、序列化和反序列化
来源:互联网 发布:淘宝静电消除器原理 编辑:程序博客网 时间:2024/04/29 21:01
package com.xupk.serialize;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.io.Serializable;/*** * 将对象数组序列化到硬盘文件中,再反序列化回来 * * @author xupk * */public class ObjStream {public static void main(String[] args) {Person p1 = new Person("xupk", "男", 18, new Car("alue", 166));Person p2 = new Person("xupk", "男", 28, new Car("blue", 266));Person p3 = new Person("xupk", "男", 38, new Car("clue", 366));Person[] ps = new Person[] { p1, p2, p3 };String path = "C:/workspace/eclipse01/IOPrj";String fileName = "p2.txt";File file = ser(ps, path, fileName);System.out.println("name: " + file.getName());System.out.println("parent: " + file.getParent());System.out.println("parentFile: " + file.getParentFile());System.out.println("path: " + file.getPath());System.out.println("absolutePath: " + file.getAbsolutePath());System.out.println("absoluteFile: " + file.getAbsoluteFile());Person[] pp = dser(file);for (Person person : pp) {System.out.println(person);}}/*** * 序列化对象,将对象输出到文件中 * @param ps * @param path 文件路径 * @param fileName 文件名 * @return 保存序列化对象的文件 */private static <T extends Serializable> File ser(T ps, String path,String fileName) {OutputStream outs = null;ObjectOutputStream out = null;File file = new File(path, fileName);//如果指定路径的文件不存在,就新建一个文件if (!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}try {outs = new FileOutputStream(file);//从file中读取文件字节输出流out = new ObjectOutputStream(outs);//将输出流转成对象字节输出流out.writeObject(ps);//把对象写到输出流中} catch (Exception e) {} finally {try {outs.close();out.close();} catch (Exception e2) {}}return file;}/*** * 反序列化对象,从文件中读取对象 * * @param file 从file中读取数据 * @return 返回反序列化后的对象 */@SuppressWarnings("unchecked")private static <T extends Serializable> T dser(File file) {if (!file.exists()) {try {throw new FileNotFoundException("");} catch (FileNotFoundException e) {e.printStackTrace();}}InputStream ins = null;ObjectInputStream in = null;try {ins = new FileInputStream(file);in = new ObjectInputStream(ins);return (T) in.readObject();} catch (Exception e) {} finally {try {ins.close();in.close();} catch (Exception e2) {}}return null;}}
package com.xupk.serialize;import java.io.Serializable;public class Person implements Serializable {/** * */private static final long serialVersionUID = -8350010615659256735L;private String name;private String sex;private int age;private Car car;public Person(String name, String sex, int age, Car car) {super();this.name = name;this.sex = sex;this.age = age;this.car = car;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}@Overridepublic String toString() {return "Person [name=" + name + ", sex=" + sex + ", age=" + age+ ", car=" + car + "]";}}
package com.xupk.serialize;import java.io.Serializable;/** * @author xupk * */public class Car implements Serializable{/** * */private static final long serialVersionUID = -555269900556154294L;public Car(String color, int maxSpeed) {super();this.color = color;this.maxSpeed = maxSpeed;}@Overridepublic String toString() {return "Car [color=" + color + ", maxSpeed=" + maxSpeed + "]";}private String color;private int maxSpeed;public String getColor() {return color;}public void setColor(String color) {this.color = color;}public int getMaxSpeed() {return maxSpeed;}public void setMaxSpeed(int maxSpeed) {this.maxSpeed = maxSpeed;}}
值得注意的是,序列化的类都要实现Serializable接口,如果不是,那么将无法序列化。上面的ObjStream类中,两个方法我都用了泛型,有一句<T extends Serializable>就是指定序列化的类必须要实现Serializable接口的,不然编译期都会报错了。
阅读全文
0 0
- IO流之序列化和反序列化
- IO流、序列化和反序列化
- Java Io 对象序列化和反序列化
- IO学习(十二)序列化和反序列化
- java IO 序列化和反序列化
- 序列和反序列化
- 序列和反序列化
- IO流对象序列化与反序列化
- IO流丶序列化与反序列化
- IO流之序列化与反序列
- IO流反序列化(objectInputStream)和序列化(ObjectOutputStream)连续存储对象StreamCorruptedException
- 18、Java入门—Java IO流之对象的序列化和反序列化
- IO流实现对象的序列化和反序列化
- java io流中为什么要序列化和反序列化
- Io流——将对象序列化和反序列化
- 【Java IO流】对象的序列化和反序列化
- java Io流中对象序列化和反序列问题
- 二进制流序列化(反序列化)和XML序列化(反序列化)
- OPPO R7 Plus推出巴萨定制版 梅西签名版将拍卖
- 连续三代产品打进苹果店的iBaby如何以硅谷创新为基因打出蓝海市场
- 机械硬盘
- 汉罗塔问题java数学模型(从数学原型到数学模型)
- 智能手机哪家强,2015高性价比手机推荐
- IO流、序列化和反序列化
- mtk led驱动总结1
- 为什么唯独Mac缺席了苹果发布会?
- centos 6.x 关闭防火墙和selinux
- 中国日报:OPPO与巴萨合作意义重大,其海外布局亮眼
- 更快、更省电、更安全——UU安全资深评测
- 你扭着屁股四处撒欢,小黄人非“小黄人”?
- 中港之战收视率创新高 金立体育营销首战告捷
- 高斯判别分析(GDA)和朴素贝叶斯(NB)