黑马程序员技术交流社区

标题: IO流中异常处理 [打印本页]

作者: 王海龙2013    时间: 2013-4-20 10:38
标题: IO流中异常处理
本帖最后由 王海龙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. }
复制代码

作者: 王海龙2013    时间: 2013-4-20 17:39
晕了,没有人了
作者: JavaUtil    时间: 2013-4-20 17:57
不是没人,是不知你要问什么。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2