对象的序列化与反序列化——ObjectOutputStream、ObjectInputStream

对象流

  1. 作用:用于存储和读取基本数据类型对象或者对象的处理流,它的强大之处是将java中的对象写入到数据源中,也能发数据源中的数据还原回来。
  2. 序列化与反序列化。
  3. 对象的序列化机制:
  • 序列化:允许将内存中的Java对象转化为平台无关的二进制流,从而允许把这种二级制流 持久的保存在磁盘上,或者通过网络将这种二进制流传输到另一个网络节点。
  • 反序列化:当其他程序获取这种二级制流,就可以恢复成原来的Java对象。
public class ObjectInputStreamOutputStreamTest{
    
    
	//序列化
	@Test
	public void testObjectOutputStream(){
    
    
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("hello,dat"));
		
		oos.writeObject(new String("我爱coding,coding爱我"));
		oos.flush();
		oos.close();
	}
	
	//反序列化
	@Test
	public void testObjectInputStream(){
    
    
		ObjectInputStream ois = new ObjectInputStream(new FileOutputStream("object.dat"));
		Object obj = ois.readObject();
		String str = (String)obj;
		System.out.println(str);
		ois.close();		
	}
}
  1. 要想一个Java对象可以序列化需要满足以下要求
  • 需要实现
package com.ntt.sts;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class HomeWork implements Serializable {
    
    

    private static final long serialVersionUID = -12312412412412L;
    private String title;
    private String content;

    public String getTitle() {
    
    
        return title;
    }

    public void setTitle(String title) {
    
    
        this.title = title;
    }

    public String getContent() {
    
    
        return content;
    }

    public void setContent(String content) {
    
    
        this.content = content;
    }

    public HomeWork(String title, String content) {
    
    
        this.title = title;
        this.content = content;
    }

    public HomeWork() {
    
    
    }

    @Override
    public String toString() {
    
    
        return "HomeWork{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

package com.ntt.sts;

import org.junit.Test;

import java.io.*;

public class ObjectOutputInputStreamTest {
    
    

    @Test
    public void testObjectOutputStream() throws IOException {
    
    
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("hello.txt"));
        objectOutputStream.writeObject(new HomeWork("日语作业","123456"));
        objectOutputStream.flush();
        objectOutputStream.close();
    }
    @Test
    public void testObjectInputStream() throws IOException{
    
    
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("hello.txt"));
        try {
    
    
            Object o =objectInputStream.readObject();
            HomeWork homeWork = new HomeWork();
            homeWork = (HomeWork)o;
            System.out.println(homeWork.toString());
        } catch (ClassNotFoundException e) {
    
    
            e.printStackTrace();
        }
    }
}

和输入输出流结合使用

package com.ntt.sts;

import org.junit.Test;

import java.io.*;

public class ObjectOutputInputStreamTest {
    
    

    @Test
    public void testObjectOutputStream() throws IOException {
    
    

        //从外部环境中读取一段文字
        File file = new File("hello.txt");
        FileReader fileReader = new FileReader(file);
        
        char[] chars = new char[1024];
        int lenth;
        String content= "";
        while ((lenth = fileReader.read(chars)) != -1){
    
    
            content = new String(chars,0,lenth);
            System.out.println(content);
        }
        //写出去
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("homeWork.txt"));
        objectOutputStream.writeObject(new HomeWork(content));
        //关闭流
        fileReader.close();
        objectOutputStream.flush();
        objectOutputStream.close();
    }
    @Test
    public void testObjectInputStream() throws Exception{
    
    
        //读入待序列化的文件
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("homeWork.txt"));

        Object o =objectInputStream.readObject();
        HomeWork homeWork = new HomeWork();
        homeWork = (HomeWork)o;
        String content = homeWork.getContent();
        System.out.println(homeWork.toString());
        //也可以将文件代写出去
        File file = new File("homeWork1.txt");
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(content);
        fileWriter.close();
        objectInputStream.close();
    }
}

序列化和反序列化必须的条件

  1. 实现Serializable接口
  2. 定义 private static final long serialVersionUID = -12312412412412L;
  3. 对象和对象的属性都必须是可序列化的

猜你喜欢

转载自blog.csdn.net/weixin_43941676/article/details/108413239