Object serialization and serialization return

package com.mepu;

import org.junit.Test;

import java.io. * ;

/ ** 
 * @author : ACON
 * @date: 2020/1/1 14:29
 * @Description: ALL
 * @ModifiedBy:
 * @version: 1.0
 * Serialized object stream and deserialize
 */
public class ObjectIOTest {
    /*
    Serialization
     */
    @Test
    public void test1(){
        ObjectOutputStream the ObjectOutputStream = null ;
         the try {
             // create a byte stream objects 
            a FileOutputStream FileOutputStream = new new a FileOutputStream ( "object.dat" );
             // create the object stream 
            ObjectOutputStream = new new the ObjectOutputStream (FileOutputStream);
             // store object, the object may need to meet the sequence conditions, all used class implements serializable interface, can not be modified sequence of static variables
             // add properties serial version: public static final long serialVersionUID = custom value L; 
            ObjectOutputStream.writeObject ( new new String ( "Beijing Beijing" ));
            objectOutputStream.flush();//刷新
        } catch (IOException e) {
            e.printStackTrace ();
        } The finally {
             // close stream 
            the try {
                objectOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }

    /*
    Deserialization
     */
    @Test
    public  void test2 () {
        ObjectInputStream objectInputStream = null;
        O Object = null ;
         the try {
             // Create a file object 
            the FileInputStream FileInputStream = new new the FileInputStream ( "object.dat" );
             // create the object stream 
            ObjectInputStream = new new the ObjectInputStream (FileInputStream);
             // read 
            O = objectInputStream.readObject ();
            System.out.println(o);
        } catch (IOException e) {
            e.printStackTrace ();
        } catch (ClassNotFoundException e) {
            e.printStackTrace ();
        } The finally {
             // close stream 
            the try {
                objectInputStream.close();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }
}

Guess you like

Origin www.cnblogs.com/aikang525/p/12128975.html