保存学生信息到文件(数组内元素排序, io操作)

来源:互联网 发布:php中文网站模板下载 编辑:程序博客网 时间:2024/06/15 00:08

今天的视频里毕老师又出新花样了, 要求输入学生信息, 包括姓名, 数学成绩,语文成绩,英语成绩, 然后算出总分, 根据总分排序,  然后可以把学生信息保存到文件.

毕老师用set写的,  我换成ArrayList试了一下, 在实现comparable接口的地方出现了点问题, 不知道该重写什么方法, 重写完了不知道该调用什么方法排序, 后来又是百度又是问人的, 总算是写出了, 还得请人家喝瓶可乐.


package com.day21;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;import com.day20.p;     //这个class是封装System.out.println()方法的.这个太长了, 你懂得.public class stuuuuu {/** * @param args * @author liuyue */public static void main(String[] args) {// TODO Auto-generated method stubFile dir = new File("D:\\test\\stus.txt");ArrayList<student> stus = StudentTools.getStudent();//Collections.sort(stus);//这句是排序, 可以让stus按照总分升序(由小到大)排列, //噢对了, 实现了comparable的类的集合使用Collections.sort()来排序.不是Array.sort(), 也不是 stus.sort().//Collections.reverse(stus);//这句是反转stus里的元素, 执行完上一句后是升序, 到了这反转后就成了降序Comparator<student> com = Collections.reverseOrder();//定义一个新的排序器, 排序的规则与实现comparable类的compareto()的规则相反.Collections.sort(stus, com);//同样是排序, 但是是按照com的规则来排序StudentTools.save(stus, dir);}static class student implements Comparable<student>{  //实现了Comparable的类, 此类的集合可以方便的排序private String name;private double math,cn,en,sum;student(String name,double math,double cn,double en){this.name=name;this.math=math;this.cn=cn;this.en=en;this.sum=math+cn+en;}public double getSum(){return this.sum;}public String toString(){return "name: "+name+"math: "+math+",cn: "+cn+",en:"+en+",sum:"+sum;}@Overridepublic int compareTo(student o) {//实现了Comparable的类必须要重写compareTo()方法// TODO Auto-generated method stubint num=new Integer((int) this.sum).compareTo(new Integer((int)o.sum));if (num == 0){return this.name.compareTo(o.name);}return num;}@Overridepublic boolean equals(Object obj) {//重写equals()方法// TODO Auto-generated method stubif(!(obj instanceof student)){//如果与非student类型比较,抛异常throw new ClassCastException("类型不匹配");}student s = (student)obj;return s.name.equals(this.name)&&s.sum==this.sum;//姓名和总分一样则认为是同一个student}}//输入学生信息的类, 用毕老师的话说就是没有技术含量, 不必多说.static class StudentTools{static ArrayList<student> getStudent(){ArrayList<student> stus = new ArrayList<student>();String len;p.pln("请输入学生信息,用英文逗号分割;示例:张三,100,100,100");BufferedReader br = new BufferedReader(new InputStreamReader(System.in));try {while((len=br.readLine())!=null){if (len.equals("over")){p.pln("正在保存您输入的学生信息...");break;}String[] info=len.split(",");//要求输入的格式为"name.math,cn,en",所以使用逗号分割获取的每一行信息student s = new student(info[0], Integer.parseInt(info[1]), Integer.parseInt(info[2]), Integer.parseInt(info[3]));stus.add(s);}}catch (NumberFormatException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {br.close();}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return stus;}        //此方法原来另写在一个新的类里, 后来感觉没必要, 就合并到StudentsTools类里了.static void save(ArrayList<student> stus, File dir){BufferedWriter bw=null;try {bw = new BufferedWriter(new FileWriter(dir));for(student s:stus){bw.write(s.toString());bw.newLine();bw.flush();}}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {bw.close();p.pln("保存完毕!");p.pln("请打开"+dir.getAbsolutePath()+"文件来查看结果.");}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}//保存学生信息的类, 好奇怪, 明明可以放到上边那个类里. 好, 说放就放.//static class saveTheStus{//private static ArrayList<student> stus = new ArrayList<student>();//private static File dir = null;//saveTheStus(ArrayList<student> stus, File dir){//this.stus=stus;//this.dir=dir;//}////void save(){////BufferedWriter bw=null;////try {//bw = new BufferedWriter(new FileWriter(dir));//for(student s:stus){//p.pln(s);//bw.write(s.toString());//bw.newLine();//bw.flush();//}//}//catch (IOException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}//finally{//try {//bw.close();//p.pln("保存完毕!");//}//catch (IOException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}//}//}//}}