java 对象序列化怎么保存对象呀
import *;class ObjectStream implements Serializable{
//保存对象
public static void saveObject(String fileName,Object obj){
try{
FileOutputStream fi=new FileOutputStream(fileName+".txt");
ObjectOutputStream os=new ObjectOutputStream(fi);
os.writeObject(obj);
os.flush();
}catch(Exception e){System.out.println("savaObject Error");}
}
//提取对象
public static Object openObject(String fileName){
ObjectInputStream oiT=null;
try{
FileInputStream fs=new FileInputStream(fileName+".txt");
ObjectInputStream oi=new ObjectInputStream(fs);
return (Object)oi.readObject();
}catch(Exception e){System.out.println("OpenObject Error");}
System.out.println("Error");
return oiT;
}
}
class TestObject{
static int i=1;
public TestObject(){
System.out.println("OK");
}
public static void main(String args[]){
ObjectStream.saveObject("testObject",new TestObject());
System.out.println(ObjectStream.openObject("testObject").toString());
}
}
为什么保存对象时总是扔出异常呀。
谁能告诉我ObjectInputStream readObject()具体使用方法呀?