Java序列化与反序列化

来源:互联网 发布:南阳师范学院教务网络 编辑:程序博客网 时间:2024/05/01 12:28

java 中的序列化与反序列化代码:

//进行序列化
 public byte[] getByteArray(String[] str)
   {
     byte[] bt = (byte[])null;
     try {
       if (str != null) {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         ObjectOutputStream oos = new ObjectOutputStream(bos);
         oos.writeObject(str);
         bt = bos.toByteArray();
       }
     } catch (Exception ex) {
       bt = (byte[])null;
       ex.printStackTrace();
     }   
     return bt;
   }
  
 //进行反序列化
 public String[] getArrayList(byte[] bt)
   {
     String[] Array;
     ObjectInputStream objIps;
     try
     {
       objIps = new ObjectInputStream(
         new ByteArrayInputStream(bt));
       Array = (String[])objIps.readObject();
      
     } catch (Exception ex) {
       ex.printStackTrace();
       return null;
     }
     return Array;
   }

原创粉丝点击