Java_Serializable

1.  Today `s  Famous quotes

Nothing is impossible as long as it is possible



Serializable :

1.Its a varification code between two files

2.Its keep your file guaranteed security(安全性), and its very defalut for eclipse and  Java,its must be.

3.If you didnt set SerialSVersionUID and the jvm will set the Defalut UID for you .

    There are two king of SerialVersionUID here , first is defalut UID  and notdefalut UID.

    Example :

                            //Serializable    Example one

FileOutputStream fos = new FileOutputStream(file);  
   ObjectOutputStream oos = new ObjectOutputStream(fos);  
   oos.writeObject(st);  
   oos.flush();  
   oos.close();  
   fos.close();  
                          

                          //Seralizbale        Example two

                            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("my.out"));        

                            MyTest myTest=new MyTest();
                            oos.writeObject(myTest);
                            oos.flush();  //BufferedIO
                            oos.close(); // close
                            private static final long serialVersionUID = 1L;   and

                            private static final long serialVersionUID = -1172613597422125119L;



                            //Deserializable  

FileInputStream fis = new FileInputStream(file);  
   ObjectInputStream ois = new ObjectInputStream(fis);  
   Student st1 = (Student) ois.readObject();  
   System.out.println("name = " + st1.getName());  
   System.out.println("sex = " + st1.getSex());  
   System.out.println("year = " + st1.getYear());  
   System.out.println("gpa = " + st1.getGpa());  
   ois.close();  
   fis.close();  


    4. There is another interface for Serializable , Externalizable

Example :    

                        

                    void writeExternal(ObjectOutput out) throws IOException;

                         void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;

                            

                         public interface Externalizable extends java.io.Serializable {

                         /**
                        * The object implements the writeExternal method to save its contents
                        * by calling the methods of DataOutput for its primitive values or  }
                        

    Note : 1.When subclass implement serializable , and  supclass shouldnt implement Serializable.

                2。static , transient cant  implements Serializable interface.

    Serializable is very simple that you just implements Serializable.that is OK! Good luck!

Iparhan_Java  2016-6-22



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值