java只能文件中读取第一个对象,先将对象存Arraylist再输出到控制台输出的时不能多个输出?怎么改?

package cn.edu.hyit.file;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

public class Demo_5 {
static ArrayList al=new ArrayList();
public static void main(String[] args) {
write();
read();

}
public static Student read(){

    FileInputStream fis = null;
    Student temp = null;
    try {
            fis = new FileInputStream("stu.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            //Student stu = (Student)ois.readObject();
            //while(fis.available()>0){
            al = (ArrayList<Student>)ois.readObject();              
            //}
            for(Student s:al){
            System.out.println("学生的名字:"+s.getName()+"\t"+"学生的年龄是"+s.getAge());
            }
       } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally{
           if(fis!=null){
            try{
                fis.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
    return temp;
}

public static Student write(){
    FileOutputStream fos = null;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    try{    
        fos = new FileOutputStream("stu.txt",true);
        ObjectOutputStream obs = new ObjectOutputStream(fos);
        System.out.println("请输入名字");
        String name=br.readLine();
        System.out.println("请输入年龄");
        String s=br.readLine();
        int age=Integer.parseInt(s);

        Student stu = new Student(name,age);
        al.add(stu);
        obs.writeObject(al);
        obs.flush();
        obs.close();
    }catch(IOException e){
        e.printStackTrace();
    }
    return null;
}

}

class Student implements Serializable{
//private static final long serialVersionUID = 1L;
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}

我只是要证明一下,除了楼上那个好人,我也会回答你的问题。

首先,如果你要保存多个值,肯定涉及要涉及到循环读取输入,并且传入List.

public static Student write(){

    try{
    while(true){
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));    
        FileOutputStream fos =  = new FileOutputStream("stu.txt",true);
        ObjectOutputStream obs = new ObjectOutputStream(fos);
        System.out.println("请输入名字");
        String name=br.readLine();
        System.out.println("请输入年龄");
        String s=br.readLine();
        int age=Integer.parseInt(s);

        Student stu = new Student(name,age);
        al.add(stu);
        obs.writeObject(al);
        obs.flush();
        obs.close();
         System.out.println("是否退出([1]退出)?");
        String exit=br.readLine();
        if(exit){
            break;
        }
        }
    }catch(IOException e){
        e.printStackTrace();
    }
    return null;
}

大概就是这样,你运行一下试试,我没运行

别乱写啊 孩子,你序列化的时候写进去的是student对象,你反序列化怎么可能读出来arraylist,顺便序列化每次只能写进一个对象,你那文件里只有一个对象
每次都会覆盖的,
你要做什么作业吗,不是像你这么写的,建议多看书,这种问题也就我会耐心地回答你了

谢谢你!是作业,把学生类保存到文件,下一次运行的时候可以直接从文件中读取,我现在是想写几个对象进去,先写到集合,再把集合写入保存到文件,然后运行从文件输出哪几个对象。那怎么写呢?