位元組流複製檔案 位元組陣列 緩衝
阿新 • • 發佈:2019-02-09
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); } }