黑马程序员技术交流社区

标题: IO流异常处理 [打印本页]

作者: 413520953    时间: 2015-11-10 00:20
标题: IO流异常处理
public class IO流异常处理 {
        public void test1() {
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try {
                        bis = new BufferedInputStream(new FileInputStream("demo4.jpg"));
                        bos = new BufferedOutputStream(new FileOutputStream("demo44.jpg"));
                        int len = -1;
                        byte[] buf = new byte[1024];
                        while ((len = bis.read(buf)) != -1) {
                                bos.write(buf, 0, len);
                                bos.flush();
                        }
                        ;
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        try {
                                if (bos != null) {
                                        bos.close();
                                }
                        } catch (IOException e) {
                                bos = null;
                                e.printStackTrace();
                        }
                        try {
                                if (bis == null) {
                                        return;
                                }
                                bis.close();
                        } catch (Exception e2) {
                                bis = null;
                                e2.printStackTrace();
                        }
                }

        }
}





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