java 序列化和反序列化数据

使用ObjectOutputStream 序列号原始数据和对象数据,使用ObjectInputStream 反序列化

使用字节存储数据,可以将序列化的数据存储到硬盘上,或输出到网络上

package com.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

public class ObjecyOutputStreamDemo {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
        out();
		in();
		
	}
    public static void out() throws IOException{
    	FileOutputStream fileOutputStream = new FileOutputStream("F:/a.txt");
		ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
		oos.writeInt(124);
		oos.writeObject("Today");
		oos.writeObject(new Date());
		oos.close();
		
		
    }
    public static void in() throws IOException, ClassNotFoundException{
    	FileInputStream in = new FileInputStream("F:/a.txt");
    	ObjectInputStream ois = new ObjectInputStream(in);
    	 int i = ois.readInt();
         String today = (String) ois.readObject();
         Date date = (Date) ois.readObject();
         System.out.println(i+today+date);
    	ois.close();
    }
}

  

原文地址:https://www.cnblogs.com/newlangwen/p/7429411.html