将数组序列化写入文件后,为什么不能正确读出?
public static class AA implements Serializable {
int CRF[] = new int[200];
}
用AA内的数组作数据保存,写入文件后,为什么不能正确读出结果?有没有人遇到这样的问题?
------解决方案--------------------同意9楼
调用方法传递的只是一个引用,对对象引用参数的赋值没有意义
代码改如下
import java.io.FileInputStream;
import java.io.FileOutputStream;
import
java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class crtt {
static AA aa = new AA() ;
public static class AA implements Serializable {
int CRF[] = new int[200];
}
public static void wrf(String fpw) {
try {
FileOutputStream fos = new FileOutputStream(fpw);
ObjectOutputStream out = new ObjectOutputStream(fos);
//AA a = new AA();
out.writeObject(aa);
} catch (
IOException e) {
e.printStackTrace();
}
}
public static void rdf(String fpr) {
try {
FileInputStream fis = new FileInputStream(fpr);
ObjectInputStream in = new ObjectInputStream(fis);
aa= (AA) in.readObject();
} catch (IOException e) {
crtt.wrf(fpr);
} catch (
ClassNotFoundException e) {
crtt.wrf(fpr);
}
}
public static void main(String[] args) {
String fp1 = "CS.txt";
crtt.wrf(fp1);
System.out.println(aa.CRF[1]);
aa.CRF[1] = 1;
aa.CRF[2] = 1;
crtt.wrf(fp1);
crtt.rdf(fp1);
System.out.println(aa.CRF[1]);
System.out.println(aa.CRF[2]);
}
}
netbeans运行正确