黑马程序员技术交流社区

标题: 【成都校区】IO流异常处理 [打印本页]

作者: 迷茫的中年大叔    时间: 2018-11-22 12:59
标题: 【成都校区】IO流异常处理
    1. JDK7之前的处理

            FileInputStream fis = null;
            FileOutputStream fos = null;
            try{
            fis = new FileInputStream("美女.jpg");
            fos = new FileOutputStream("d:\\meinv.jpg");
   
            byte[] bys = new byte[1024];
            int  len;
            while ((len = fis.read(bys)) != -1) {
                fos.write(bys,0,len);
            }

            } catch (IOException e) {
                   
            } finally {
                    try {
                            if(fis!=null)
                                       fis.close();
                    } catch (IOException e) {
                   
                    }       
                    try {
                            if(fos!=null)
                                       fos.close();
                    } catch (IOException e) {
                   
                    }       
            
            }
   
    2. JDK7之后的处理
           
            try(
            FileInputStream fis = new FileInputStream("美女.jpg");
            FileOutputStream fos = new FileOutputStream("d:\\meinv.jpg");
            ) {
           
                    byte[] bys = new byte[1024];
            int  len;
            while ((len = fis.read(bys)) != -1) {
                fos.write(bys,0,len);
            }
            } catch (IOException e) {
                   
            }       





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