A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王海龙2013 中级黑马   /  2013-4-20 10:38  /  1035 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王海龙2013 于 2013-4-20 23:55 编辑

毕老师没有说怎么处理,这样行不啊
  1. class StudentInfoTool
  2. {
  3.         //输入学生信息,存入集合
  4.         public static Set<Student>getStudents()
  5.         {
  6.                 BufferedReader bufr =
  7.                         new BufferedReader(new InputStreamReader(System.in));

  8.                 String line = null;
  9.                
  10.                 Set<Student> stus = new TreeSet<Student>();
  11.                 try
  12.                 {
  13.                         while((line = bufr.readLine())!=null)
  14.                         {
  15.                                 if("over".equals(line))
  16.                                         break;
  17.                                 
  18.                                 String[] info = line.split(",");
  19.                                 
  20.                                 Student stu = new Student(info[0],Integer.parseInt(info[1]),
  21.                                                                 Integer.parseInt(info[2]),Integer.parseInt(info[3]));
  22.                                 stus.add(stu);
  23.                         }
  24.                         return stus;
  25.                         
  26.                 }
  27.                 catch (IOException e)
  28.                 {
  29.                         throw new RuntimeException("学生信息输入失败");
  30.                 }
  31.                 finally
  32.                 {
  33.                         
  34.                         try
  35.                         {
  36.                                 bufr.close();
  37.                         } catch (IOException e)
  38.                         {

  39.                                 throw new RuntimeException("关闭流失败");
  40.                         }                        
  41.                 }
  42.                
  43.         }
  44.         //集合中学生信息存入文件
  45.         public static void writeToFile(Set<Student> stus)
  46.         {
  47.                 BufferedWriter bufw = null;
  48.                 try
  49.                 {
  50.                         bufw = new BufferedWriter
  51.                                                         (new FileWriter("stu.txt"));
  52.         
  53.                         for(Student stu : stus)
  54.                         {
  55.                                 
  56.                                 bufw.write(stu.toString()+"\t");
  57.                                 
  58.                                 bufw.newLine();
  59.                                 bufw.flush();
  60.                                 
  61.                         }
  62.                                                 
  63.                 }
  64.                 catch (IOException e)
  65.                 {
  66.                         throw new RuntimeException("学生信息存入失败");        
  67.                 }
  68.                 finally
  69.                 {
  70.                         if(bufw!=null)
  71.                                 try
  72.                                 {
  73.                                         bufw.close();
  74.                                 }
  75.                                 catch (IOException e)
  76.                                 {
  77.                                         throw new RuntimeException("关闭流失败");
  78.                                 }
  79.                 }               
  80.         }        
  81. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

2 个回复

倒序浏览
晕了,没有人了
回复 使用道具 举报
不是没人,是不知你要问什么。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马