zoukankan      html  css  js  c++  java
  • JavaLearning:对象序列化

    package org.fun.io;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    @SuppressWarnings("serial")
    class Person implements Serializable {
    	private String name;
    	private int age;
    
    	public Person(String name, int age) {
    		this.name = name;
    		this.age = age;
    	}
    
    	public String toString() {
    		return "姓名:" + this.name + ",年龄:" + this.age;
    	}
    }
    
    public class SerializableDemo {
    	public static void main(String[] args) throws Exception {
    		Person[] per= { new Person("张三", 30), new Person("李四", 31),
    				new Person("王五", 32) };
    		ser(per);
    		Person[] p= (Person[]) dser();
    		print(p);
    	}
    
    	public static void ser(Object obj) throws Exception {
    		File file = new File("d:" + File.separator + "person.ser");
    		ObjectOutputStream oos = null;
    		oos = new ObjectOutputStream(new FileOutputStream(file));
    		oos.writeObject(obj);
    		oos.close();
    	}
    
    	@SuppressWarnings("resource")
    	public static Object dser() throws Exception {
    		Object temp = null;
    		File file = new File("d:" + File.separator + "person.ser");
    		ObjectInputStream ois = null;
    		ois = new ObjectInputStream(new FileInputStream(file));
    		temp = ois.readObject();
    		return temp;
    	}
    
    	public static void print(Person per[]) {
    		for (Person p : per) {
    			System.out.println(p);
    		}
    	}
    }
    

  • 相关阅读:
    去掉谷歌浏览器下input框自动填充的背景色
    ajax请求中动态显示问题
    Array对象的方法有
    请求页面的方法
    IE浏览器checkbox的样式问题
    property、classmethod和staticmethod总结
    面向对象和类
    内置函数补充map、reduce和filter等
    python实现控制台的进度条功能
    python常见内置函数
  • 原文地址:https://www.cnblogs.com/javafly/p/6037245.html
Copyright © 2011-2022 走看看