public void save(){
String tmpfile = "/mnt/sdcard/1234.txt";
KeyFrameInfo flashMotionInfoCache = new KeyFrameInfo();
try {
File f = new File(tmpfile);
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(flashMotionInfoCache);
oos.flush();
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class KeyFrameInfo implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
public int bBlank;
public float aca;
}在oos.writeObject(flashMotionInfoCache);会抛异常,java.io.NotSerializableException:xxx
何解?androidSerializable异常java
String tmpfile = "/mnt/sdcard/1234.txt";
KeyFrameInfo flashMotionInfoCache = new KeyFrameInfo();
try {
File f = new File(tmpfile);
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(flashMotionInfoCache);
oos.flush();
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class KeyFrameInfo implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
public int bBlank;
public float aca;
}在oos.writeObject(flashMotionInfoCache);会抛异常,java.io.NotSerializableException:xxx
何解?androidSerializable异常java
发:
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
new ObjectOutputStream(output).writeObject(flashMotionInfoCache);
return output.toByteArray();
} catch(IOException e) {
return null;
}
收:
new ObjectInputStream(
new ByteArrayInputStream(上面返回的byte[])).readObject();
public static class KeyFrameInfo implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
public int bBlank;
public float aca;
}第二种方法就是把KeyFrameInfo单独作为一个类,不要写成内部类
oos.writeObject(flashMotionInfoCache);
ObjectInputStream 与 ObjectOutputStream 类所读写的对象必须实现了 Serializable 接口。
需要注意的是:对象中的 transient 和 static 类型的成员变量也不会被读取和写入 。只要让这个类 implements Serializable 即可
就相当于一个“通行证”一样,你这个类具备了这个Serializable,就可以被 ObjectInputStream 与 ObjectOutputStream 类操作了~~~~个人理解,讲得不好勿喷~~~