Java中的IO流(下)——序列化与反序列化

一、引入

Case 1:
把ArrayList集合中的字符串数据存储到文本文件中

public class Demo04 {
	public static void main(String[] args) {
		//创建集合
		List<String> list = new ArrayList<String>();
		//加入元素
		list.add("时间有泪");
		list.add("年轮");
		list.add("凉凉");
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(
					new OutputStreamWriter(
							new FileOutputStream("demo.txt")));
			//遍历集合
			for(String str:list) {
				bw.write(str);
			}
			System.out.println("写入成功");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

控制台显示:
在这里插入图片描述
Case 2:
读取test.txt里面的学生数据,并存入到集合中去

test.txt文件的内容为(事先存好):
在这里插入图片描述

public class Demo04 {
	public static void main(String[] args) {
		List<Student> list = new ArrayList<Student>();
		//开始读取数据
		BufferedReader br = null;
		try {
			br = new BufferedReader(
					new InputStreamReader(
							new FileInputStream("test.txt")));
			String str = null;
			while((str = br.readLine())!=null) {
				String[] ss = str.split(",");//用,来拆分字符串
				Student s = new Student(ss[0],ss[1],ss[2]);
				list.add(s);
			}
			System.out.println("存储完毕");
			System.out.println(list);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
class Student{
	//序号
	private String num;
	//姓名
	private String name;
	//年龄
	private String age;
	/**
	 * 有参构造方法
	 */
	public Student(String num, String name, String age) {
		super();
		this.num = num;
		this.name = name;
		this.age = age;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [num=" + num + ", name=" + name + ", age=" + age + "]";
	}
}

控制台显示:
在这里插入图片描述
那么:
当我们想将程序中的对象存储到文件中,该用什么方法?
answer:要用到序列化与反序列化的知识。

二、对象的序列化与反序列化概述

定义:
序列化:将对象转换成方便传输的流(存到文件中)
反序列化:将流转换成对象(可以在程序中操作对象)

注意:
需要传输的对象对应的实体类必须实现
implements Serializable接口
Serializable接口中没有属性及方法,它就相当于一个标志,如果一个实体类实现了它,就可以进行序列化与反序列化。
如果没有实现该接口,则会有java.io.NotSerializableException异常。

使用方法:
序列化:ObjectOutputStream(流)
使用:writeObject(obj):将obj对象序列化到流中

反序列化:ObjecIntputStream(流)
使用:readObject():将流中的对象反序列化成对象

三、序列化与反序列化(初步)

只序列化一个对象到文件中,以及反序列化回到程序来。
1、序列化:

public class Demo05 {
	public static void main(String[] args) {
		//要存入的对象
		Person p = new Person("666@qq.com","123456","ff");
		//创建流
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(
					new FileOutputStream("test.txt"));
			oos.writeObject(p);
			System.out.println("序列化成功!");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
class Person implements Serializable{
	/**
	 * 序列化的ID,是User对象被序列化之后的唯一标识
	 * 还有另外一种生成方式。
	 */
	private static final long serialVersionUID = 1L;
	
	private String email;
	private String pwd;
	private String name;
	public Person(String email, String pwd, String name) {
		super();
		this.email = email;
		this.pwd = pwd;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Person [email=" + email + ", pwd=" + pwd + ", name=" + name + "]";
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

控制台显示:
在这里插入图片描述
2、反序列化
基于进行上述序列化操作后的文件,再进行反序列化。

public class Demo05 {
	public static void main(String[] args) {
		
		//创建流
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(
					new FileInputStream("test.txt"));
			Person p = (Person) ois.readObject();
			System.out.println(p);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

控制台显示:
在这里插入图片描述
PS:
反序列化时可能会遇到:java.io.EOFException:当读取过程中意外读取到文件或流的末尾时,End Of File。

四、引申

当我将输入流设置为追加,运行多次,在文件中存储对应个数的对象,此时我的序列化与反序列化还能否实现。
(以两个对象的序列与反序列化为例)

public class Demo05 {
	public static void main(String[] args) {
		//要存入的对象
		Person p = new Person("666@qq.com","123456","ff");
		//创建流
		ObjectOutputStream oos = null;
		try {
			//第一处修改:
			oos = new ObjectOutputStream(
					new FileOutputStream("test.txt",true));
			oos.writeObject(p);
			System.out.println("序列化成功!");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		//创建流
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(
					new FileInputStream("test.txt"));
			Person p1 = (Person) ois.readObject();
			System.out.println(p1);
			//第二处修改:
			Person p2 = (Person) ois.readObject();
			System.out.println(p2);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
class Person implements Serializable{
	/**
	 * 序列化的ID,是User对象被序列化之后的唯一标识
	 * 还有另外一种生成方式。
	 */
	private static final long serialVersionUID = 1L;
	
	private String email;
	private String pwd;
	private String name;
	public Person(String email, String pwd, String name) {
		super();
		this.email = email;
		this.pwd = pwd;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Person [email=" + email + ", pwd=" + pwd + ", name=" + name + "]";
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

出现异常:
在这里插入图片描述
分析:
1、java.io.StreamCorruptedException: invalid type code: AC 无效代码。
2、序列化时,每次调用writeObject()方法写入对象,都会先调用 writeStreamHeader() 这个方法,从而在文件中写入头标记。但是在一个文件中有一个头标记即可,有多个头标记读取第二个对象是会报java.io.StreamCorruptedException: invalid type code: AC 异常。
3、解决方法:
进行文件中第一个对象的序列化时,加上头标记。
后面的对象则不用加。
如果不是第一次写入文件就用自己创建的子类Sub写入对象,就不会写入头标记。

五、有多个对象的序列及反序列化

(1)序列化:

public class Sub extends ObjectOutputStream{
	//此处要注意
	public Sub(OutputStream out) throws IOException {
		super(out);
	}
	//重写writeStreamHeader,则使用此类创建的对象不会自动加上头标记
	@Override
	protected void writeStreamHeader() throws IOException {
		//super.writeStreamHeader();
	}
}
public class Demo05 {
	public static void main(String[] args) {
		ObjectOutputStream oos = null;
		FileOutputStream fos = null;
		try {
			//创建文件对象
			File file = new File("text.txt");
			//创建字节输出流
			fos = new FileOutputStream(file,true);//允许追加
			if(file.length() < 1) {//如果文件长度为空
				System.out.println("111");
				oos = new ObjectOutputStream(fos);
				System.out.println(oos);
			}else {//用自定义的类去创建对象
				System.out.println("222");
				oos = new Sub(fos);
				System.out.println(oos);
			}
			//要存入的对象
			Person p = new Person("666@qq.com","123456","ff");
			oos.writeObject(p);
			System.out.println("序列化成功!");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
class Person implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private String email;
	private String pwd;
	private String name;
	public Person(String email, String pwd, String name) {
		super();
		this.email = email;
		this.pwd = pwd;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Person [email=" + email + ", pwd=" + pwd + ", name=" + name + "]";
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

文件显示:
在这里插入图片描述
(2)反序列化
自定义异常,当读取到文件末尾时。

public class Demo06 {
	public static void main(String[] args) {
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(
					new FileInputStream("text.txt"));
			try {
				Object obj = null;
				while((obj = ois.readObject())!=null) {
					Person p = (Person)obj;
					System.out.println(p);
				}
			}catch(EOFException e) {//当文件读取到末尾,自定义异常
				System.out.println("已读取完毕");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值