一、这个案例的作用?
将网盘文件中的内容存储解析到内存中,然后分装到Student1类中。
public static void main(String[] args) throws IOException, ParseException {
//定义File对象
File f1=new File("C:\\aa\\a.txt");
//字符输入流Reader
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
//使用list集合对Student1进行操作
ArrayList<Student1> list=new ArrayList<>();
//将日期字符类型转换成date类型
SimpleDateFormat sbf=new SimpleDateFormat("yyyy/MM/dd");
// 使用FileReader对象中的方法readLine读取文件
String stem;
while((stem=br.readLine())!=null){ // 读到末尾
//使用分割符分割进行封装
String[] spilt=stem.split("-");
Student1 student1=new Student1();
student1.setName(spilt[0]);
student1.setSex(spilt[1]);
student1.setBirthday(sbf.parse(spilt[2]));
student1.setAddress(spilt[3]);
list.add(student1);
}
System.out.println(list);
}
二、抛异常
1.编译时异常
编译过程中发生的错误,编译时提示的异常
2.运行时异常
运行时代码发生的异常
3.空指针异常
返回(return)的数据为null
捕获异常
使用 try 和 catch 关键字可以捕获异常。try/catch 代码块放在异常可能发生的地方。
try/catch代码块中的代码称为保护代码
try
{
// 程序代码
}catch(ExceptionName e1)
{
//Catch 块
}