郁闷就是看不明白UsrTester类中的writeObject()和readObject()怎么起作用的!!packageiolc;importjava.io.*;publicclassUserTesterimplementsSerializable{privatestaticfinallongse...
郁闷就是看不明白UsrTester类中的writeObject()和readObject()怎么起作用的!!
package iolc;
importjava.io.*;
public class UserTester implements Serializable {
private static final long serialVersionUID = -5482654093968922731L;
private String name;
private transient String password;
public UserTester(String name,String password){
this.name=name;
this.password=password;
}
public String toString(){
return name+" "+password;
}
private byte[] change(byte[] buff){
for(int i=0;i
int b=0;
for(int j=0;j<8;j++){
int bit=(buff[i]>>j&1)==0?1:0;
b+=(1<
}
buff[i]=(byte)b;
}
return buff;
}
private void writeObject(ObjectOutputStream stream)throws IOException{
stream.defaultWriteObject();
stream.writeObject(change(password.getBytes()));
}
private void readObject(ObjectInputStream stream)throws IOException,ClassNotFoundException{
stream.defaultReadObject();
byte[] buff=(byte[])stream.readObject();
password=new String(change(buff));
}
public static void main(String args[])throws Exception{
UserTester userter=new UserTester("tom","123455789456");
System.out.println("Before Serialization:"+userter);
ByteArrayOutputStream buf=new ByteArrayOutputStream();
ObjectOutputStream o=new ObjectOutputStream(buf);
o.writeObject(userter);
ObjectInputStream in=new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));
userter=(UserTester)in.readObject();
System.out.println("After Serialization:"+ userter);
}
}
展开