Java中的IO流操作 ObjectInputStream和ObjectOutputStream实现信息的增删功能

ObjectInputStream循环读取文件中的对象个数,要做对象为空的catch异常

 1 package object.io;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Student implements Serializable{
 6     @Override
 7     public String toString() {
 8         return code+"	"+name+"	"+age+"	"+score;
 9     }
10     public int getCode() {
11         return code;
12     }
13     public void setCode(int code) {
14         this.code = code;
15     }
16     public String getName() {
17         return name;
18     }
19     public void setName(String name) {
20         this.name = name;
21     }
22     public int getAge() {
23         return age;
24     }
25     public void setAge(int age) {
26         this.age = age;
27     }
28     public double getScore() {
29         return score;
30     }
31     public void setScore(double score) {
32         this.score = score;
33     }
34     private int code;
35     private String name;
36     private int age;
37     private double score;
38     public Student(int code, String name, int age, double score) {
39         super();
40         this.code = code;
41         this.name = name;
42         this.age = age;
43         this.score = score;
44     }
45     public Student() {
46         super();
47     }
48     
49 }
  1 package object.io;
  2 
  3 import java.io.FileInputStream;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.io.ObjectInputStream;
  7 import java.io.ObjectOutputStream;
  8 import java.util.ArrayList;
  9 import java.util.List;
 10 import java.util.Scanner;
 11 
 12 
 13 
 14 public class StudentMain {
 15     static Scanner sc=new Scanner(System.in);
 16     //static List<Student> list=new ArrayList<Student>();
 17     static List<Student> list;
 18     //static List<Student> list2=new ArrayList<Student>();
 19 //    static List<Student> list2;
 20     
 21     //录入学生信息
 22     public static void add() throws Exception{
 23         
 24     
 25         try {
 26             FileInputStream input=new FileInputStream("D:\Program Files (x86)\io\Student.txt");
 27             ObjectInputStream oip=new ObjectInputStream(input);
 28             list=new ArrayList<Student>();
 29             while(true){
 30                 //list.add(student);
 31                 if((oip.available())!=-1){
 32                     Student student=(Student) oip.readObject();
 33                     list.add(student);
 34                     
 35                 }else{
 36                     break;
 37                 }
 38                 
 39             }
 40         } catch (Exception e) {
 41             // TODO Auto-generated catch block
 42         } 
 43         FileOutputStream output=new FileOutputStream("D:\Program Files (x86)\io\Student.txt");
 44         ObjectOutputStream oop=new ObjectOutputStream(output);
 45         boolean flag=true;
 46         while(flag){
 47             System.out.println("请输入学生的学号:");
 48             int code=sc.nextInt();
 49             if (code!=0) {
 50                 System.out.println("请输入学生的姓名:");
 51                 String name=sc.next();
 52                 System.out.println("请输入学生的年龄:");
 53                 int age=sc.nextInt();
 54                 System.out.println("请输入学生的成绩:");
 55                 double score=sc.nextDouble();
 56                 Student s=new Student(code, name, age, score);
 57                 list.add(s);
 58                 
 59             }else{
 60                 flag=false;
 61             }
 62             
 63             
 64         }
 65         for(Student student2:list){
 66             oop.writeObject(student2);
 67         }
 68         
 69     }
 70     
 71     
 72     //展示学生信息
 73     public static void show(){
 74         try {
 75             FileInputStream input=new FileInputStream("D:\Program Files (x86)\io\Student.txt");
 76             ObjectInputStream oip=new ObjectInputStream(input);
 77             list=new ArrayList<Student>();
 78             while(true){
 79                 //list.add(student);
 80                 if((oip.available())!=-1){
 81                     Student student=(Student) oip.readObject();
 82                     list.add(student);
 83                     
 84                 }else{
 85                     break;
 86                 }
 87                 
 88             }
 89         } catch (Exception e) {
 90             // TODO Auto-generated catch block
 91         } 
 92         try {
 93             System.out.println("学号	姓名	年龄	成绩");
 94             FileInputStream input=new FileInputStream("D:\Program Files (x86)\io\Student.txt");
 95             ObjectInputStream oip=new ObjectInputStream(input);
 96             while(true){
 97                 //list.add(student);
 98                 if((oip.available())!=-1){
 99                     Student student=(Student) oip.readObject();
100                     System.out.println(student.getCode()+"	"+student.getName()+"	"+student.getAge()+"	"+student.getScore());
101                 }else{
102                     break;
103                 }
104                 
105             }
106         } catch (Exception e) {
107             // TODO Auto-generated catch block
108         } 
109         
110     }
111     
112     public static void delete() throws IOException, ClassNotFoundException{
113         //先获取到文本中对象到集合
114         list=new ArrayList<Student>();
115         try {
116             FileInputStream input=new FileInputStream("D:\Program Files (x86)\io\Student.txt");
117             ObjectInputStream oip=new ObjectInputStream(input);
118             while(true){
119                 //list.add(student);
120                 if((oip.available())!=-1){
121                     Student student=(Student) oip.readObject();
122                     list.add(student);
123                     
124                 }else{
125                     break;
126                 }
127                 
128             }
129         } catch (Exception e) {
130             // TODO Auto-generated catch block
131         } 
132         //找到集合中想要删除的学号
133         System.out.println("请输入要删除的学生学号:");
134         int code=sc.nextInt();
135         boolean flag = false;
136         for(int i=0;i<list.size();i++){
137             if(list.get(i).getCode()==code){
138                 list.remove(i);
139                 System.out.println("删除成功");
140                 flag=true;
141                 break;
142             }
143         }
144         if(flag==false){
145             System.out.println("输入的学号不存在");
146         }
147         
148         
149         //重新将更过的集合写入文本中
150         FileOutputStream output=new FileOutputStream("D:\Program Files (x86)\io\Student.txt");
151         ObjectOutputStream oop=new ObjectOutputStream(output);
152         for(Student student3:list){
153             oop.writeObject(student3);
154         }
155         
156         
157     }
158     public static void main(String[] args) throws Exception {
159         while(true){
160             System.out.println("1.显示  2.增加  3.删除  0.退出");
161             System.out.println("请输入选择:");
162             int num=sc.nextInt();
163             switch(num){
164             case 1:
165                 show();
166                 break;
167             case 2:
168                 add();
169                 break;
170             case 3:
171                 delete();
172                 break;
173             case 0:
174                 System.out.println("退出系统");
175                 return;
176             }
177             
178         }
179     }
180 }
原文地址:https://www.cnblogs.com/ztt0918/p/8124196.html