java Serializable

Posted 不被女生喜欢好多年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
	}

}

  

以上是关于java Serializable的主要内容,如果未能解决你的问题,请参考以下文章

JAVA IO流相关代码(Serializable接口,管道流PipedInputStream类,RandomAccessFile类相关代码)

JAVA IO流相关代码(Serializable接口,管道流PipedInputStream类,RandomAccessFile类相关代码)

JAVA基础-Serializable序列化学习总结

java里面 implements Serializable有啥作用?

Java对象为啥要实现Serializable接口?

尝试在 Android 上调用虚拟方法