zoukankan      html  css  js  c++  java
  • Java序列化与反序列化

    1. 存在背景 :在内存中可以创建可复用的Java对象,,但是会随着JVM的停止儿消失,但是在部分场景中,JVM停止后需要保存指定的对象,Java对象序列化满足了这一需求
    2. 实现过程

    • 实体类实现 Serializable接口 
    public class User implements Serializable
    • ObjectInputStream写入对象
        //write object into file
        ObjectOutputStream oos = null;       try{ oos = new ObjectOutputStream(new FileOutputStream("tempfile")); oos.writeObject(user); }catch (Exception e){ e.printStackTrace(); }finally { IOUtils.closeQuietly(oos); }
    • ObjectOutputStream 读取对象
        //read object from file
            File file = new File("tempFile");
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream(file));
                User newUser = (User) ois.readObject();
                System.out.println(newUser);
            }catch (IOException e){
                e.printStackTrace();
            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }finally {
                IOUtils.closeQuietly(ois);
                try {
                    FileUtils.forceDelete(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

      

  • 相关阅读:
    Struts2 拦截器
    Struts2 常用标签
    Struts2 OGNL表达式、ValueStack
    Struts2 在Action中操作数据
    Struts2 动态方法调用
    Struts2 常量配置
    Struts2 struts.xml配置
    Struts2 Action的3种创建方式
    Struts2 运行流程
    JUnit
  • 原文地址:https://www.cnblogs.com/gara/p/9397191.html
Copyright © 2011-2022 走看看