用try...catch...处理流异常

mac2024-07-16  46

用try…catch…方法可以自行捕获异常并处理,以让复制数据正常写入; 最后将流都关闭;

FileInputStream in = null; FileOutputStream out = null; try { File file = new File("G:\\oracle视频\\韩顺平.玩转oracle第0讲.开山.wmv"); in = new FileInputStream(file); out = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test"+"\\"+file.getName()); int len=0;//记录读取到的有效字节个数 //创建一个缓冲区 byte[] bytes = new byte[1024 * 1024 * 100]; while ((len=in.read(bytes))!=-1){ out.write(bytes,0,len); out.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { //释放资源 try { if (out!=null){out.close();} if (in!=null){ in.close();} } catch (IOException e) { e.printStackTrace(); } } System.out.println("复制完成");
最新回复(0)