zoukankan      html  css  js  c++  java
  • java的序列化和反序列化

    package com.lideng.work325;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    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;
    import java.io.Serializable;
    /**
     * 序列化和反序列化
     * @author Administrator
     *
     */
    public class Demo05  implements Serializable{
    
    	public static void main(String[] args) {
    		try {
    			test01("c:/Test.java");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	/**
    	 * 反序列化
    	 * @param file
    	 * @throws Exception
    	 */
    	public static void test01(String file) throws Exception{
    		ObjectInputStream os=new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
    		Object obj = os.readObject();
    		System.out.println(obj);
    		int[] num=(int[])obj;
    		for (int i = 0; i < num.length; i++) {
    			System.out.println(num[i]);
    		}
    		
    	}
    	/**
    	 * 序列化
    	 * @param file
    	 * @throws Exception
    	 */
    	public static void test(String file) throws Exception{
    		
    		int [] num={5,8,9,4,8};
    		
    		ObjectOutputStream os=new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream(file)));
    		os.writeObject(num);
    		os.flush();
    		os.close();
    	}
    }
    

      

  • 相关阅读:
    集训队日常训练20180518-DIV1
    集训队日常训练20180513-DIV1
    python类的使用与多文件组织
    性能指标
    python调用.so
    动态链接库的使用
    python读写xml文件
    使用python读取文本中结构化数据
    python画图
    numpy及scipy的使用
  • 原文地址:https://www.cnblogs.com/qurui1997/p/10609841.html
Copyright © 2011-2022 走看看