JavaSE基礎--檔案的複製
阿新 • • 發佈:2021-01-21
package com.Day19.StudyTest; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; /** * 檔案的複製(舉例:圖片複製) * 分析:建立輸入流和輸出流 * 輸入流讀取圖片資訊寫入 * 設定緩衝區 * 輸出流寫出到指定路徑 * 資源釋放 * */ public class Demo4 { public static voidmain(String[] args) throws FileNotFoundException { //建立流物件 FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("F:\\ff.jpg"); fos = new FileOutputStream("D:\\mm.jpg"); //建立緩衝區 byte[] buffer = newbyte[1024]; int len ; while ((len = fis.read(buffer)) != -1){ //把buffer的資料寫入到fos物件的指定路徑 fos.write(buffer,0,len); } } catch (Exception e){ System.out.println(e.getMessage()); }finally { try{ if(fis != null){ fis.close(); } if(fos != null){ fos.close(); } } catch (Exception e){ System.out.println(e.getMessage()); } } System.out.println("照片有兩張了耶!"); } }