zoukankan      html  css  js  c++  java
  • 黑马程序员——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 Studentimplements 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(objinstanceof 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培训、期待与您交流! ----------------------
    用心-细心-专心-决心 学习就像爬大山,一步一步向前走 -态度决定高度-
  • 相关阅读:
    Day17_购物车
    Kali小技巧
    sqlserver误删数据库表恢复记录
    JS轮流播放视频和图片
    .net core使用ocelot---第八篇 Consul
    .net core使用ocelot---第七篇 服务发现
    .net core使用ocelot---第六篇 负载均衡
    .net core使用ocelot---第五篇 服务质量
    .net core使用ocelot---第四篇 限流熔断
    .net core使用ocelot---第三篇 日志记录
  • 原文地址:https://www.cnblogs.com/xianyou-liang/p/8503362.html
Copyright © 2011-2022 走看看