贾绍贾绍 2016-07-23 12:53 采纳率: 50%
浏览 1943
已采纳

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;
}
}

  • 写回答

3条回答 默认 最新

  • JPF1024 2016-07-23 15:06
    关注

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

    首先,如果你要保存多个值,肯定涉及要涉及到循环读取输入,并且传入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;
    }
    
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的