import javax.swing.*; 
import java.util.*;
import java.lang.String.*;
import java.awt.event.*;
import java.awt.*;
import java.lang.*;
import java.io.*;class Student implements Serializable 
{
String name;
String sex;
String date;
    String grade;
    String college;
    String cLass;
    String classDuty;
    String sextion;
    String sextionDuty;
    String address;
    String telephone;
    public Student(String name,String sex,String date,String grade,String college,
                     String cLass,String classDuty,String sextion,String sextionDuty,
                     String address,String telephone)
    {
     this.name=name;
     this.sex=sex;
     this.date=date;
     this.grade=grade;
     this.college=college;
     this.cLass=cLass;
     this.classDuty=classDuty;
     this.sextion=sextion;
     this.sextionDuty=sextionDuty;
     this.address=address;
     this.telephone=telephone;
    }
}
class FileOperate 
{
public void write(Student stu)
    {   
    try
        {  
           ObjectOutputStream data=new ObjectOutputStream(new FileOutputStream("bb.txt",true));
           data.writeObject(stu);
        }
        catch(Exception event)
        {  
            System.out.println("error for write!");
        }
    }
    public void read()
    {
     try
        {  
          ObjectInputStream out=new ObjectInputStream(new FileInputStream("bb.txt"));
              Student stu2=(Student)out.readObject();
              System.out.println(stu2.name+stu2.address+stu2.sex+stu2.date);        }
        catch(Exception event)
        {  
            System.out.println("error for read!");
        }    
    }
}
public class bb 
{
public static void main(String args[])
{   
    Student s1=new Student("邬志刚","1","1","1","1","1","1","1","1","1","1");
    Student s2=new Student("时磊","2","2","2","2","2","2","2","2","2","2");
    Student s3=new Student("李广强","3","3","3","3","3","3","3","3","3","3");
    Student s4=new Student("蒋雨辰","4","4","4","4","4","4","4","4","4","4");
        FileOperate file=new FileOperate();
        file.write(s1);
        file.write(s2);
        file.write(s3);
        file.write(s4);
        for(int i=1;i<=4;i++)
        {
         file.read();
        }
}
}
写文件是把4个对象成功写进去了
问题是读的时候 读的总是第一个对象,
怎么才能把所有对象都读出来
谢!

解决方案 »

  1.   

    package csdn.basic;import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;class Student implements Serializable {
    String name; String sex; String date; String grade; String college; String cLass; String classDuty; String sextion; String sextionDuty; String address; String telephone; public Student(String name, String sex, String date, String grade,
    String college, String cLass, String classDuty, String sextion,
    String sextionDuty, String address, String telephone) {
    this.name = name;
    this.sex = sex;
    this.date = date;
    this.grade = grade;
    this.college = college;
    this.cLass = cLass;
    this.classDuty = classDuty;
    this.sextion = sextion;
    this.sextionDuty = sextionDuty;
    this.address = address;
    this.telephone = telephone;
    }
    }class FileOperate {
    public FileOutputStream fileOut = null ;
    public FileInputStream fileIn = null ;
    public FileOperate() throws Exception{
     fileOut = new FileOutputStream("bb.txt", true) ;
     fileIn = new FileInputStream("bb.txt") ;
    }

    public void write(Student stu) {
    try {
    ObjectOutputStream data = new ObjectOutputStream(
    fileOut);
    data.writeObject(stu);
    } catch (Exception event) {
    System.out.println("error for write!");
    }
    } public void read() {
    try {
    ObjectInputStream out = new ObjectInputStream(fileIn);
    Student stu2 = (Student) out.readObject();
    System.out.println(stu2.name + stu2.address + stu2.sex + stu2.date); } catch (Exception event) {
    System.out.println("error for read!");
    }
    }
    }public class BB {
    public static void main(String args[]) throws Exception{
    Student s1 = new Student("邬志刚", "1", "1", "1", "1", "1", "1", "1", "1",
    "1", "1");
    Student s2 = new Student("时磊", "2", "2", "2", "2", "2", "2", "2", "2",
    "2", "2");
    Student s3 = new Student("李广强", "3", "3", "3", "3", "3", "3", "3", "3",
    "3", "3");
    Student s4 = new Student("蒋雨辰", "4", "4", "4", "4", "4", "4", "4", "4",
    "4", "4");
    FileOperate file = new FileOperate();
    file.write(s1);
    file.write(s2);
    file.write(s3);
    file.write(s4);
    for (int i = 1; i <= 4; i++) {
    file.read();
    }
    }
    }你每次独得时候都是重新开一个流去读数据 肯定会每次只读 到第一个改了一下 ok!
    ////////////////
    帮你改了一下