首页 > 编程语言 > 详细

Java序列简单使用

时间:2014-09-29 10:30:51      阅读:236      评论:0      收藏:0      [点我收藏+]
package javatest;

import java.io.*;

public class SerializableTest implements Serializable {
	public static class Test implements Serializable {
		private int width;
		private int height;

		public void setWidth(int width) {
			this.width = width;
		}

		public void setHeight(int height) {
			this.height = height;
		}
	}

	public static void write(Object obj, String path) {
		try {
			File f=new File(path);
		       if(f.exists()){
		           f.delete();
		       }
		       
			FileOutputStream fs = new FileOutputStream(path);
			ObjectOutputStream os = new ObjectOutputStream(fs);
			os.writeObject(obj);
			os.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static Object read(String path) throws IOException,
			ClassNotFoundException {
		FileInputStream fs = new FileInputStream(path);
		ObjectInputStream os = new ObjectInputStream(fs);
		return os.readObject();
	}

	public static void main(String[] args) throws ClassNotFoundException, IOException {
		Test test = new Test();
		test.setWidth(50);
		test.setHeight(30);
		
		String path = "ser.file";
		write(test, path);
		Test test1 = (Test)read(path);
		System.out.println(test1.height);
	}
}

 

Java序列简单使用

原文:http://www.cnblogs.com/chengxin1982/p/3999597.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!