关于java 这个程序中的writeObject()和readObject()方法怎么调用的!
郁闷就是看不明白UsrTester类中的writeObject()和readObject()怎么起作用的!!packageiolc;importjava.io.*;pub...
郁闷就是看不明白UsrTester类中的writeObject()和readObject()怎么起作用的!!
package iolc;
import java.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<buff.length;i++){
int b=0;
for(int j=0;j<8;j++){
int bit=(buff[i]>>j&1)==0?1:0;
b+=(1<<j)*bit;
}
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);
}
} 展开
package iolc;
import java.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<buff.length;i++){
int b=0;
for(int j=0;j<8;j++){
int bit=(buff[i]>>j&1)==0?1:0;
b+=(1<<j)*bit;
}
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);
}
} 展开
2个回答
2014-05-02
展开全部
自己加个断点跟进去就可以看到,在ObjectStreamClass中,会有一步操作
writeObjectMethod.invoke(obj, new Object[]{ out });是尝试着去找传入对象中的WriteObject(ObjectOutputStream out)方法的。
writeObjectMethod.invoke(obj, new Object[]{ out });是尝试着去找传入对象中的WriteObject(ObjectOutputStream out)方法的。
追问
没明白 ,大侠能说的白话点吗,新手看不懂啊
追答
ByteArrayOutputStream buf=new ByteArrayOutputStream();
ObjectOutputStream o=new ObjectOutputStream(buf);
o.writeObject(userter);
这个最后的o.writeObject(userter);在实现的过程中,会通过反射在userter中寻找方法名为writeObject,参数为ObjectOutputStream的方法,如果找到了就会调用userter.writeObject(o)的方法;没找到的话会使用默认的实现,这种情况下你的这个类中的password字段由于是transient的就会丢失掉。
反过来读取对象的时候也是这样的。
你在main方法里下个断点,然后使用debug as方式运行,到o.writeObject(userter)这个方法的时候追进去,然后见有方法调用就往进跟,最后可以看到这个调用的地方。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询