java中的ObjectInputStream和ObjectOutputStream的使用

一、ObjectOutputStream

ObejctOutputStream是将一个对象转化为字节写到一个文件中去,以此来实现对象的持久化保存,这个过程被称为序列化。

构造方法:

  public ObjectOutputStream(OutputStream out) throws IOException {

  写入对象:

void writeObject(Object obj) 将指定的对象写入 ObjectOutputStream。

创建步骤:

   1、创建ObjectOutputStream对象,构造方法中传递字节输出流OutputStream

   2、通过使用writeObejct(Object obj)写入对象 ,数组和String都可以被写入到文件中

  3、关闭流 close()

注意:

1、需要实现序列化的对象必须继承 java.io.Serialiable接口,否则会抛出NotSerialiableException异常。同时如果一个类中存在引用,那么该引用也需要实现Serialiable接口。

2、对于不需要序列化的属性或者对象可以使用transient来进行修饰。

3、序列化一般用于深度复制

实现代码: 

  构造被序列化的类Employee

package IO;

public class Employee implements java.io.Serializable {
	 private transient int age;
	 private  String name;
	public Employee(int age, String name) {
		super();
		this.age = age;
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	 

}

  存储对象和读取对象

package IO;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
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 ObjectDemo0 {
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		
	seri("e:/1.txt");
	read("e:/1.txt");
	}
	
	//序列化对象
	public static void seri(String pathname) throws FileNotFoundException, IOException
	{
		Employee employee=new Employee(100, "猴子那个样");
		File file =new File(pathname);
		ObjectOutputStream objectInputStream=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
	  objectInputStream.writeObject(employee);
	  //将对象写入文件中去
	  objectInputStream.close();
		
	}
	//从文件中读取对象
	public static void read(String pathname) throws FileNotFoundException, IOException, ClassNotFoundException
	{
		File file =new File(pathname);
		ObjectInputStream objectInputStream=new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
	  Object object=objectInputStream.readObject();
	  if(object instanceof Employee)
	  {
		 Employee employee=   (Employee) object;
		System.out.println(employee.getAge()); 
		System.out.println(employee.getName()); 
	  }
	  
	  //将对象写入文件中去
		
	}


}

 

二、ObjectInputStream

ObejctInputStream是从文件中读取被序列化的对象。

构造方法:

ObjectInputStream(InputStream in) 创建从指定 InputStream 读取的 ObjectInputStream。

  特有方法:

readObject()

注意:

serialVersionUID 是用于记录class文件的版本信息的,serialVersionUID这个数字是通过一个类的类名、成员、包名、工程名算出的一个数字。使用ObejctInputStream进行反序列化的时候,ObejctInputStream首先会读取文件中的serialVersionUID,然后与本地的Class文件进行对比,如果两个serialVersionUID 不同的话,那么反序列化失败,所以建议在类中加上serialVersionUID属性

private static final long serialVersionUID = 1L;

 

 

 

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值