zoukankan      html  css  js  c++  java
  • java基础之序列化与反序列化

    原理:

     Java序列化是指把Java对象转换为二进制的数据流
    Java反序列化是指把字节序列恢复为Java对象的过程。

    如何实现序列化?

    将需要序列化的类实现Serializable接口就可以了,Serializable接口中没有任何方法,可以理解为一个标记,即表明这个类可以序列化。

    代码原理:

    private static void read() throws IOException, ClassNotFoundException {
    		// 创建反序列化对象
    		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"));
    
    		// 还原对象
    		Object obj = ois.readObject();
    
    		// 释放资源
    		ois.close();
    
    		// 输出对象
    		System.out.println(obj);
    	}
    
    	private static void write() throws IOException {
    		// 创建序列化流对象
    		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"));
    
    		// 创建对象
    		Person p = new Person("霞", 27);
    
    		// public final void writeObject(Object obj)
    		oos.writeObject(p);
    
    		// 释放资源
    		oos.close();
    	}
    
    具体代码:

    1.序列化对象

    package com.etc.day16.xulie;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    //序列化对象
    public class TestDemo_1 {
    	public static void main(String[] args) {
    		ObjectOutputStream oos = null;
    		try {
    			 oos = new ObjectOutputStream(new FileOutputStream("e:\x.txt"));
    			
    			Demo_1 person = new Demo_1("小明",18);
    			oos.writeObject(person);
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally{
    			if(oos!=null){
    				try {
    					oos.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				
    			}
    		}
    		
    		
    		
    		
    		
    		
    		
    		
    	}
    
    }
    
    2.反序列化

    package com.etc.day16.xulie;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    
    public class TestDemo_2 {
    	public static void main(String[] args) {
    		//反序列化
    		ObjectInputStream ois = null;
    		// 创建反序列化对象
    		try {
    			ois = new ObjectInputStream(new FileInputStream("e:\x.txt"));
    			
    			//还原对象
    			Demo_1 person =(Demo_1)ois.readObject();
    			System.out.println(person);
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally{
    			
    			if(ois!=null){
    				try {
    					ois.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}
    		
    		
    		
    		
    		
    		
    		
    		
    	}
    
    }
    


  • 相关阅读:
    Merge Two Sorted Lists
    4Sum
    Letter Combinations of a Phone Number
    3Sum Closest
    3Sum
    Longest Common Prefix
    Roman to Integer
    Integer to Roman
    Container With Most Water
    Regular Expression Matching
  • 原文地址:https://www.cnblogs.com/jatpeo/p/11767590.html
Copyright © 2011-2022 走看看