集合与IO流结合的练习

Edit

集合与IO流结合的练习

练习:

有五个学生,每个学生有3门课程:数学,语文,英语

从键盘输入以上数据(包括姓名,三门课成绩),

输入的格式:zhangsan, 30, 40, 40计算出总成绩,

并把学生的信息和计算出的总分数高低顺序存放在磁盘文件”stud.txt”中.

1.描述学生对象;

2.定义一个可操作学生对象的工具类;

思路:

1.通过获取键盘录入的一行数据,并将录入的信息封装成学生对象;

2.因为学生对象很多,且要对学生的总分排序,所以用TreeSet集合;

3.将集合中的信息写入到文件中;


1.import java.io.*;2.import java.util.*;3.class  StudentInfoTest4.{5.    public static void main(String[] args)  throws IOException6.    {7.        Comparator<Student> comp = Collections.reverseOrder();//因为之前的排序是从小到大排,现在从大到小排,所以定义比较器8.        Set<Student> stus = StudentInfoTool.getStudent(comp);9.        StudentInfoTool.writeToFile(stus);10.    }11.}12.class Student implements Comparable<Student>13.{14.    private String name;15.    private int ma,cn,en;16.    private int sum;17.18.    Student(String name,int ma,int cn,int en)19.    {20.        this.name = name;21.        this.ma = ma;22.        this.cn = cn;23.        this.en = en;24.        sum = ma + cn + en;25.    }26.    public String getName()27.    {28.        return name;29.    }30.    public int getSum()31.    {32.        return sum;33.    }34.    public String toString()35.    {36.        return "student["+name+", "+ma+", "+cn+", "+en+",]";37.    }38.    public int compareTo(Student stu)39.    {40.        int num = new Integer(this.sum).compareTo(new Integer(stu.sum));41.        if (num==0)42.        {43.            return this.name.compareTo(stu.name);44.        }45.        return num;46.    }47.    public int hashCode()48.    {49.        return name.hashCode()+sum*39;50.    }51.    public boolean equals(Object obj)52.    {53.        if (!(obj instanceof Student))54.        {55.            throw new ClassCastException("传入类型不正确");56.        }57.        Student stu = (Student)obj;58.        return this.name.equals(stu.name) && this.sum==stu.sum;59.    }60.}61.class StudentInfoTool62.{63.    public static Set<Student> getStudent() throws IOException64.    {65.        getStudent(null);66.        return null;67.    }68.    public static Set<Student> getStudent(Comparator<Student> comp) throws IOException69.    {70.        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));71.        String line = null;72.        Set<Student> stus = null;73.        if (comp==null)74.        {75.            stus = new TreeSet<Student>();76.        }77.        else78.        {79.            stus = new TreeSet<Student>(comp);80.        }81.        while ((line=bufr.readLine()) !=null)82.        {83.            if ("over".equals(line))84.            {85.                break;86.            }87.            String[] arr = line.split(",");88.            Student stu = new Student(arr[0],Integer.parseInt(arr[1]),89.                Integer.parseInt(arr[2]),90.                Integer.parseInt(arr[3]));91.            stus.add(stu);92.        }93.        bufr.close();94.        return stus;95.    }96.    public static void writeToFile(Set<Student> stus) throws IOException97.    {98.        BufferedWriter bufw = new BufferedWriter(new FileWriter("e:\\stu.txt"));99.        for (Student stu : stus)100.        {101.            bufw.write(stu.toString());102.            bufw.write(stu.getSum()+"");103.            bufw.newLine();104.            bufw.flush();105.        }106.        bufw.close();107.    }108.109.}110.111.

@%2802.%u8D44%u6599%29%5B%20%u96C6%u5408%u4E0E%u6D41%u7684%u7EC3%u4E60%5D%0A%23%u96C6%u5408%u4E0EIO%u6D41%u7ED3%u5408%u7684%u7EC3%u4E60%0A%3E%u7EC3%u4E60%3A%0A%u6709%u4E94%u4E2A%u5B66%u751F%2C%u6BCF%u4E2A%u5B66%u751F%u67093%u95E8%u8BFE%u7A0B%3A%u6570%u5B66%2C%u8BED%u6587%2C%u82F1%u8BED%0A%u4ECE%u952E%u76D8%u8F93%u5165%u4EE5%u4E0A%u6570%u636E%28%u5305%u62EC%u59D3%u540D%2C%u4E09%u95E8%u8BFE%u6210%u7EE9%29%2C%0A%u8F93%u5165%u7684%u683C%u5F0F%3Azhangsan%2C%2030%2C%2040%2C%2040%u8BA1%u7B97%u51FA%u603B%u6210%u7EE9%2C%0A%u5E76%u628A%u5B66%u751F%u7684%u4FE1%u606F%u548C%u8BA1%u7B97%u51FA%u7684%u603B%u5206%u6570%u9AD8%u4F4E%u987A%u5E8F%u5B58%u653E%u5728%u78C1%u76D8%u6587%u4EF6%22stud.txt%22%u4E2D.%0A%0A1.%u63CF%u8FF0%u5B66%u751F%u5BF9%u8C61%3B%0A2.%u5B9A%u4E49%u4E00%u4E2A%u53EF%u64CD%u4F5C%u5B66%u751F%u5BF9%u8C61%u7684%u5DE5%u5177%u7C7B%3B%0A%0A**%u601D%u8DEF%3A**%0A1.%u901A%u8FC7%u83B7%u53D6%u952E%u76D8%u5F55%u5165%u7684%u4E00%u884C%u6570%u636E%2C%u5E76%u5C06%u5F55%u5165%u7684%u4FE1%u606F%u5C01%u88C5%u6210%u5B66%u751F%u5BF9%u8C61%3B%0A2.%u56E0%u4E3A%u5B66%u751F%u5BF9%u8C61%u5F88%u591A%2C%u4E14%u8981%u5BF9%u5B66%u751F%u7684%u603B%u5206%u6392%u5E8F%2C%u6240%u4EE5%u7528TreeSet%u96C6%u5408%3B%0A3.%u5C06%u96C6%u5408%u4E2D%u7684%u4FE1%u606F%u5199%u5165%u5230%u6587%u4EF6%u4E2D%3B%0A%0A---%0A%60%60%60java%0Aimport%20java.io.*%3B%0Aimport%20java.util.*%3B%0Aclass%20%20StudentInfoTest%0A%7B%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%20throws%20IOException%0A%09%7B%0A%09%09Comparator%3CStudent%3E%20comp%20%3D%20Collections.reverseOrder%28%29%3B//%u56E0%u4E3A%u4E4B%u524D%u7684%u6392%u5E8F%u662F%u4ECE%u5C0F%u5230%u5927%u6392%2C%u73B0%u5728%u4ECE%u5927%u5230%u5C0F%u6392%2C%u6240%u4EE5%u5B9A%u4E49%u6BD4%u8F83%u5668%0A%09%09Set%3CStudent%3E%20stus%20%3D%20StudentInfoTool.getStudent%28comp%29%3B%0A%09%09StudentInfoTool.writeToFile%28stus%29%3B%0A%09%7D%0A%7D%0Aclass%20Student%20implements%20Comparable%3CStudent%3E%0A%7B%0A%09private%20String%20name%3B%0A%09private%20int%20ma%2Ccn%2Cen%3B%0A%09private%20int%20sum%3B%0A%0A%09Student%28String%20name%2Cint%20ma%2Cint%20cn%2Cint%20en%29%0A%09%7B%0A%09%09this.name%20%3D%20name%3B%0A%09%09this.ma%20%3D%20ma%3B%0A%09%09this.cn%20%3D%20cn%3B%0A%09%09this.en%20%3D%20en%3B%0A%09%09sum%20%3D%20ma%20+%20cn%20+%20en%3B%0A%09%7D%0A%09public%20String%20getName%28%29%0A%09%7B%0A%09%09return%20name%3B%0A%09%7D%0A%09public%20int%20getSum%28%29%0A%09%7B%0A%09%09return%20sum%3B%0A%09%7D%0A%09public%20String%20toString%28%29%0A%09%7B%0A%09%09return%20%22student%5B%22+name+%22%2C%20%22+ma+%22%2C%20%22+cn+%22%2C%20%22+en+%22%2C%5D%22%3B%0A%09%7D%0A%09public%20int%20compareTo%28Student%20stu%29%0A%09%7B%0A%09%09int%20num%20%3D%20new%20Integer%28this.sum%29.compareTo%28new%20Integer%28stu.sum%29%29%3B%0A%09%09if%20%28num%3D%3D0%29%0A%09%09%7B%0A%09%09%09return%20this.name.compareTo%28stu.name%29%3B%0A%09%09%7D%0A%09%09return%20num%3B%0A%09%7D%0A%09public%20int%20hashCode%28%29%0A%09%7B%0A%09%09return%20name.hashCode%28%29+sum*39%3B%0A%09%7D%0A%09public%20boolean%20equals%28Object%20obj%29%0A%09%7B%0A%09%09if%20%28%21%28obj%20instanceof%20Student%29%29%0A%09%09%7B%0A%09%09%09throw%20new%20ClassCastException%28%22%u4F20%u5165%u7C7B%u578B%u4E0D%u6B63%u786E%22%29%3B%0A%09%09%7D%0A%09%09Student%20stu%20%3D%20%28Student%29obj%3B%0A%09%09return%20this.name.equals%28stu.name%29%20%26%26%20this.sum%3D%3Dstu.sum%3B%0A%09%7D%0A%7D%0Aclass%20StudentInfoTool%0A%7B%0A%09public%20static%20Set%3CStudent%3E%20getStudent%28%29%20throws%20IOException%0A%09%7B%0A%09%09getStudent%28null%29%3B%0A%09%09return%20null%3B%0A%09%7D%0A%09public%20static%20Set%3CStudent%3E%20getStudent%28Comparator%3CStudent%3E%20comp%29%20throws%20IOException%0A%09%7B%0A%09%09BufferedReader%20bufr%20%3D%20new%20BufferedReader%28new%20InputStreamReader%28System.in%29%29%3B%0A%09%09String%20line%20%3D%20null%3B%0A%09%09Set%3CStudent%3E%20stus%20%3D%20null%3B%0A%09%09if%20%28comp%3D%3Dnull%29%0A%09%09%7B%0A%09%09%09stus%20%3D%20new%20TreeSet%3CStudent%3E%28%29%3B%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09stus%20%3D%20new%20TreeSet%3CStudent%3E%28comp%29%3B%0A%09%09%7D%0A%09%09while%20%28%28line%3Dbufr.readLine%28%29%29%20%21%3Dnull%29%0A%09%09%7B%0A%09%09%09if%20%28%22over%22.equals%28line%29%29%0A%09%09%09%7B%0A%09%09%09%09break%3B%0A%09%09%09%7D%0A%09%09%09String%5B%5D%20arr%20%3D%20line.split%28%22%2C%22%29%3B%0A%09%09%09Student%20stu%20%3D%20new%20Student%28arr%5B0%5D%2CInteger.parseInt%28arr%5B1%5D%29%2C%0A%09%09%09%09Integer.parseInt%28arr%5B2%5D%29%2C%0A%09%09%09%09Integer.parseInt%28arr%5B3%5D%29%29%3B%0A%09%09%09stus.add%28stu%29%3B%0A%09%09%7D%0A%09%09bufr.close%28%29%3B%0A%09%09return%20stus%3B%0A%09%7D%0A%09public%20static%20void%20writeToFile%28Set%3CStudent%3E%20stus%29%20throws%20IOException%0A%09%7B%0A%09%09BufferedWriter%20bufw%20%3D%20new%20BufferedWriter%28new%20FileWriter%28%22e%3A%5C%5Cstu.txt%22%29%29%3B%0A%09%09for%20%28Student%20stu%20%3A%20stus%29%0A%09%09%7B%0A%09%09%09bufw.write%28stu.toString%28%29%29%3B%0A%09%09%09bufw.write%28stu.getSum%28%29+%22%22%29%3B%0A%09%09%09bufw.newLine%28%29%3B%0A%09%09%09bufw.flush%28%29%3B%0A%09%09%7D%0A%09%09bufw.close%28%29%3B%0A%09%7D%0A%0A%7D%0A%0A%0A%60%60%60%0A

时间: 2024-11-08 23:58:53

集合与IO流结合的练习的相关文章

集合、IO流部分思维导图框架梳理

下图是学习集合过程自己梳理的脉络,希望对你有帮助.

java基础复习(集合、泛型、IO流、多线程、Junit 、内省 、Properties、 路径问题)

集合 泛型 IO流 多线程 Junit Assert 注解 内省 Properties 路径问题 集合 ---|Collection: 单列集合 ---|List: 有存储顺序, 可重复 ---|ArrayList: 数组实现, 查找快, 增删慢 由于是数组实现, 在增和删的时候会牵扯到数组 增容, 以及拷贝元素. 所以慢.数组是可以直接按索引查找, 所以查找时较快 ---|LinkedList: 链表实现, 增删快, 查找慢由于链表实现, 增加时只要让前一个元素记住自己就可以, 删除时让前一个

Java基础知识强化之IO流笔记66:Properties的概述 和 使用(作为Map集合使用)

1. Properties的概述  Properties:属性集合类.是一个可以和IO流相结合使用的集合类.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串. Properties是Hashtable的子类,说明是一个Map集合. 2. Properties作为Map集合使用 1 package cn.itcast_08; 2 3 import java.util.Properties; 4 import java.util.Set; 5 6 /* 7 *

Java基础知识强化之IO流笔记68:Properties和IO流集合使用

1. Properties和IO流集合使用 这里的集合必须是Properties集合:  public void load(Reader reader):把文件中的数据读取到集合中  public void store(Writer writer,String comments):把集合中的数据存储到文件 2. 代码实现: 1 package cn.itcast_08; 2 3 import java.io.FileReader; 4 import java.io.FileWriter; 5 i

IO流--与properties集合配合使用

IO流--与properties集合配合使用: 注:生产上主要用于常量文件的配置,读取常量文件: 1:properties集合的放值与取值: /* * properties集合继承自hashTable,使用properties父类的放值(put();),取值(get();) * 功能,遍历集合得到的是Object类型的: * 所以我们使用properties自己特有的放值(setProperties();)和取值(getProperties();)的功能 * 遍历集合得到的是String类型的:

Java 中与IO流有关的集合(Properties集合)

属性集 java.util.Properties继承于Hashtable,来表示一个持久的属性集.它使用键值结构存储数据,每个键及其对应值都是一个字符串.该类也被许多Java类使用,比如获取系统属性时, System.getProperties方法就是返回一个Properties对象. Properties类 Properties集合是Hashtable集合的子类,Hashtable类是Map接口的其中一个实现类.Properties集合 是唯一一个和IO流相结合的集合.Properties类表

Java IO流 探险

Java的IO流使用了一种装饰器设计模式,它将IO流分为底层节点流和上层处理流.本篇重点在如何访问文件与目录.如何以二进制格式和文本格式来读写数据.对象序列化机制.还有Java7的"NIO.2". 装饰设计模式:当想要对已有的对象进行功能增强时,可以定义类,将已有对象传入,基于已有的功能,并提供加强功能.那么自定义的该类称为装饰类. 装饰类通常会通过构造方法接收被装饰的对象.并基于被装饰的对象的功能,提供更强的功能. IO的方式通常分为:BIO(同步阻塞).NIO(同步非阻塞).AIO

Java 之IO流及应用

IO流 IO流概述及FileWriter类的使用 FileReader类使用 缓冲流介绍和使用 IO流相关案例 NO.one IO流概述及FileWriter类使用 1.1 IO流概述及分类 IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的类都在IO包中 流按流向分为两种:输入流,输出流 1.2 FileWriter类使用 A:打开帮助文档 B:点击显示,找到索引,看到输入框 C:你要学习什么内容,你就在框框里面输入什么内容 举例:Random D:看包

java基础8(io流3)

1.LineNumberReader: public int getLineNumber():获取行号 public void setLineNumber(int lineNumber):设置起始行号 String readLine():读取一行 2.操作基本数据类型的流 DataInputStream:读数据 DataOutputStream:写数据 作用:可以操作基本类型的流对象,其读写顺序必须一致,否则数据有问题. public static void main(String[] args