黑马程序员技术交流社区
标题:
对象序列化的问题
[打印本页]
作者:
丁岩
时间:
2012-11-3 12:36
标题:
对象序列化的问题
为什么对象序列化方法中的readOject方法只能读取最新的那个writeObject呢,意思是假如我用writeObject存储多个对象到指定的路径下,当我调用readObject方法时候却仅仅只能读取最后的一个writeObject存储对象,前面都被覆盖了,这样很不方便啊,有没有什么解决方法呢(举例说是假如我用writeObject分别存储了Student stu1 、stu2、stu3,但是存储完毕后我调用readObject仅仅只能显示stu3的信息),后来我用list集合的形式,虽然能将这三个对象都显示出来,但是啊又觉得对象序列化是不是又有点多余呢。。。我等于又属于兜圈子了,下面给大家看看我的代码
package TestIO;
import java.io.Serializable;
class Student implements Serializable
{
private String name;
private int age;
private String pro;
private float score;
public Student(String name, int age, String pro, float score) {
super();
this.name = name;
this.age = age;
this.pro = pro;
this.score = score;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getPro() {
return pro;
}
public float getScore() {
return score;
}
}
package TestIO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
class RW
{
public ArrayList<Student> al=new ArrayList<Student>();
void Rw()
{
}
public void writeIO() throws FileNotFoundException, IOException
{
System.out.println("请依次输入学生的姓名、年龄、课程、分数:");
Scanner in=new Scanner(System.in);
String name=in.next();
int age=in.nextInt();
String pro=in.next();
float score=in.nextFloat();
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\Student.txt"));
Student stu=new Student(name,age,pro,score);
oos.writeObject(stu);
oos.close();
System.out.println("存储成功");
}
public void readIO() throws FileNotFoundException, IOException, ClassNotFoundException
{
int i=1;
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("C:\\Student.txt"));
Student stu=(Student)ois.readObject();
al.add(stu);
ois.close();
}
public void Out()
{
while(!al.isEmpty())
{
int i=0;
Student stu=al.get(0);
System.out.println("name="+stu.getName()+"\t"+"age="+stu.getAge()+"\t"+"pro="+stu.getPro()+"\t"+"score="+stu.getScore());
al.remove(i);
i++;
}
}
}
package TestIO;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Seriz
{
public static void main(String[]args) throws FileNotFoundException, IOException, ClassNotFoundException
{
RW rw=new RW();
for(int i=0;i<3;i++)
{
rw.writeIO();
rw.readIO();
}
rw.Out();
}
}
作者:
刘士林
时间:
2012-11-3 14:37
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Test {
public static void main(String[] args)throws Exception
{
writeObj();
readObj();
}
public static void writeObj()throws IOException
{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("abc.txt"));
oos.writeObject(new Person("lili",23,"cn"));
oos.writeObject(new Person("lisi",27,"cn"));
oos.writeObject(new Person("liwu",25,"cn"));
oos.close();
}
public static void readObj()throws Exception
{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("abc.object"));
Person p1 = (Person)ois.readObject();
Person p2 = (Person)ois.readObject();
Person p3 = (Person)ois.readObject();
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
ois.close();
}
}
class Person implements Serializable
{
String name;
int age;
static String country;
Person(String name,int age,String country)
{
this.name=name;
this.age=age;
this.country=country;
}
public String toString()
{
return(name+".."+age+".."+country);
}
}
打印结果:
lili..23..cn
lisi..27..cn
liwu..25..cn
用这个程序就能回答你的疑问,三个对象全写进去了,也都能读出来。因为用writeObject方法往文件里写对象,不会覆盖原有的对象,
而用readObject方法读取时,读完一个它会将其返回,然后再调用它,它就自动读取下一个,因为它在对象后做了标记。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2