package p3; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Test_IOTest { public static void main(String[] args) throws IOException { /* * 有五个学生,每个学生有三门课的成绩,定义一种比较直观的文件格式, * 输入学生的姓名和成绩,输入格式:name,30,30,30从键盘输入以上数 * 据(包括姓名,三门课成绩),按总分从高到低的顺序将学生的信息存放 * 在硬盘文件“stu.txt”中。 */ inputGread(); } /* * 步骤 * 1,键盘录入数据。 * 2,根据录入数据创建学生对象将数据存储。 * 3,将学生对象存入TreeSet集合。 * 4,通过流将数据存储在"stu.txt"文件中。 */ public static void inputGread() throws IOException{ File file = new File("tempfile\\stu.txt"); if(!file.exists()) file.createNewFile(); ArrayList<Student> list = new ArrayList<Student>(); BufferedReader bfr = null; BufferedWriter bfw = null; try { bfr = new BufferedReader(new InputStreamReader(System.in)); String line = null; while ((line=bfr.readLine())!=null) { if ("over".equals(line)) break; String[] data = line.split(","); String name = data[0]; int math = Integer.parseInt(data[1]); int english = Integer.parseInt(data[2]); int chinses = Integer.parseInt(data[3]); Student student = new Student(name,math,english,chinses); list.add(student); } Collections.sort(list, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { int temp = o2.getSumGrade()-o1.getSumGrade(); return temp==0?o1.getName().compareTo(o2.getName()):temp; } }); bfw = new BufferedWriter(new FileWriter(file)); for (Student student : list) { String data = student.toString(); bfw.write(data); bfw.newLine(); bfw.flush(); } } catch (IOException e) { }finally{ bfr.close(); if(bfw!=null) bfw.close(); } } } class Student implements Comparable<Student>{ private String name; private int math; private int english; private int chinses; /** * @param name * @param math * @param english * @param chinses */ public Student(String name, int math, int english, int chinses) { super(); this.name = name; this.math = math; this.english = english; this.chinses = chinses; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the math */ public int getMath() { return math; } /** * @param math the math to set */ public void setMath(int math) { this.math = math; } /** * @return the english */ public int getEnglish() { return english; } /** * @param english the english to set */ public void setEnglish(int english) { this.english = english; } /** * @return the chinses */ public int getChinses() { return chinses; } /** * @param chinses the chinses to set */ public void setChinses(int chinses) { this.chinses = chinses; } /** * @return the sumGrade */ public int getSumGrade() { return english+math+chinses; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Student" + name + ",math=" + math + ",english=" + english + ",chinses=" + chinses +",SumGrade"+getSumGrade()+ "]"; } @Override public int compareTo(Student o) { int temp = getSumGrade()-o.getSumGrade(); return temp==0?name.compareTo(o.name):temp; } }来自: iPhone客户端 |