zoukankan      html  css  js  c++  java
  • java IO之ObjectInputStream和ObjectOutputStream

    package com.io.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.io.Serializable;

    import org.junit.Test;

    /**
     * 直接把object写入或者读出
     * 
     * 
    @author chengjj
     * 
     
    */
    public class TestObjectStream {
        @Test
        public void testObjectStream() {
            try {
                T t = new T();
                t.k = 8;
                FileOutputStream fos = new FileOutputStream("E:/4.txt");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(t);
                oos.flush();
                oos.close();

                FileInputStream fis = new FileInputStream("E:/4.txt");
                ObjectInputStream ois = new ObjectInputStream(fis);
                T otherT = (T) ois.readObject();
                System.out.println(otherT.i);
                System.out.println(otherT.h);
                System.out.println(otherT.d);
                System.out.println(otherT.k);
                ois.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 标记性接口,标记给编译器看
     * transient 修饰的成员变量在序列化的时候不予考虑
     * 
    @author chengjj
     * 
     
    */
    class T implements Serializable {
        int i = 10, h = 9;
        double d = 2.3;
        transient int k = 15;
         
    }
  • 相关阅读:
    VS Code 编译运行C/C++
    滴水逆向-文件读写&内存分配-PE准备阶段
    技术从业者的未来(三)
    房价预测-California House Prices
    记录一下Linux的文件夹的rwx权限使用。
    对微任务和宏任务的执行顺序的个人理解
    算法1.4 节 ----- 算法分析
    .NetCore中间件实现原理
    谷歌浏览器禁用JS步骤
    管理经验分享
  • 原文地址:https://www.cnblogs.com/cjunj/p/2755735.html
Copyright © 2011-2022 走看看