| 嗯,按这个方法已经解决,就是感觉有点麻烦
 package Test2;
 
 /** 有五格学生,每个学生有三门的课程
 * 从键盘输入以上数据(包括姓名,三门课成绩)
 * 输入的格式:如:zhangsan,30,40,50 计算出总成绩
 * 并把学生的信息和计算出的总分数,高低顺序存放在磁盘文件“stud.txt”中
 *
 * 1、描述学生对象
 * 2、定义一个可以操作学生对象的工具类
 */
 import java.util.*;
 import java.io.*;
 public class studentInfoRecord {
 
 public static void main(String[] args) {
 //利用学生工具来创建学生信息文档
 new studentTool("e:\\studentIoform.txt");
 
 }
 
 }
 //创建学生工具类实现将从键盘输入的学生信息写入指定的文件中
 class studentTool{
 public studentTool(String string){
 Set<student2> InformationSet=new TreeSet<student2>();
 Set<String> nameSet=new TreeSet<String>();
 BufferedReader bfr1=null;
 //若所存文件已经存在,则把数据中的名字取出读入到名字集合nameSet中,把所有学生信息取出到集合InformationSet中,以便重名时修改分数
 File file=null;
 try {
 file=new File(string);
 if(file!=null){
 bfr1=new BufferedReader(new FileReader(file));
 String str=null;
 while((str=bfr1.readLine())!=null){
 String[] by1=str.split(",");
 nameSet.add(by1[0]);
 InformationSet.add(new student2(by1[0]
 ,Integer.parseInt(by1[1])
 ,Integer.parseInt(by1[2])
 ,Integer.parseInt(by1[3])));
 }
 }
 } catch (Exception e2) {
 new RuntimeException("读取文件信息失败");
 } finally{
 if(bfr1!=null)
 try {
 bfr1.close();
 } catch (Exception e) {
 new RuntimeException("文件读取流关闭失败");
 }
 }
 //载入原来文本上的信息后,添加后来键盘输入的信息
 BufferedReader bfr=null;
 System.out.println("请输入学生信息:格式如下:学生名字,语文,数学,英语");
 System.out.println("回车输入下一个学生信息,输入over回车则停止输入");
 try {
 //建立输入流指向键盘输入
 bfr=new BufferedReader(new InputStreamReader(System.in));
 //只要未接收到结束符号“over”,循环监听键盘,不断添加学生对象
 while(true){
 String s=bfr.readLine();
 if("over".equals(s))
 break;
 else {
 //将输入的信息以","拆分后,检查名字集合中是否包含此名字
 String[] strs=s.split(",");
 //若有相同名字,则迭代取出信息集合的所有学生,修改相同名字的学生的信息
 if(nameSet.contains(strs[0])){
 Iterator<student2> it=InformationSet.iterator();
 while(it.hasNext()){
 student2 stu1=it.next();
 String str1=stu1.toString();
 String[] strs1=str1.split(",");
 if(strs1[0].equals(strs[0])){
 InformationSet.remove(stu1);
 stu1=new student2(strs[0]
 ,Integer.parseInt(strs[1])
 ,Integer.parseInt(strs[2])
 ,Integer.parseInt(strs[3]));
 InformationSet.add(stu1);
 }
 
 }
 
 }
 //若没有相同名字,则直接添加
 else{
 InformationSet.add(new student2(strs[0]
 ,Integer.parseInt(strs[1])
 ,Integer.parseInt(strs[2])
 ,Integer.parseInt(strs[3])));
 }
 }
 
 }
 
 } catch (Exception e2) {
 new IOException("输入失败");
 }
 
 BufferedWriter bfw=null;
 try {
 //建立流出流,指向主函数指定的文件
 bfw=new BufferedWriter(new FileWriter(string));
 Iterator<student2> it=InformationSet.iterator();
 //往对应的文件中输出学生对象信息,每输入一个学生对象刷新一次
 while(it.hasNext()){
 student2 stu=it.next();
 bfw.write(stu.toString());
 bfw.newLine();
 bfw.flush();
 System.out.println(stu);
 }
 System.out.println("保存成功");
 } catch (Exception e) {
 new IOException("写入失败");
 //finally中关闭输出流
 } finally{
 if(bfw!=null)
 try {
 bfw.close();
 } catch (Exception e2) {
 new IOException("写入流关闭失败");
 }
 }
 }
 }
 //建立学生类,实现Comparable接口
 class student2 implements Comparable<student2>{
 private String name;
 private int chinese,math,English;
 private int total;
 student2(String name,int x,int y,int z){
 this.name=name;
 this.chinese=x;
 this.math=y;
 this.English=z;
 total=x+y+z;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getX() {
 return chinese;
 }
 public void setX(int x) {
 this.chinese = x;
 }
 public int getY() {
 return math;
 }
 public void setY(int y) {
 this.math = y;
 }
 public int getZ() {
 return English;
 }
 public void setZ(int z) {
 this.English = z;
 }
 public int getTotal() {
 return total;
 }
 //复写hashCode方法
 @Override
 public int hashCode(){
 return this.name.hashCode()+this.total*39;
 }
 //复写equals方法
 @Override
 public boolean equals(Object obj){
 if(!(obj instanceof student2))
 throw new RuntimeException("学生信息不正确");
 else{
 student2 ss=(student2)obj;
 return this.name.equals(ss.name)&this.total==ss.total;
 }
 }
 
 //复写compareTo方法,以总分排序,若总分相同则以名字排序
 @Override
 public int compareTo(student2 stu) {
 if(this.total>stu.total)
 return -1;
 else if(this.total<stu.total)
 return 1;
 else
 return this.name.compareTo(stu.name);
 }
 //复写toString方法
 @Override
 public String toString(){
 return this.name+","+this.chinese+","+this.math+","+this.English+",total="+this.total;
 }
 }
 |