开始有个Student的类
import java.util.*;
import java.io.*;
public class Job2Main {
static final String studentsFileName = "Student.txt";
/*
 * 将List中的Student保存到Student.txt文件中
 * 格式:
 * 一个Student一行,行的格式如下
 * 学号,姓名,年龄
 * Student.txt示例
 * 1,zhang3,20
 * 2,Li4,20
 * 3,Wang5,20
 */
void saveStudent(List<Student> students) throws Exception {
ObjectOutputStream fos=new ObjectOutputStream(new FileOutputStream(studentsFileName));
for(int i=0;i<students.size();i++){
Student s=students.get(i);
fos.writeObject(s);
}
fos.writeObject (null);
fos.close();
}
/*
 * 读取Student.txt文件中的学生数据,每一行创建一个Student的对象,
 * 多行数据便可以创建多个Student的对象,将这些对象保存到List中
 * Student.txt示例
 * 1,zhang3,20
 * 2,Li4,20
 * 3,Wang5,20
 */
List<Student> loadStudent() {
List<Student> stuList = new ArrayList<Student>();
studentsFileName.split(studentsFileName,stuList.size() );
int i1=Integer.parseInt(",");

 if(stuList.size()>0){        
 for (int i = 0; i < stuList.size(); i++) {             
 Student s = (Student) stuList.get(i);             
   System.out.println(s);        
 }        
 }      
 else{            
 System.out.println("没有任何学生信息!");
 }
return stuList;
} public static void main(String[] args) throws Exception {
Job2Main job2=new Job2Main();
Student s1=new Student(1, "zhang3", 20);
Student s2=new Student(2, "Li4", 20);
Student s3=new Student(3, "Wang5", 20);
List<Student> stuList=new ArrayList<Student>();
stuList.add(s1);
stuList.add(s2);
stuList.add(s3);
job2.saveStudent(stuList);
List<Student> students=job2.loadStudent();
for(Student s:students){
System.out.println(s);
}
}}

解决方案 »

  1.   

    菜鸟修改了小一下,楼主试试:
    代码用两种方式写入文件,读出显式.import java.util.*;
    import java.io.*;
    public class Job2Main
    {
    static final String studentsFileName = "Student.dat"; //以对象方式写入文件。
    static final String studentsTxtFileName = "Student.txt"; //文本方式写入文件。
    /*
     * 将List中的Student保存到Student.txt文件中
     * 格式:
     * 一个Student一行,行的格式如下
     * 学号,姓名,年龄
     * Student.txt示例
     * 1,zhang3,20
     * 2,Li4,20
     * 3,Wang5,20
     */
    //以对象方式写入文件
    //
    public void saveStudent(List<Student> students) throws Exception
            {
    ObjectOutputStream fos=new ObjectOutputStream(new FileOutputStream(studentsFileName));
    for(int i=0;i<students.size();i++)
    {
    Student s=students.get(i);
    fos.writeObject(s);
    }
    fos.writeObject (null);
    fos.close();
    }
    //以文本方式写入文件
    //
    public void saveStudentInTxt(List<Student> students) throws Exception
    {
    BufferedWriter bw =new BufferedWriter(new FileWriter(studentsTxtFileName));
    for(int i=0;i<students.size();i++)
    {
    Student s=students.get(i);
    bw.write(s+"\r\n");
    }
    bw.flush();
    bw.close();
    } /*
     * 读取Student.txt文件中的学生数据,每一行创建一个Student的对象,
     * 多行数据便可以创建多个Student的对象,将这些对象保存到List中
     * Student.txt示例
     * 1,zhang3,20
     * 2,Li4,20
     * 3,Wang5,20
     */
    //以对象方式读入
    //
    public List<Student> loadStudent(String file) throws Exception
    {
    List<Student> stuList = new ArrayList<Student>();
    ObjectInputStream ois =new ObjectInputStream(new FileInputStream(file));
    Student s;
    while((s=(Student)ois.readObject())!=null)
    {
    stuList.add(s);
    }
    ois.close();
    return stuList;
    } //从文本文件读入,并放入对象,存入List.
    //
    public List<Student> loadStudentFromTxtFile(String file) throws Exception
    {
    List<Student> list =new ArrayList<Student>();
    String temp=null; BufferedReader br =new BufferedReader(new FileReader(file));
    while((temp=br.readLine())!=null)
    {
    String[] input= temp.split(",");
    Student s=new Student();
    s.setId(Integer.parseInt(input[0]));
    s.setName(input[1]);
    s.setAge(Integer.parseInt(input[2]));
    list.add(s);
    }
    br.close();
    return list;
    } public static void main(String[] args) throws Exception
    {
    Job2Main job2=new Job2Main();
    Student s1=new Student(1, "zhang3", 20);
    Student s2=new Student(2, "Li4", 20);
    Student s3=new Student(3, "Wang5", 20);
    List<Student> stuList=new ArrayList<Student>();
    stuList.add(s1);
    stuList.add(s2);
    stuList.add(s3);
    job2.saveStudentInTxt(stuList); //以文本形式写入文件。
    job2.saveStudent(stuList); //以对象的方式写入文件(不是文本文件)。 //job2.saveStudent(stuList);
    //List<Student> students=job2.loadStudent();
    List<Student> students= job2.loadStudentFromTxtFile(studentsTxtFileName); //以文本方式读入。
    for(Student s:students)
    {
    System.out.println(s);
    }
    List<Student> students1= job2.loadStudent(studentsFileName); //以对象方式读入。
    for(Student s:students1)
    {
    System.out.println(s);
    }
    }
    }
    class Student implements Serializable
    {
    private int id;
    private String name;
    private int age;
    public Student()
    {
    }
    public Student(int id,String name,int age)
    {
    this.id = id;
    this.name = name;
    this.age = age;
    }
    public void setName(String name)
    {
    this.name = name;
    }
    public String getName()
    {
    return name;
    }
    public void setId(int id)
    {
    this.id = id;
    }
    public int getId()
    {
    return id;
    }
    public void setAge(int age)
    {
    this.age= age;
    }
    public int getAge()
    {
    return age;
    }
    public boolean equals(Object o)
    {
    if(this==o)
    {
    return true;
    }
    if(o==null)
    {
    return false;
    }
    if(o instanceof Student)
    {
    Student other=(Student)o;
    return this.id==other.getId() && this.name.equals(other.getName()) && this.age==other.getAge();
    }
    else
    {
    return false;
    }
    } public int hashCode()
    {
    return 13*id+17*age+name.hashCode();
    } public String toString()
    {
    return id+","+name+","+age;
    }
    }
      

  2.   

    大哥这个Student s=new Student();有问题啊,开始是个Student的构造方法