把十个学生信息(假设每个信息都有三个属性)写入文件 可以用循环吗?怎么用

解决方案 »

  1.   

    //*******下面是学生类********
    import java.io.Serializable;public class Student implements Serializable {//被序列化的类必须扩展Serializable接口,并且得有一个ID。 private static final long serialVersionUID = 1L;//上面所说的ID
    private String name;
    private int age;
    private String sex; public Student(String name,int age,String sex)//构造函数
    {
    this.name = name;
    this.age = age;
    this.sex = sex;
    } //以下为各属性的setter和getter方法
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getSex() {
    return sex;
    }
    public void setSex(String sex) {
    this.sex = sex;
    }
    }//*********下面是序列化的代码*******
    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    import java.io.FileNotFoundException;
    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
    import java.io.IOException;public class Test { public static void main(String[] args)
    {
    Student[] student ={new Student("student1",22,"男"),
    new Student("student2",21,"女"),
    new Student("student3",20,"男"),
    new Student("student4",19,"女"),
    new Student("student5",18,"男"),
    new Student("student6",17,"男"),
    new Student("student7",22,"女"),
    new Student("student8",22,"女"),
    new Student("student9",22,"女"),
    new Student("student10",22,"男"),};

    try
    {
    //写入文件
    ObjectOutputStream oos = new ObjectOutputStream(
    new FileOutputStream("haoguicai000.txt"));
    oos.writeObject(student);//这里存的是数组对象,你也可以用循环把每一个Student对象写进去。

    //从文件中读出对象
    Student[] students2;
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("haoguicai000.txt"));
    students2 = (Student[])ois.readObject();

    for(Student s :students2)
    {
    System.out.println("姓名:"+s.getName());
    System.out.println("年龄:"+s.getAge());
    System.out.println("性别"+s.getSex());
    }
    }
    catch(FileNotFoundException ex)
    {
    ex.printStackTrace();
    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }
    catch(ClassNotFoundException ex)
    {
    ex.printStackTrace();
    }
    }
    }
      

  2.   

    哦,写入文件用循环啊?得改一下。学生类代码不变,序列化代码改为:import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    import java.io.FileNotFoundException;
    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
    import java.io.IOException;public class Test { public static void main(String[] args)
    {
    Student[] students ={new Student("student1",22,"男"),
    new Student("student2",21,"女"),
    new Student("student3",20,"男"),
    new Student("student4",19,"女"),
    new Student("student5",18,"男"),
    new Student("student6",17,"男"),
    new Student("student7",22,"女"),
    new Student("student8",22,"女"),
    new Student("student9",22,"女"),
    new Student("student10",22,"男"),};
    try
    {
    //写入文件
    ObjectOutputStream oos = new ObjectOutputStream(
    new FileOutputStream("haoguicai000.txt"));
    for(Student student : students)
    oos.writeObject(student);
    oos.close();

    //从文件中读出对象
    FileInputStream file = new FileInputStream("haoguicai000.txt");
    ObjectInputStream ois = new ObjectInputStream(file);
    while(file.available()>0)
    System.out.println(((Student)ois.readObject()).getName());
    ois.close();
    file.close();
    }
    catch(FileNotFoundException ex)
    {
    ex.printStackTrace();
    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }
    catch(ClassNotFoundException ex)
    {
    ex.printStackTrace();
    }
    }
    }
    ps:一楼的代码忘了关闭流了。