java IO流 複製圖片,javaio流圖片

來源:互聯網
上載者:User

java IO流 複製圖片,javaio流圖片

(一)使用位元組流複製圖片

 1 //位元組流方法 2     public static void copyFile()throws IOException { 3          4         //1.擷取目標路徑 5         //(1)可以通過字串 6 //        String srcPath = "C:\\Users\\bigerf\\Desktop\\筆記\\11.jpg"; 7 //        String destPath = "C:\\Users\\bigerf\\Desktop\\圖片備份\\11.jpg"; 8          9         //(2)通過檔案類10         File srcPath = new File("C:\\Users\\bigerf\\Desktop\\筆記\\22.PNG");11         File destPath = new File("C:\\Users\\bigerf\\Desktop\\圖片備份\\22.PNG");12         13         //2.建立通道,依次 開啟輸入資料流,輸出資料流14         FileInputStream fis = new FileInputStream(srcPath);15         FileOutputStream fos = new FileOutputStream(destPath);16 17         byte[] bt = new byte[1024];18         19         //3.讀取和寫入資訊(邊讀取邊寫入)20         while (fis.read(bt) != -1) {//讀取21             fos.write(bt);//寫入22         }23 24         //4.依次 關閉流(先開後關,後開先關)25         fos.close();26         fis.close();27     }

 

(二)使用字元流複製檔案

 1 //字元流方法,寫入的資料會有丟失 2     public static void copyFileChar()throws IOException { 3          4         //擷取目標路徑 5         File srcPath = new File("C:\\Users\\bigerf\\Desktop\\筆記\\33.PNG"); 6         File destPath = new File("C:\\Users\\bigerf\\Desktop\\圖片備份\\33.PNG"); 7          8         //建立通道,依次 開啟輸入資料流,輸出資料流 9         FileReader frd = new FileReader(srcPath);10         FileWriter fwt = new FileWriter(destPath);11 12         char[] ch = new char[1024];13         int length = 0;14         // 讀取和寫入資訊(邊讀取邊寫入)15         while ((length = frd.read(ch)) != -1) {//讀取16             fwt.write(ch,0,length);//寫入17             fwt.flush();18         }19 20         // 依次 關閉流(先開後關,後開先關)21         frd.close();22         fwt.close();23     }

 

(三)以複製圖片為例,實現拋出異常案例

 1     //以複製圖片為例,實現try{ }cater{ }finally{ } 的使用 2         public static void test(){ 3             //1.擷取目標路徑 4             File srcPath = new File("C:\\Users\\bigerf\\Desktop\\筆記\\55.PNG"); 5             File destPath = new File("C:\\Users\\bigerf\\Desktop\\圖片備份\\55.PNG"); 6             //2.建立通道,先賦空值 7             FileInputStream fis = null; 8             FileOutputStream fos = null; 9             //3.建立通道時需要拋出異常10             try {11                 fis = new FileInputStream(srcPath);12                 fos = new FileOutputStream(destPath);13                 14                 byte[] bt = new byte[1024];15                 //4.讀取和寫入資料時需要拋出異常16                 try {17                     while(fis.read(bt) != -1){18                         fos.write(bt);19                     }20                 } catch (Exception e) {21                     System.out.println("儲存檔異常,請修理");22                     throw new RuntimeException(e);23                 }24                 25                 26             } catch (FileNotFoundException e) {27                 System.out.println("資源檔不存在");28                 throw new RuntimeException(e); 29             }finally{30                 31                 //5.無論有無異常,需要關閉資源(分別拋出異常)32                 try {33                     fos.close();34                 } catch (Exception e) {35                     System.out.println("資源檔或目標檔案關閉失敗!");36                     throw new RuntimeException(e);37                 }38                 39                 try {40                     fis.close();41                 } catch (IOException e) {42                     System.out.println("資源檔或目標檔案關閉失敗!");43                     throw new RuntimeException(e);44                 }45                 46             }47         }

字元流  =  位元組流 + 解碼 --->找對應的碼錶  GBK

  字元流解碼 : 拿到系統預設的編碼方式來解碼

將圖片中的位元據和GBK碼錶中的值進行對比, 對比的時候會出現二進位檔案在碼錶中找不對應的值,他會將位元據標記為未知字元,當我在寫入資料的是後會將未知的字元丟掉。所以會造成圖片拷貝不成功(遺失資料)

疑問:何時使用位元組流?何時使用字元流?

  使用位元組流的情境:讀寫的資料不需要轉為我能夠看得懂的字元。比如:圖片,視頻,音頻...

  使用字元流的情境 :如果讀寫的是字元資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.