jdk1.6及之前版本IO流异常处理标准代码

来源:互联网 发布:淘宝卖家体检中心 编辑:程序博客网 时间:2024/06/06 04:17
public void demo() throws FileNotFoundException, IOException {  // 抛出异常    FileInputStream fis = null;  // 赋初值    FileOutputStream fos = null;    try {        fis = new FileInputStream("a.txt");        fos = new FileOutputStream("b.txt");        byte[] arr = new byte[1024*8];  // 8192Bytes是有讲究的        int len;        while((len = fis.read(arr)) != -1) {            fos.write(arr,0,len);        }    }finally {        try{   // try...finally...嵌套是为了防止关闭流时出现异常            if(fis != null)                fis.close();        }finally {                      if(fos != null)                fos.close();        }    }}
0 0