package cn.niit.demo10; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* 字节流复制文件 字节数组 缓冲 */ public class Copy { //字节流复制文件 public static void main(String[] args){ long s=System.currentTimeMillis(); FileInputStream fis=null; FileOutputStream fos=null; try { fis=new FileInputStream("c:\\a.rar"); fos=new FileOutputStream("d:\\a.rar"); //定义字节数组,缓冲 byte[] bytes=new byte[1024]; //读取数组,写入数组 int len=0; while ((len=fis.read(bytes))!=-1){ fos.write(bytes,0,len); } }catch (IOException e){ System.out.println(e); throw new RuntimeException("文件复制失败!"); }finally { try { if (fos!=null) { fos.close(); } }catch (IOException e){ throw new RuntimeException("释放资源失败"); }finally{ try { if (fis!=null) fis.close(); }catch (IOException e){ throw new RuntimeException("释放资源失败"); } } } long e=System.currentTimeMillis(); System.out.println(e-s); } }
字节流复制文件 字节数组 缓冲
最新推荐文章于 2024-05-06 09:29:19 发布