Java序列化、反序列化实战代码(随笔)

    创建实体类,实现序列化接口类: 

package com.kgc7;

import java.io.Serializable;
//定义一个Student类,实现接口(implements Serializable)
public class Student implements Serializable{
    String name;
    String gender;
    int age;

    public Student(String name, String gender, int age) {
	this.name = name;
	this.gender = gender;
	this.age = age;
    }
}

     单个实体序列化测试:

package com.kgc7;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
//序列化信息到默认保存文件"stu.txt",需要使用文件输出流FileOutputStream,对象输出流ObjectOutputStream。
public class Test {
    public static void main(String[] args) {
	FileOutputStream fos = null;
	ObjectOutputStream oos = null;
	try {
	    fos = new FileOutputStream("stu.txt");
	    oos = new ObjectOutputStream(fos);
	    Student stu = new Student("小梁", "男", 26);
	    oos.writeObject(stu);
	    oos.flush();   //缓冲
	    oos.close();   //关掉流
	    fos.close();
	} catch (FileNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
}

   多个实体序列化测试:

package com.kgc7;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Test1 {
    /**
     * 序列化多个学生信息
     * 
     * @param args
     */
    public static void main(String[] args) {
	// TODO Auto-generated method stub
	FileOutputStream fos = null;
	ObjectOutputStream oos = null;
	try {
	    fos = new FileOutputStream("stu.txt");
	    oos = new ObjectOutputStream(fos);
	    Student stu = new Student("小梁", "男", 26);
	    Student stu1 = new Student("小黄", "男", 20);
	    // 泛型集合存储学生信息
	    ArrayList stuList = new ArrayList();
	    //集合添加学生信息
	    stuList.add(stu);
	    stuList.add(stu1);
	    //将集合序列化
	    oos.writeObject(stuList);
	    //关闭流
	    oos.flush();
	    oos.close();
	    fos.close();
	} catch (FileNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
}

     反序列化文本文件内容测试:

package com.kgc7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * 反序列化文本文件内容
 * 
 * @author Administrator
 *
 */
public class Test2 {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) {
	// TODO Auto-generated method stub
	// 反序列化文本文件需要用到文件输入流FileInputStream;对象输入流ObjectInputStream。
	FileInputStream fis = null;
	ObjectInputStream ois = null;

	try {
	    fis = new FileInputStream("stu1.txt");
	    ois = new ObjectInputStream(fis);
	    // 读取文件内容并赋给list集合,ArrayList泛型集合,并进行强制转换。
	    ArrayList list = (ArrayList) ois.readObject();
	    // 用迭代器遍历集合输出集合对象到内存(控制台),调用构造函数输出对象信息。
	    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
		Student student = (Student) iterator.next();
		System.out.println(student.name + "\t" + student.gender + "\t" + student.age);
	    }
	    // 关闭流
	    ois.close();
	    fis.close();
	} catch (FileNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (ClassNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
}

输出结果("stu1.txt"文本文件内容):

小梁 男 26

小黄 男 20

你可能感兴趣的:(Java高级)