首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

判断一个种是否为Serializable zz

2012-12-27 
判断一个类是否为Serializablezz转自:http://www.tutorialspoint.com/java/java_serialization.htm用下面

判断一个类是否为Serializable zz
转自:http://www.tutorialspoint.com/java/java_serialization.htm
用下面的第一个类Serialize一下,再用第二个类 De-Serialize一下.  如果对象的值相同就行了



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("employee.ser");         ObjectOutputStream out =                            new ObjectOutputStream(fileOut);         out.writeObject(e);         out.close();          fileOut.close();      }catch(IOException i)      {          i.printStackTrace();      }   }}



Deserializing an Object:

The following DeserializeDemo program deserializes the Employee object created in the SerializeDemo program. Study the program and try to determine its output:

import java.io.*;   public class DeserializeDemo   {      public static void main(String [] args)      {         Employee e = null;         try         {            FileInputStream fileIn =                          new FileInputStream("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);    }}

热点排行