JavaSE-19.1.1【IO流练习案例-集合到文件(排序改进版)】

 1 package day10.lesson1.p1;
 2 
 3 import java.io.BufferedWriter;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 import java.util.Comparator;
 7 import java.util.Scanner;
 8 import java.util.TreeSet;
 9 
10 /*
11 1 IO流练习
12 
13 1.1 案例-集合到文件(排序改进版)
14 
15     键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩)。
16     要求按照成绩总分从高到低写入文本文件
17     格式:姓名,语文成绩,数学成绩,英语成绩 举例:林青霞,98,99,100
18 
19     定义学生类
20     2. 创建TreeSet集合,通过比较器排序进行排序
21     3. 键盘录入学生数据
22     4. 创建学生对象,把键盘录入的数据对应赋值给学生对象的成员变量
23     5. 把学生对象添加到TreeSet集合
24     6. 创建字符缓冲输出流对象
25     7. 遍历集合,得到每一个学生对象
26     8. 把学生对象的数据拼接成指定格式的字符串
27     9. 调用字符缓冲输出流对象的方法写数据
28     10. 释放资源
29  */
30 public class TreeSetToFileDemo {
31     public static void main(String[] args) throws IOException {
32         TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() { //比较器排序
33             @Override
34             public int compare(Student s1, Student s2) { //自定义比较规则
35                 //主要条件
36                 int num = s2.getSum() - s1.getSum();
37                 //次要条件
38                 int num2 = (num==0) ? (s1.getChinese()-s2.getChinese()) : num;
39                 int num3 = (num2==0) ? (s1.getMath()-s2.getMath()) : num2;
40                 int num4 = (num3==0) ? (s1.getName().compareTo(s2.getName())) : num3;
41                 return num4;
42             }
43         });
44 
45         for (int i=1; i<=5; i++){
46             Scanner sc = new Scanner(System.in);
47             System.out.println("请输入第" + i + "个学生的信息:");
48             System.out.println("姓名:");
49             String name = sc.nextLine();
50             System.out.println("语文成绩:");
51             int chinese = sc.nextInt();
52             System.out.println("数学成绩:");
53             int math = sc.nextInt();
54             System.out.println("英语成绩:");
55             int english = sc.nextInt();
56 
57             Student student = new Student();
58             student.setName(name);
59             student.setChinese(chinese);
60             student.setMath(math);
61             student.setEnglish(english);
62 
63             treeSet.add(student);
64         }
65 
66         BufferedWriter bw = new BufferedWriter(new FileWriter("stage2\\src\\day10\\lesson1\\ts.txt"));
67 
68         for (Student s: treeSet){
69             StringBuilder sb = new StringBuilder();
70             sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.getSum());
71 
72             bw.write(sb.toString());
73             bw.newLine();
74             bw.flush();
75         }
76 
77         bw.close();
78     }
79 }
 1 package day10.lesson1.p1;
 2 
 3 public class Student {
 4 
 5     private String name;
 6     private int chinese;
 7     private int math;
 8     private int english;
 9 
10     public Student() {
11     }
12 
13     public Student(String name, int chinese, int math, int english) {
14         this.name = name;
15         this.chinese = chinese;
16         this.math = math;
17         this.english = english;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public void setChinese(int chinese) {
25         this.chinese = chinese;
26     }
27 
28     public void setMath(int math) {
29         this.math = math;
30     }
31 
32     public void setEnglish(int english) {
33         this.english = english;
34     }
35 
36     public String getName() {
37         return name;
38     }
39 
40     public int getChinese() {
41         return chinese;
42     }
43 
44     public int getMath() {
45         return math;
46     }
47 
48     public int getEnglish() {
49         return english;
50     }
51 
52     public int getSum(){
53         return this.chinese + this.math + this.english;
54     }
55 }

 

冯七,85,85,75,245
王五,55,99,88,242
张三,90,80,70,240
赵六,88,77,66,231
李四,80,60,80,220

  

上一篇:掌握 analyze API,一举搞定 Elasticsearch 分词难题


下一篇:go将一个文件的内容写入另外一个文件