本帖最后由 王海龙2013 于 2013-4-20 23:55 编辑
毕老师没有说怎么处理,这样行不啊- class StudentInfoTool
- {
- //输入学生信息,存入集合
- public static Set<Student>getStudents()
- {
- BufferedReader bufr =
- new BufferedReader(new InputStreamReader(System.in));
- String line = null;
-
- Set<Student> stus = new TreeSet<Student>();
- try
- {
- while((line = bufr.readLine())!=null)
- {
- if("over".equals(line))
- break;
-
- String[] info = line.split(",");
-
- Student stu = new Student(info[0],Integer.parseInt(info[1]),
- Integer.parseInt(info[2]),Integer.parseInt(info[3]));
- stus.add(stu);
- }
- return stus;
-
- }
- catch (IOException e)
- {
- throw new RuntimeException("学生信息输入失败");
- }
- finally
- {
-
- try
- {
- bufr.close();
- } catch (IOException e)
- {
- throw new RuntimeException("关闭流失败");
- }
- }
-
- }
- //集合中学生信息存入文件
- public static void writeToFile(Set<Student> stus)
- {
- BufferedWriter bufw = null;
- try
- {
- bufw = new BufferedWriter
- (new FileWriter("stu.txt"));
-
- for(Student stu : stus)
- {
-
- bufw.write(stu.toString()+"\t");
-
- bufw.newLine();
- bufw.flush();
-
- }
-
- }
- catch (IOException e)
- {
- throw new RuntimeException("学生信息存入失败");
- }
- finally
- {
- if(bufw!=null)
- try
- {
- bufw.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("关闭流失败");
- }
- }
- }
- }
复制代码 |