黑马程序员技术交流社区
标题:
IO流中异常处理
[打印本页]
作者:
王海龙2013
时间:
2013-4-20 10:38
标题:
IO流中异常处理
本帖最后由 王海龙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("关闭流失败");
}
}
}
}
复制代码
作者:
王海龙2013
时间:
2013-4-20 17:39
晕了,没有人了
作者:
JavaUtil
时间:
2013-4-20 17:57
不是没人,是不知你要问什么。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2