黑马程序员——IO流中的练习

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------
需求:从键盘输入学生成绩信息,并将信息按照总分高低存入文件中
1、首先建立一个描述学生对象的类 Student,这个学生信息存储方式,按照需要覆写toString()、comperTo()、hashCode()、equals()等基本方法,另外一个学生类一般具备set()、get()方法,用来设置和获取学生信息,这个主要是集合章节的使用
2、键盘录入信息,键盘读取流;将信息先存到一个集合中以实现排序
3、录入完毕后,遍历集合并将集合中的数据使用输出流存入文件中
4、关闭资源,完成整个需求
代码实现如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class  Student implements  Comparable<Student>{
     
     private  String name;
     private  float  chinese;
     private  float  math;
     private  float  english;
     public  Student(String name, float  chinese, float  math, float  english){
         this .name = name;
         this .chinese = chinese;
         this .math = math;
         this .english = english;
     }
     public  int  compareTo(Student s){
         float  t = s.getTotal();
         int  num = new  Float( this .getTotal()).compareTo( new  Float(t));           //this.name.compareTo(s.name);
         return  num;    //比较器的反转?
     }
     public  String toString(){
         return  "name:" +name+ " chinese:" +chinese+ " math:" +math+ " english:" +english+ " -->Total" + this .getTotal();
     }
     public  void  setName(String name){
         this .name = name ;
     }
     public  void  setChinese( float  chinese){
         this .chinese = chinese;
     }
     public  void  setMath( float  math){
         this .math = math;
     }
     public  void  setEnglish( float  english){
         this .english = english;
     }
     public  float  getTotal(){
         return  chinese + math + english;
     }
     public  int  hashCode(){
         return  name.hashCode() + ( int )getTotal()* 39 ;
     }
     public  boolean  equals(Object obj){
         if (obj instanceof  Student)
             throw  new  ClassCastException( "类型不匹配!" );
         Student s = (Student)obj;
         return  this .name.equals(s.name) &&
             this .getTotal() == s.getTotal();
     }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import  java.io.BufferedReader;
import  java.io.BufferedWriter;
import  java.io.FileNotFoundException;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.io.InputStreamReader;
import  java.io.OutputStreamWriter;
import  java.util.Iterator;
import  java.util.TreeSet;
 
public  class  Storage {
 
     public  static  void  main(String args[]){
     
         stor();
         
         
     }
     public  static  void  stor(){ 
         TreeSet<Student> ts = new  TreeSet<Student>();
         BufferedReader bufr= new  BufferedReader( new  InputStreamReader(System.in));
         try  {
             String line = null ;
             while ((line = bufr.readLine()) != null ){
                 String[] ss = line.split( " " );
                 
                 if (judge(line)) //判断合法
                 {
                     ts.add( new  Student(ss[ 0 ], Float.parseFloat(ss[ 1 ]), Float.parseFloat(ss[ 2 ]), new  Float(ss[ 3 ])));
                     
                 }
                 else
                 {
                     if ( "over" .equals(line))
                         break ;
                     else
                         sop( "输入数据非法,请重新输入" );
                 }
                 
             }
         } catch  (IOException e) {
             sop( "读取信息失败" );
             e.printStackTrace();
         } finally {
             try  {
                 bufr.close();
             } catch  (IOException e) {
                 new   RuntimeException( "读取信息关闭失败!" );
                 e.printStackTrace();
             }
             
         }
         //将信息存入文件中
         printts(ts);
     }
     public  static  void  printts(TreeSet<Student> ts){
         BufferedWriter bufw = null ;
         try  {
             bufw = new  BufferedWriter( new  OutputStreamWriter( new  FileOutputStream( "stud.txt" )));
             Iterator<Student> it = ts.iterator();
             while (it.hasNext()){
                 Student s = it.next();
                 String s0 = s.toString();
                 try  {
                     bufw.write(s0);
                     bufw.newLine();
                     bufw.flush();
                 } catch  (IOException e) {
                     sop( "信息写入失败!!" );
                     e.printStackTrace();
                 }
             }
             try  {
                 bufw.close();
             } catch  (IOException e) {
                 sop( "关闭写入流失败!" );
                 e.printStackTrace();
             }
             
         } catch  (FileNotFoundException e1) {
             sop( "信息写入失败!" );
             e1.printStackTrace();
         }
     }
     public  static  boolean  judge(String s){
         String[] ss = s.split( " " );
         if (ss.length == 4 )
             return  true ;
         else
             return  false ;
     }
     public  static  void  sop(Object obj){
         System.out.println(obj);
     }
}

  

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值