我需要序列化一个GeoPoints列表,然后将它们读回来。我搜索了网页,发现我可以实现自己的GeoPoint并实现可序列化。它的工作原理是,我可以将对象写入一个.ser文件,但是当我读回来时,我得到了一个invalidclassexception,它继续说详细信息 - 对原始GeoPoint对象的illegalaccessexception。我假设我没有正确地在MyGeoPoint类中扩展GeoPoint。有人可以看看并告诉我我犯了什么错误吗?扩展可序列化的geopoint - 无法读取对象在
import com.google.android.maps.GeoPoint;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class MyGeoPoint extends GeoPoint implements Serializable {
static final long serialVersionUID = -3010695769693014199L;
public MyGeoPoint(int latitude, int longitude){
super(latitude, longitude);
}
public MyGeoPoint(){
this(0,0);
}
private void writeObject(ObjectOutputStream s) throws IOException{
s.defaultWriteObject();
}
private void readObject(ObjectInputStream s)
throws java.io.IOException, java.lang.ClassNotFoundException{
s.defaultReadObject();
}
}
,然后它被使用像
File filex = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +"floodData.ser");
MyGeoPoint aPoint = new MyGeoPoint();
MyGeoPoint readPoint = null;
//write
try{
FileOutputStream f = new FileOutputStream(filex);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(aPoint);
s.close();
} catch (IOException e) {
e.printStackTrace();
}
//read
try {
FileInputStream ff = new FileInputStream(filex);
ObjectInputStream ss = new ObjectInputStream(ff);
readPoint = (MyGeoPoint)ss.readObject();
ss.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
//end serialisationtest
ü也应该关闭FileOutputStream对象 - 序列化部分 – xyz 2012-01-11 10:15:42
@xyz后F:自动关闭外部流关闭内部流(请参阅[这里](http://java.sun.com/developer/technicalArticles/Streams/ProgIOStreams/))。但'close()'应该在'finally'块中调用。 – Kai 2012-01-11 10:40:54