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);
        }
    }
  • 相关阅读:
    jq 字符串去除空格
    wpf 加载资源文件
    wpf 寻找TreeView的子元素,并对其进行操作
    IIS发布MVC ASP.NET网站
    wpf Binding 小记录
    asp.net mvc表单异步提交
    把路径设置为全局变量
    MVC将服务器端的物理路径转换为服务器路径
    silverlight控件阴影效果示例
    NLP的12条前提假设
  • 原文地址:https://www.cnblogs.com/tszr/p/10967108.html
Copyright © 2011-2022 走看看