关于java序列化多个不同类型对象的存取与读取?万分感谢
publicclassSerializableTestListError{staticList<Student>listStudentObject=newArrayLis...
public class SerializableTestListError { static List<Student> listStudentObject=new ArrayList<Student>();
static List<Car> listCarObject=new ArrayList<Car>();
static List<Object> listObject=new ArrayList<Object>();
public static void main(String[] args) throws IOException {
Student stu1=new Student(001, "yteng1", 20);
Student stu2=new Student(002,"yteng2",21);
Car car1=new Car("DazsAuto", "ShangHai", 200000.0);
Car car2=new Car("Ford","JiangSu",30000.00);
SerializableTestListError serializableTestInstance= new SerializableTestListError();
listObject.add(car2);
listObject.add(stu1);
writeObject2File(listObject,"D:\\date1.txt");
try {
readObjectFromFile("D:\\date1.txt");
} catch (SerilizableException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
for(Iterator<Car> iterCar=listCarObject.iterator();iterCar.hasNext();){
System.out.println(iterCar.next());
}
for(Iterator<Student> iterStudent=listStudentObject.iterator();iterStudent.hasNext();){
System.out.println(iterStudent.next()) }}
public static void writeObject2File(List<Object>o,String fileName) throws IOException{
FileOutputStream fout=new FileOutputStream(fileName,true);
ObjectOutputStream sout=new ObjectOutputStream(fout);
for(int i=0;i<o.size();i++){
sout.writeObject(o.get(i));
}
System.out.println("写入对象成功!");
}
public static void readObjectFromFile(String fileName) throws Exception{
FileInputStream fin=new FileInputStream(fileName);;
BufferedInputStream bis=new BufferedInputStream(fin);
ObjectInputStream oip=null;
while(true){
try{
oip=new ObjectInputStream(bis); //每次重新构造对象输入流
}catch(EOFException e)
{
System.out.println("已达文件末尾");//如果到达文件末尾,则退出循环
break;
}
Object object=new Object();
object=oip.readObject();
if(object instanceof Student) { //判断对象类型
listStudentObject.add((Student)object);
}
else if(object instanceof Car){
listCarObject.add((Car)object); } }
}}错误如下: 跪求啊
java.io.StreamCorruptedException: invalid stream header: 7371007E
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at travelsky.eb.yteng.test.SerializableTestListError.readObjectFromFile(SerializableTestListError.java:78)
at travelsky.eb.yteng.test.SerializableTestListError.main(SerializableTestListError.java:43)
Student [id=1, name=yteng1, age=20] 展开
static List<Car> listCarObject=new ArrayList<Car>();
static List<Object> listObject=new ArrayList<Object>();
public static void main(String[] args) throws IOException {
Student stu1=new Student(001, "yteng1", 20);
Student stu2=new Student(002,"yteng2",21);
Car car1=new Car("DazsAuto", "ShangHai", 200000.0);
Car car2=new Car("Ford","JiangSu",30000.00);
SerializableTestListError serializableTestInstance= new SerializableTestListError();
listObject.add(car2);
listObject.add(stu1);
writeObject2File(listObject,"D:\\date1.txt");
try {
readObjectFromFile("D:\\date1.txt");
} catch (SerilizableException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
for(Iterator<Car> iterCar=listCarObject.iterator();iterCar.hasNext();){
System.out.println(iterCar.next());
}
for(Iterator<Student> iterStudent=listStudentObject.iterator();iterStudent.hasNext();){
System.out.println(iterStudent.next()) }}
public static void writeObject2File(List<Object>o,String fileName) throws IOException{
FileOutputStream fout=new FileOutputStream(fileName,true);
ObjectOutputStream sout=new ObjectOutputStream(fout);
for(int i=0;i<o.size();i++){
sout.writeObject(o.get(i));
}
System.out.println("写入对象成功!");
}
public static void readObjectFromFile(String fileName) throws Exception{
FileInputStream fin=new FileInputStream(fileName);;
BufferedInputStream bis=new BufferedInputStream(fin);
ObjectInputStream oip=null;
while(true){
try{
oip=new ObjectInputStream(bis); //每次重新构造对象输入流
}catch(EOFException e)
{
System.out.println("已达文件末尾");//如果到达文件末尾,则退出循环
break;
}
Object object=new Object();
object=oip.readObject();
if(object instanceof Student) { //判断对象类型
listStudentObject.add((Student)object);
}
else if(object instanceof Car){
listCarObject.add((Car)object); } }
}}错误如下: 跪求啊
java.io.StreamCorruptedException: invalid stream header: 7371007E
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at travelsky.eb.yteng.test.SerializableTestListError.readObjectFromFile(SerializableTestListError.java:78)
at travelsky.eb.yteng.test.SerializableTestListError.main(SerializableTestListError.java:43)
Student [id=1, name=yteng1, age=20] 展开
展开全部
jgjhgkjhlkhlkh
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询