String Student File 案例及抛异常

一、这个案例的作用?

将网盘文件中的内容存储解析到内存中,然后分装到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 块
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值