Java實現本地 fileCopy

來源:互聯網
上載者:User

標籤:

前言:

  Java中流是重要的內容,基礎的檔案讀寫與拷貝知識點是很多面試的考點。故通過本文進行簡單測試總結。

 

2.圖展示【文本IO/二進位IO】(這是參考自網上的一張總結圖,相當經典,方便對比記憶)

 

3.文本複製的實驗Java實現code:
  1 package com.gdufe.io;  2   3 import java.io.BufferedInputStream;  4 import java.io.BufferedOutputStream;  5 import java.io.DataInputStream;  6 import java.io.DataOutputStream;  7 import java.io.FileInputStream;  8 import java.io.FileOutputStream;  9 import java.io.IOException; 10 import java.io.InputStream; 11 import java.io.OutputStream; 12  13 public class TestInputOutputStream { 14  15 //    private final static String SOURCE="t.txt"; 16 //    private final static String TARGET="tt.txt"; 17 //    private final static String SOURCE="p.png"; 18 //    private final static String TARGET="pp.png"; 19     private final static String SOURCE="D:\\Tool_Software\\mysql-installer-community-5.6.23.0.msi"; 20     private final static String TARGET="mysql-installer-community-5.6.23.0.msi"; 21     //D:\Tool_Software\Ryuyan.zip 22      23      24     public static void main(String[] args) { 25 //        TestInputOutputStream.copy1(SOURCE, TARGET); 26 //        TestInputOutputStream.copy2(SOURCE, TARGET); 27         TestInputOutputStream.copy3(SOURCE, TARGET); 28         System.out.println("--End---"); 29     } 30      31     public static void copy1(String src,String tar){ 32          33         InputStream input = null; 34         OutputStream output = null; 35         try{ 36             input = new FileInputStream(src); 37             output = new FileOutputStream(tar); 38             int r; 39             while((r=input.read())!=-1){ 40                 output.write((byte)r); 41             } 42         }catch(Exception e){ 43             e.printStackTrace(); 44         }finally{ 45             try { 46                 input.close(); 47                 output.close(); 48             } catch (IOException e) { 49                 e.printStackTrace(); 50             } 51         } 52          53     } 54     public static void copy2(String src,String tar){ 55         InputStream input = null; 56         OutputStream output = null; 57         try{ 58             input = new FileInputStream(src); 59             output = new FileOutputStream(tar); 60             int length=0; 61             byte []b  = new byte[1024]; 62             while((length=input.read(b))!=-1){ 63                 output.write(b,0,length); 64             } 65         }catch(Exception e){ 66             e.printStackTrace(); 67         }finally{ 68             try { 69                 input.close(); 70                 output.close(); 71             } catch (IOException e) { 72                 e.printStackTrace(); 73             } 74         } 75     } 76      77     public static void copy3(String src,String tar){ 78         InputStream input = null; 79         OutputStream output = null; 80         try{ 81             input = new DataInputStream(new BufferedInputStream(new FileInputStream(src))); 82             output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tar))); 83             /***經過親測,buffer緩衝流讀取時確實更快一些****/ 84              85             int length=0; 86             byte []b  = new byte[1024];    //先將stream讀入位元組數組 87             while((length=input.read(b))!=-1){ 88                 output.write(b,0,length); 89             } 90         }catch(Exception e){ 91             e.printStackTrace(); 92         }finally{ 93             try { 94                 input.close(); 95                 output.close(); 96             } catch (IOException e) { 97                 e.printStackTrace(); 98             } 99         }100     }101 }

 

(附:參考代碼時注意準備好測試檔案,不然出現異常“file can‘t be found”!

  檔案拷貝到Java工程的直接目錄下,重新整理project可查看!

)

Java實現本地 fileCopy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.