码迷,mamicode.com
首页 > 其他好文 > 详细

课堂测试四(文件读写)

时间:2017-11-01 01:14:06      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:输入   函数   科学   删除   2.0   设置   技术   throw   boolean   

学生信息管理系统

(本程序以文本方式存储数据,不通过数组,直接对文件进行操作)

学生类

Student.java

 1 public class Student {
 2     private String stunumber;            //学号
 3     private String name;                //姓名
 4     private int age;                    //年龄
 5     private boolean sex;                //性别
 6     private double score;                //分数
 7     
 8     /***构造方法***/
 9     public Student(String num,String name,int age,boolean sex,double score){
10         stunumber = num;
11         this.name = name;
12         this.age = age;
13         this.sex = sex;
14         this.score = score;
15     }
16     
17     /***get函数***/
18     public String getNum(){                //取学号
19         return stunumber;
20     }
21     public String getName(){            //取姓名
22         return name;
23     }
24     public int getAge(){                //取年龄
25         return age;
26     }
27     public boolean getSex(){            //取性别
28         return sex;
29     }
30     public double score(){                //取分数
31         return score;
32     }
33     
34     /***set函数***/
35     public void setNum(String num){                //设置学号
36          stunumber = num;
37     }
38     public void setName(String name){            //设置姓名
39          this.name = name;
40     }
41     public void setAge(int age){                //设置年龄
42          this.age = age;
43     }
44     public void setSex(boolean sex){            //设置性别
45          this.sex = sex;
46     }
47     public void score(double score){                //设置分数
48          this.score = score;
49     }
50 }

管理类

StudentManager.java

  1 import java.io.*;
  2 import java.util.Scanner;
  3 public class StudentManager {
  4     
  5     /***主函数***/
  6     public static void main(String[] args) throws IOException {
  7         Scanner in = new Scanner(System.in);
  8         while(true){
  9             menu();
 10             int n = in.nextInt();
 11             switch(n){
 12             case 1:showStudent();break;
 13             case 2:
 14                 System.out.println("请依次输入学生的学号、姓名、年龄、性别(true/false)、分数:");
 15                 addStudent(in.next(),in.next(),in.nextInt(),in.nextBoolean(),in.nextDouble());
 16                 break;
 17             case 3:
 18                 System.out.println("输入要删除的学生的学号:");
 19                 deleteStudent(in.next());
 20                 break;
 21             case 4:
 22                 System.out.println("输入要修改的学生的学号,及修改后的姓名、年龄、性别(true/false)、分数:");
 23                 updateStudent(in.next(),in.next(),in.nextInt(),in.nextBoolean(),in.nextDouble());
 24                 break;
 25             case 5:
 26                 System.out.println("输入要查询的学生的学号:");
 27                 selectStudent(in.next());
 28                 break;
 29             }
 30         }
 31     }
 32     
 33     /***显示菜单***/
 34     public static void menu(){
 35         System.out.println("**********************************************************");
 36         System.out.println("                                           石家庄铁道大学信息科学与技术学院");
 37         System.out.println("                                                        学生信息管理系统V2.0");
 38         System.out.println("**********************************************************");
 39         System.out.println("                     1.遍历输出学生信息;");
 40         System.out.println("                     2.新学生信息录入;");
 41         System.out.println("                     3.删除学生信息;");
 42         System.out.println("                     4.修改学生信息;");
 43         System.out.println("                     5.查询学生信息;");
 44         System.out.println("**********************************************************");
 45     }
 46     
 47     /***显示所有学生信息 ***/
 48     public static void showStudent() throws IOException{
 49         File file = new File("student.dat");
 50         BufferedReader reader = null;
 51         reader = new BufferedReader(new FileReader(file));
 52         String tempString = null;
 53         while ((tempString = reader.readLine()) != null) {
 54             System.out.println(tempString);
 55         }
 56     }
 57     
 58     /***添加学生信息***/
 59     public static void addStudent(String stunumber,String name,int age,boolean sex,double score) throws IOException{
 60         FileWriter writer = new FileWriter("student.dat",true);
 61         String s;
 62         if(sex)
 63             s = "女";
 64         else s = "男";
 65         writer.append(stunumber + " " + name + " " + age + " " + s + " " + score + "\r\n");
 66         writer.close();
 67         System.out.println("添加成功!");
 68     }
 69     
 70     /***删除学生信息 ***/
 71     public static void deleteStudent(String stunumber) throws IOException{
 72         
 73         StringBuffer all = new StringBuffer();
 74         File file = new File("student.dat");
 75         String temp;
 76         BufferedReader reader = new BufferedReader(new FileReader(file));
 77         
 78         while((temp = reader.readLine()) != null){
 79             if(!temp.startsWith(stunumber))
 80                 all.append(temp + "\r\n");
 81         }
 82         reader.close();
 83         
 84         BufferedWriter writer = new BufferedWriter(new FileWriter(file));
 85         writer.write(all.toString());;
 86         writer.close();
 87         System.out.println("删除成功!");
 88     }
 89     
 90     /***更新学生信息***/
 91     public static void updateStudent(String stunumber,String name,int age,boolean sex,double score) throws IOException{
 92         StringBuffer all = new StringBuffer();
 93         File file = new File("student.dat");
 94         String temp,s;
 95         if(sex)
 96             s = "女";
 97         else s = "男";
 98         BufferedReader reader = new BufferedReader(new FileReader(file));
 99         
100         while((temp = reader.readLine()) != null){
101             if(!temp.startsWith(stunumber))
102                 all.append(temp + "\r\n");
103             else all.append(stunumber + " " + name + " " + age + " " + s + " " + score + "\r\n");
104         }
105         reader.close();
106         
107         BufferedWriter writer = new BufferedWriter(new FileWriter(file));
108         writer.write(all.toString());;
109         writer.close();
110         System.out.println("修改成功!");
111     }
112     
113     /***查询学生信息***/
114     public static void selectStudent(String stunumber) throws IOException{
115         File file = new File("student.dat");
116         BufferedReader reader = new BufferedReader(new FileReader(file));
117         String stuLine;
118         while((stuLine = reader.readLine()) != null)
119         {
120             if(stuLine.startsWith(stunumber))
121             {
122                 System.out.println(stuLine);
123                 return;
124             }
125         }
126         System.out.println("查询结果为空!");
127     }
128 }

 

课堂测试四(文件读写)

标签:输入   函数   科学   删除   2.0   设置   技术   throw   boolean   

原文地址:http://www.cnblogs.com/lzq666/p/7764359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!