• java Serializable


    package com.zyw.serializablebean;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    public class SerializableTest {
    	public static void main(String[] args) { 
    		ObjectInputStream ois;
    		Person person1=new Person("张三","男");
    		try {
    			FileOutputStream fos=new FileOutputStream("G:\serialization.txt");
    			ObjectOutputStream oos=new ObjectOutputStream(fos);
    			oos.writeObject(person1);
    			System.out.println(person1.toString());
    			fos.close();
    			oos.close();
    			FileInputStream fis=new FileInputStream("G:\serialization.txt");
    			ois = new ObjectInputStream(fis);
    			Person p=(Person)ois.readObject();
    			System.out.println("name:"+p.getName()+" sex"+p.getSex()+" "+p.toString());
    		
    		} 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();
    		}
    	}
    
    }
    

      

    package com.zyw.serializablebean;
    
    import java.io.Serializable;
    
    public class Person implements Serializable {
    	private String name;
    	private transient  String sex;
    	public Person(String name, String sex) {
    		super();
    		this.name = name;
    		this.sex = sex;
    	}
    	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;
    	}
    	
    }
    

      

    package com.zyw.serializablebean;
    
    import java.io.Externalizable;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInput;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutput;
    import java.io.ObjectOutputStream;
    
    public class Blip3 implements Externalizable {
    	private  int i;
    	private String s;
    	public Blip3(){System.out.println("Blip3 Constructor");i=100;s="default";}
    	public Blip3(String s,int i){System.out.println("Blip("+i+","+s+")");this.s=s;this.i=i;}
    	public String toString(){return s+i;}
    	@Override
    	public void writeExternal(ObjectOutput out) throws IOException {
    		System.out.println("Blip3 writeExternal");
    		out.writeObject(s);
    		out.writeInt(i);
    	}
    	@Override
    	public void readExternal(ObjectInput in) throws IOException,
    			ClassNotFoundException {
    		System.out.println("blip3 readExternal");
    		s=(String)in.readObject();
    		i=in.readInt();
    	}
    	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    		Blip3 b=new Blip3("zhanyunwu",6666);
    		ObjectOutputStream o=new ObjectOutputStream(new FileOutputStream("G:\a.txt"));
    		o.writeObject(b);
    		o.close();
    		ObjectInputStream in=new ObjectInputStream(new FileInputStream("G:\a.txt"));
    		b=(Blip3)in.readObject();
    		System.out.println(b);
    	}
    
    }
    

      

  • 相关阅读:
    AngularJS(3)-过滤器
    AngularJS(2)-Scope作用域和控制器
    iOS局部刷新
    python(一)入门
    Java基础
    AngularJS(1)随笔
    mac下如何查看指定端口被谁占用并且杀死该进程
    Python 字节码bytecode
    Python 作用域和命名空间
    Python函数的默认参数的设计【原创】
  • 原文地址:https://www.cnblogs.com/yunwuzhan/p/5503573.html
Copyright © 2020-2023  润新知