我现在先序列化一些对象到一个文件中,然后反序列化把他们都读出来,为什么会出现异常??
程序如下
/**
* @(#)TestObjectStream.java
*
*
* @author
* @version 1.00 2009/12/11
*/import java.io.Serializable;
import java.io.FileInputStream;
import java.io.*;
public class TestObjectStream { public static void main (String[] args) {
FileInputStream fis=null;
FileOutputStream fos=null;
ObjectInputStream ois=null;
ObjectOutputStream oos=null;
Person kelly=new Person();
kelly.age=(int)(10*Math.random());
kelly.name="xixo";
kelly.married=true;
try{
fos=new FileOutputStream("object.txt",true);
oos=new ObjectOutputStream(fos);
oos.writeObject(kelly);
fis=new FileInputStream("object.txt");
ois=new ObjectInputStream(fis);
Object o=null;
Person p=null;
while((o=ois.readObject())!=null){
p=(Person)o;
System.out.println ("name:"+p.name+"age:"+p.age+"married:"+p.married);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
ois.close();
oos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
class Person implements Serializable{
public int age;
public String name;
public boolean married;
}
程序如下
/**
* @(#)TestObjectStream.java
*
*
* @author
* @version 1.00 2009/12/11
*/import java.io.Serializable;
import java.io.FileInputStream;
import java.io.*;
public class TestObjectStream { public static void main (String[] args) {
FileInputStream fis=null;
FileOutputStream fos=null;
ObjectInputStream ois=null;
ObjectOutputStream oos=null;
Person kelly=new Person();
kelly.age=(int)(10*Math.random());
kelly.name="xixo";
kelly.married=true;
try{
fos=new FileOutputStream("object.txt",true);
oos=new ObjectOutputStream(fos);
oos.writeObject(kelly);
fis=new FileInputStream("object.txt");
ois=new ObjectInputStream(fis);
Object o=null;
Person p=null;
while((o=ois.readObject())!=null){
p=(Person)o;
System.out.println ("name:"+p.name+"age:"+p.age+"married:"+p.married);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
ois.close();
oos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
class Person implements Serializable{
public int age;
public String name;
public boolean married;
}
public int age;
public String name;
public boolean married; }
有Serializable接口啊,实现了阿
此种控制方式下readObject方法运行多次导致异常
直接调一遍无需控制
import java.io.Serializable;
import java.io.FileInputStream;
import java.io.*;public class Test { public static void main(String[] args) { FileInputStream fis = null;
FileOutputStream fos = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
Person kelly = new Person();
kelly.age = (int) (10 * Math.random());
kelly.name = "xixo";
kelly.married = true;
try {
fos = new FileOutputStream("object.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(kelly);
oos.writeObject(kelly);
oos.close(); fis = new FileInputStream("object.txt");
ois = new ObjectInputStream(fis);
Object o = null;
Person p = null;
int i = fis.available();
while (i>0) {
o = ois.readObject();
p = (Person) o;
System.out.println("name:" + p.name + "age:" + p.age
+ "married:" + p.married);
i = fis.available();
} } catch (Exception e) {
e.printStackTrace(); } finally {
try {
ois.close();
oos.close();
} catch (Exception e) {
e.printStackTrace(); }
}
}}class Person implements Serializable {
public int age;
public String name;
public boolean married;}
我想你如果在oos.writeObject(kelly); 后面再加一句oos.writeObject(null);就好了
* String path 序列化出的实体文件保存路径
* Object o 需要序列化的对象
* return void
*/
public static void writeEntity(String path, Object o){
FileOutputStream fileOut = null;
ObjectOutputStream objOut = null;
try {
fileOut = new FileOutputStream(path+"/DBObj.obj",true);
objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(o);
objOut.flush();
objOut.close();
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* String path 序列化出的实体文件路径
* return Object
*/
public static Object readEntity(String path){
Object o = null;
try {
FileInputStream fileIn = new FileInputStream(path);
ObjectInputStream objIn = new ObjectInputStream(fileIn);
o = objIn.readObject();
objIn.close();
fileIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return o;
}
}
只要你传入的Object实现了Serializable接口就行了,不过要注意的就是版本问题
java序列化存在一个版本问题,就是说你要序列化一个javaBean名叫A,序列化前后A的版本必须一致,不能在类中修改任何东西,否则是不能正确反序列化的。
while(fis.available()!=0){
p=(Person)(ois.readObject());
System.out.println ("name:"+p.name+"age:"+p.age+"married:"+p.married);
}