黑马程序员 java基础IOTest

----------- 、、java学习型技术博客、期待与您交流! ------------

/** * 有5个学生 每个学生有3门课的成绩 从键盘输入以上数据 (包括 姓名 三门课成绩) 输入的格式 如 张三,3,40,50 计算出总成绩 * 并把学生的信息和计算出的总分数高低顺序存档到磁盘文件 "stu.txt"中 * * 1 描述学生对象 2 对应一个可以操作学生对象的工具类 * * 思想 1 通过获取键盘录入的一行数据 并将改行数据中德信息取出封装成学生对象 2 因为学生有很多 那么就需要存储 使用到集合 因为要对学生的总分排序 * 所以可以使用TreeSet 3 将集合众的数据写到指定的文件中 * * @author lazy * */ public class IOTest { public static void main(String[] args) { Set<Student> stus = StudentInfoTool.getStudents(); try { StudentInfoTool.writeToFile(stus); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 定义学生类 用于描述学生对象 * * @author lazy * */ class Student implements Comparable<Student> { // 实现Comparable接口使之具有比较性方便后面进行排序 private String name; private int math, cn, en; private int sum; Student(String name, int math, int cn, int en) { this.name = name; this.math = math; this.cn = cn; this.en = en; sum = math + cn + en; } @Override public int hashCode() { // TODO Auto-generated method stub return name.hashCode() + sum * 13; // 确保hash值唯一 } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub if (!(obj instanceof Student)) throw new ClassCastException("类型不匹配"); Student s = (Student) obj; return this.name.equals(s.name) && this.sum == s.sum; } @Override public int compareTo(Student o) { // TODO Auto-generated method stub int num = ((Integer) this.sum).compareTo((Integer) o.sum); if (num == 0) return this.name.compareTo(o.name); return num; } @Override public String toString() { // TODO Auto-generated method stub return "stundent[" + name + " ," + math + " " + cn + " ," + en + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getCn() { return cn; } public void setCn(int cn) { this.cn = cn; } public int getEn() { return en; } public void setEn(int en) { this.en = en; } public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } } class StudentInfoTool { public static Set<Student> getStudents() { BufferedReader bufr = null; Set<Student> stus = null; // 读取键盘 try { bufr = new BufferedReader(new InputStreamReader(System.in)); String line = null; stus = new TreeSet<Student>(); while ((line = bufr.readLine()) != null) { if (("over").equals(line)) break; String[] info = line.split(","); Student stu = new Student(info[0], Integer.parseInt(info[1]), Integer.parseInt(info[2]), Integer.parseInt(info[3])); stus.add(stu);// 添加到set集合中 } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bufr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return stus; } // 将集合中的信息写到文件中 public static void writeToFile(Set<Student> stus) throws IOException { BufferedWriter bufw = new BufferedWriter(new FileWriter("stuInfo.txt")); for (Student stu : stus) { bufw.write(stu.toString() + " "); bufw.write(stu.getSum() + ""); bufw.newLine(); bufw.flush(); } bufw.close(); } }

----------------------- 、、java学习型技术博客、期待与您交流! ----------------------

详情请查看:

经验分享 程序员 微信小程序 职场和发展