墨竹小院
代码改变世界

Java序列化和反序列化

2011-06-30 13:22  MzXy  阅读(162)  评论(0)    收藏  举报

被序列化的类

package com.mzxy.io;

import java.io.Serializable;
/**
 * @author MzXy
 * 
 * */

public class Book implements Serializable {
	private String book_name;// 书名
	private String book_auther;// 作者
	private int isbn;// 图书编号
	private int editionNumber;// 版本
	private String copyright;// 版权
	private int publisherID;// 出版社ID
	private String imageFIle;// 封面图片
	private double price;// 价格

	/**
	 * @return the book_name
	 */
	public String getBook_name() {
		return book_name;
	}

	/**
	 * @param bookName
	 *            the book_name to set
	 */
	public void setBook_name(String bookName) {
		book_name = bookName;
	}

	/**
	 * @return the book_auther
	 */
	public String getBook_auther() {
		return book_auther;
	}

	/**
	 * @param bookAuther
	 *            the book_auther to set
	 */
	public void setBook_auther(String bookAuther) {
		book_auther = bookAuther;
	}

	/**
	 * @return the book_beizhu
	 */

	public Book() {
	}

	public int getIsbn() {
		return isbn;
	}

	public void setIsbn(int isbn) {
		this.isbn = isbn;
	}

	public int getEditionNumber() {
		return editionNumber;
	}

	public void setEditionNumber(int editionNumber) {
		this.editionNumber = editionNumber;
	}

	public String getCopyright() {
		return copyright;
	}

	public void setCopyright(String copyright) {
		this.copyright = copyright;
	}

	public int getPublisherID() {
		return publisherID;
	}

	public void setPublisherID(int publisherID) {
		this.publisherID = publisherID;
	}

	public String getImageFIle() {
		return imageFIle;
	}

	public void setImageFIle(String imageFIle) {
		this.imageFIle = imageFIle;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public Book(String book_name, String book_auther, int isbn,
			int editionNumber, String copyright, int publisherID,
			String imageFIle, double price) {
		this.setBook_auther(book_auther);
		this.setBook_name(book_name);
		this.setCopyright(copyright);
		this.setEditionNumber(editionNumber);
		this.setImageFIle(imageFIle);
		this.setIsbn(isbn);
		this.setPrice(price);
		this.setPublisherID(publisherID);

	}

	public String toString() {
		return "书名: " + this.getBook_name();
	}
}

序列化操作类

package com.mzxy.io;

import java.io.*;

public class Test {

	// 序列化对象到文件
	public static void serialize(String fileName) {
		try {

			FileOutputStream f = new FileOutputStream(fileName);//创建文件输出流
			ObjectOutputStream s = new ObjectOutputStream(f);//创建对象输出流
			Book user = new Book();//实例化对象
			user.setBook_auther("罗贯中");
			user.setBook_name("《三国演绎》");
			user.setCopyright("人民出版社");
			user.setEditionNumber(1254623);
			s.writeObject(user);//执行序列化方法
			s.close();
		} catch (Exception x) {
			System.out.println(x.toString());
		}

	}

	// 从文件反序列化到对象
	public static void deserialize(String fileName) {
		try {
			// 创建一个对象输入流,从文件读取对象
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(
					fileName));

			// 读取UserInfo对象并调用它的toString()方法
			Book user = (Book) (in.readObject());
			System.out.println(user.toString());
			in.close();
		} catch (Exception x) {
			System.out.println(x.toString());
		}

	}

	public static void main(String[] args) {

		serialize("F:\\test.txt");
		System.out.println("序列化完毕");

		deserialize("F:\\test.txt");
		System.out.println("反序列化完毕");
	}
}