62,614
社区成员
发帖
与我相关
我的任务
分享
public class CStorable {
public void loadFromFile(RandomAccessFile raf) throws IOException{
Class clsThis=this.getClass();
Field afldThis[]=clsThis.getDeclaredFields();
Field.setAccessible(afldThis,true);
for (int i=0;i<afldThis.length;i++){
Class clsType=afldThis[i].getType();
try {
if (clsType==Long.TYPE){
afldThis[i].set(this,raf.readLong());
}else if (clsType==String.class){
int intLength=raf.readInt();
byte abytString[]=new byte[intLength];
raf.read(abytString);
afldThis[i].set(this,new String(abytString,"utf-8"));
}
}catch (IllegalArgumentException e){
}catch (IllegalAccessException e) {
}
}
}
public void saveToFile(RandomAccessFile raf) throws IOException{
Class clsThis=this.getClass();
Field afldThis[]=clsThis.getDeclaredFields();
Field.setAccessible(afldThis,true);
for (int i=0;i<afldThis.length;i++){
Class clsType=afldThis[i].getType();
try {
if (clsType==Long.TYPE){
raf.writeLong(afldThis[i].getLong(this));
}else if (clsType==String.class){
String strData=(String) afldThis[i].get(this);
byte abytString[]=strData.getBytes("utf-8");
int intLength=abytString.length;
raf.writeInt(intLength);
raf.write(abytString);
}
}catch (IllegalArgumentException e){
}catch (IllegalAccessException e) {
}
}
}
}
public class IllegalArgumentException
extends RuntimeException
抛出的异常表明向方法传递了一个不合法或不正确的参数。