首页 > 其他分享 >一个demo快速理解序列号和反序列化

一个demo快速理解序列号和反序列化

时间:2024-05-02 17:25:20浏览次数:35  
标签:AppleTree name demo System println 序列号 序列化 out

一个demo快速理解序列号和反序列化

分享一个例子用来快速理解序列化和反序列化
其实序列化和反序列化就是为了交换数据,(简单粗暴的理解就是把运行中的对象存进文件里面)

import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception{
        System.out.println("________________java序列化________________");
        System.out.println("[+]new一个对象小李");
        person XiaoLi=new person(18,"小李");

        System.out.println("[+]调用XiaoLi的say方法");
        XiaoLi.say();

        System.out.println("[+]进行序列化\n    对象XiaoLi -> 文件XiaoLi.ser");
        FileOutputStream fileout=new FileOutputStream("XiaoLi.ser");
        ObjectOutputStream objout=new ObjectOutputStream(fileout);
        objout.writeObject(XiaoLi);
        objout.close();

        System.out.println("[+]进行反序列化\n   文件XiaoLi.ser -> 对象people");
        FileInputStream filein=new FileInputStream("XiaoLi.ser");
        ObjectInputStream objin=new ObjectInputStream(filein);
        person people=(person)objin.readObject();
        System.out.println("[+]调用people的say方法");
        people.say();


        System.out.println("________________java序列化-嵌套+自定义序列化方法________________");
        AppleTree appleTree=new AppleTree(5,"红苹果","苹果树");
        appleTree.say();

        System.out.println("[+]进行序列化");
        FileOutputStream fileout2=new FileOutputStream("AppleTree.ser");
        ObjectOutputStream objout2=new ObjectOutputStream(fileout2);
        objout2.writeObject(appleTree);
        objout2.close();

        System.out.println("[+]进行反序列化");
        FileInputStream filein2=new FileInputStream("AppleTree.ser");
        ObjectInputStream objin2=new ObjectInputStream(filein2);
        AppleTree tree=(AppleTree) objin2.readObject();
        System.out.println("[+]调用tree的say方法");
        tree.say();

    }
}

class person implements Serializable{
    private int age;
    private String name;

    public person(int age,String name){
        System.out.println("[+]person构造方法");
        this.age=age;
        this.name=name;
    }

    public void say(){
        System.out.println("    我是'"+this.name+"'今年"+this.age+"岁了");
    }
}




class AppleTree implements Serializable{
    private Apple apple;
    private String TreeName;

    public AppleTree(int num,String name,String TreeName){
        System.out.println("[+]AppleTree构造方法");
        this.TreeName=TreeName;
        this.apple=new Apple(num,name);
    }
    private void readObject (ObjectInputStream objin)throws Exception{
        System.out.println("    AppleTree的readObject处理");
        //objin.defaultReadObject();
        System.out.println("    AppleTree的readObject处理apple");
        this.apple=(Apple) objin.readObject();
        System.out.println("    AppleTree的readObject处理TreeName");
        this.TreeName=(String) objin.readObject();
    }
    private void writeObject(ObjectOutputStream objout)throws Exception{
        System.out.println("    AppleTree的writeObject");
        //objout.defaultWriteObject();
        System.out.println("    AppleTree的writeObject处理apple");
        objout.writeObject(this.apple);
        System.out.println("    AppleTree的writeObject处理TreeName");
        objout.writeObject(this.TreeName);
    }
    public void say(){
        System.out.println("    我是'"+this.TreeName);
        this.apple.say();
    }
}





class Apple implements Serializable{
    private int num;
    private String name;

    public Apple(int num,String name){
        System.out.println("[+]Apple构造方法");
        this.num=num;
        this.name=name;
    }
    private void readObject (ObjectInputStream objin)throws Exception{
        System.out.println("        Apple的readObject");
        //objin.defaultReadObject();
        System.out.println("        Apple的readObject处理name");
        this.name=(String) objin.readObject();
        System.out.println("        Apple的readObject处理num");
        this.num= (int) objin.readObject();
    }
    private void writeObject(ObjectOutputStream objout)throws Exception{
        System.out.println("        Apple的writeObject");
        //objout.defaultWriteObject();
        System.out.println("        Apple的writeObject处理name");
        objout.writeObject(this.name);
        System.out.println("        Apple的writeObject处理num");
        objout.writeObject(this.num);
    }
    public void say(){
        System.out.println("    我是'"+this.name+"'一共有"+this.num+"个");
    }
}

运行结果

image

image

运行结果

标签:AppleTree,name,demo,System,println,序列号,序列化,out
From: https://www.cnblogs.com/Aixve/p/18170340

相关文章

  • OceanBase单机版重新部署提示[ERROR] Deploy “demo“ is running. You could not dep
    执行介质里uninstall.sh脚本删除部署信息后重新安装Demo提示:[root@tidb01bin]#obddemo[ERROR]Deploy"demo"isrunning.Youcouldnotdeployanrunningcluster.Seehttps://www.oceanbase.com/product/ob-deployer/error-codes.TraceID:b18c41ba-07af-11ef-bd8f-......
  • php反序列化gc
    通过一道题来边看边讲php中的zval容器和gc回收机制ezpop<?phperror_reporting(0);highlight_file(__FILE__);classAAA{public$s;public$a;publicfunction__toString(){echo"youget2A<br>";$p=$this->a;r......
  • 对接银行支付,自己的demo可以调通,放到项目里,却总提示验签失败。原来竟是因为...
    原因是字符集(charset)不一致对接一个银行支付通道的支付API,自己java写的demo可以调通,放到项目工程里,部署到环境上,总是收到验签失败的响应。这个问题,困扰我们的开发大兄弟长达一个星期。对接通道接口联调不通,常见的场景有许多,如:签名原串需要对key进行排序。不同的排序算法会导......
  • Go语言实现多协程文件上传,断点续传--demo
    packagemainimport("fmt""io""os""regexp""strconv""sync""github.com/qianlnk/pgbar")/***需求:1.多协程下载文件2.断点续连**/funcmain(){//获取要下载文件DownloadFileName:=&quo......
  • WPF所有原生空间使用demo
    //前台窗体<Windowx:Class="WpfTestDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.......
  • 芯科SiWx917学习笔记:1-测试Out of Box Demo
    实验目的:测试OutofBoxDemo实验环境:SimplicityStudioV5实验器材:WirelessStarterKitMainboard(BRD4002ARevA06)+ SiWG917SingleBandWi-FiandBLE8MBFlashRadioBoard(BRD4338ARevA01)实验开始:1.新建工程:在demos中找到OutofBoxDemo(SoC)应用演示工程......
  • JSON 序列化 属性名 大写变成小写 保持不变 newsoft.json system.text.json
    JSON序列化属性名由大写变成小写的问题在ASP.NET中,默认情况下,JSON序列化会将属性名转换为小写(camelcase)以匹配JSON的约定。如果您希望保留C#的命名约定(即属性名的大小写不变),您需要更改默认的JSON序列化器。System.Text.Json使用System.Text.Json(推荐):在Startup.c......
  • Vue3 简单登录管理页面Demo
    目录前言项目基础配置新建项目引入组件项目配置Vue项目配置项目基本结构基础页面布局和路由搭建新增页面,简单跳转LoginViewMainViewrouterApp嵌套路由Test[1-4]Layout.vuerouter给个简单的跳转路由守护,重定向,动态路由,路由传值。这里不做展开描述简单登录页面:烂尾了总结前言这里......
  • 没有对应芯片手册,不知道哪些IO口可以控,测试demo
     //sdk\apps\earphone\include\app_config.h//////////↓↓↓↓↓↓↓↓↓↓codesnippetfromxwh↓↓↓↓↓↓↓↓↓↓////////////////////#defineLED0_IOIO_PORTA_01#defineLED0_ONOFF(x)do{gpio_set_pull_down(LED0_IO,0);\gpio_set......
  • WPF 使用 ManipulationDemo 工具辅助调试设备触摸失效问题
    本文将和大家介绍我所在的团队开源的ManipulationDemo工具。通过ManipulationDemo工具可以提升调试设备触摸失效的效率此工具在GitHub上完全开源,请看https://github.com/dotnet-campus/ManipulationDemo/软件界面效果大概如下可以显示接收到的Win32消息、当前的触摸......