Serializable

    1. import java.io.*;
    2.   
    3. public class  Box implements Serializable  
    4. {  
    5.     private int width;  
    6.     private int height;  
    7.   
    8.     public void setWidth(int width){  
    9.         this.width  = width;  
    10.     }  
    11.     public void setHeight(int height){  
    12.         this.height = height;  
    13.     }  
    14.   
    15.     public static void main(String[] args){  
    16.         Box myBox = new Box();  
    17.         myBox.setWidth(50);  
    18.         myBox.setHeight(30);  
    19.   
    20.         try{  
    21.             FileOutputStream fs = new FileOutputStream("foo.ser");  
    22.             ObjectOutputStream os =  new ObjectOutputStream(fs);  
    23.             os.writeObject(myBox);  
    24.             os.close();  
    25.         }catch(Exception ex){  
    26.             ex.printStackTrace();  
    27.         }  
    28.     }  
    29.       
    30. }  
原文地址:https://www.cnblogs.com/myhome-1/p/5763681.html