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

    把java中的对象保存在持久化文件中,这就是序列化!

    把持久化文件中保存的信息还原为java中的对象,这就是反序列化!

    想实现序列化和反序列化必须让对象所在的类实现一个Serializable接口!

    public class Student implements Serializable{}

        
         //序列化
    public static void register() { Scanner input = new Scanner (System.in); System.out.println("请输入学号"); int id = input.nextInt(); System.out.println("请输入年龄:"); int age = input.nextInt(); System.out.println("请输入姓名:"); String name = input.next(); Student stu = new Student(id, age, name); OutputStream out = null; ObjectOutputStream oos = null; try { out = new FileOutputStream("d:/student.txt"); oos = new ObjectOutputStream(out); oos.writeObject(stu); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { oos.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
         //反序列化
         private
    static void login() { InputStream in = null; ObjectInputStream ois = null; try { in = new FileInputStream("d:/student.txt"); ois = new ObjectInputStream(in); Student stu = (Student) ois.readObject(); System.out.println(stu.getAge()); System.out.println(stu.getId()); System.out.println(stu.getName()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
  • 相关阅读:
    不同环境下vue-cli3+打包命令配置
    本地node服务启动vue打包项目
    js匿名函数
    本地vue扩展程序。
    vant轮播插件swipe实现三个一屏,并修改指示器样式
    vue中placeholder中使用字体图标
    为什么js中重复多次调用正则时会报错,会交替出现的那种
    create-react-app兼容ie9配置
    react之form表单工具:formik+yup
    Flex布局
  • 原文地址:https://www.cnblogs.com/liutianci/p/8168022.html
Copyright © 2011-2022 走看看