zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然 JAVA开发学习:序列化

    public final void writeObject(Object x) throws IOException
    public final Object readObject() throws IOException, 
                                     ClassNotFoundException
    public class Employee implements java.io.Serializable
    {
       public String name;
       public String address;
       public transient int SSN;
       public int number;
       public void mailCheck()
       {
          System.out.println("Mailing a check to " + name
                               + " " + address);
       }
    }
    import java.io.*;
     
    public class SerializeDemo
    {
       public static void main(String [] args)
       {
          Employee e = new Employee();
          e.name = "Reyan Ali";
          e.address = "Phokka Kuan, Ambehta Peer";
          e.SSN = 11122333;
          e.number = 101;
          try
          {
             FileOutputStream fileOut =
             new FileOutputStream("/tmp/employee.ser");
             ObjectOutputStream out = new ObjectOutputStream(fileOut);
             out.writeObject(e);
             out.close();
             fileOut.close();
             System.out.printf("Serialized data is saved in /tmp/employee.ser");
          }catch(IOException i)
          {
              i.printStackTrace();
          }
       }
    }
    import java.io.*;
     
    public class DeserializeDemo
    {
       public static void main(String [] args)
       {
          Employee e = null;
          try
          {
             FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn);
             e = (Employee) in.readObject();
             in.close();
             fileIn.close();
          }catch(IOException i)
          {
             i.printStackTrace();
             return;
          }catch(ClassNotFoundException c)
          {
             System.out.println("Employee class not found");
             c.printStackTrace();
             return;
          }
          System.out.println("Deserialized Employee...");
          System.out.println("Name: " + e.name);
          System.out.println("Address: " + e.address);
          System.out.println("SSN: " + e.SSN);
          System.out.println("Number: " + e.number);
        }
    }
  • 相关阅读:
    Java反射机制
    dd命令
    分区工具fdisk,gdisk,parted
    硬盘初识
    shell脚本之算术运算和逻辑运算
    linux防火墙简单的使用
    压缩解压打包工具基础
    find命令基础讲解
    个人数据备份方案
    数据库的表名字段名大小写问题
  • 原文地址:https://www.cnblogs.com/tszr/p/10967108.html
Copyright © 2011-2022 走看看