java序列化与反序列化

1.概念:序列化的意义是把一个实例对象进行即时冻结,保存即时数据;反序列化即是对冻结的数据进行解冻还原;或者可以说是数据格式的转化与复原。

2.需注意的几个问题:

       1):所有要序列化的对象必须实现Serializable,并且对

                private static final long serialVersionUID = -2185617442774674835L;

                虚拟机是否允许反序列化,不仅取决于类路径和功能代码是否一致,一个非常重要的一点是两个类的序列化 ID 是否一致

       2):transient和static修饰的字段默认都不会序列化,还有父类字段但父类没有实现Serializable的也不会序列化

3.对某些字段如果在传输过程中需要处理,则可重写writeObject和readObject

      ATTENTION:writeObject and readObject must decorated by private

/**

* Created by dyf on 2019/5/22 9:35

* ATTENTION:writeObject and readObject must decorated by private

* 重写时,不处理的字段如果要序列化和反序列化也要put或read

* transient和static修饰的都不会序列化,或者父类字段但父类没有实现Serializable的也不会序列化

*/

public class Userimplements Serializable{

private static final long serialVersionUID = -6375006318163451630L;

    private StringuserName;

    private StringpassWord;

    public User() {

}

public StringgetUserName() {

return userName;

    }

public void setUserName(String userName) {

this.userName = userName;

    }

public StringgetPassWord() {

return passWord;

    }

public void setPassWord(String passWord) {

this.passWord = passWord;

    }

private void writeObject(ObjectOutputStream out){

try {

ObjectOutputStream.PutField putField = out.putFields();

            System.out.println("原密码:"+passWord);

            passWord="encryption";

            putField.put("passWord",passWord);

            putField.put("userName",userName);

            System.out.println("加密后的密码:"+passWord);

            out.writeFields();

        }catch (IOException e) {

e.printStackTrace();

        }

}

private void readObject(ObjectInputStream in)throws IOException, ClassNotFoundException {

ObjectInputStream.GetField getField = in.readFields();

        Object password = getField.get("passWord", "");

        Object username = getField.get("userName", "");

        System.out.println("要解密的字符串:"+password.toString());

        passWord="pass";

        userName=username.toString();

    }

@Override

    public StringtoString() {

return "User{" +

"userName='" +userName +'\'' +

", passWord='" +passWord +'\'' +

'}';

    }

}

public class FieldsProcess {

public static void main(String[] args)throws IOException, ClassNotFoundException {

write();

      read();

    }

public static void write()throws IOException {

User user =new User();

        user.setUserName("lining");

        user.setPassWord("pass");

        ObjectOutputStream objectOutputStream =new ObjectOutputStream(new FileOutputStream("d://test/4.obj"));

        objectOutputStream.writeObject(user);

    }

public static void read()throws IOException, ClassNotFoundException {

ObjectInputStream objectInputStream =new ObjectInputStream(new FileInputStream("d://test/4.obj"));

        User user = (User) objectInputStream.readObject();

        System.out.println(user.toString());

    }

}

运行结果:

原密码:pass

加密后的密码:encryption

要解密的字符串:encryption

User{userName='lining', passWord='pass'}

4.多个对象写入和读取:

      每次写入(同一个out对象write)都会清除上一次的文件内容,重新写入;一次可以写入多个对象,如果是不同类型的实例,读取时也要按顺序读取,否则报强转错误;如果读取的次数超过写入的次数,则报EOFEXCEPTION;注意:不同对象要按顺序,读写次数一致

你可能感兴趣的:(java序列化与反序列化)