1. 程式人生 > >四種復制文件的方法

四種復制文件的方法

打開 buffere sta close style throws != admin bis

  1 package com.hxl;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 
  9 public class Test {
 10     public static void main(String[] args) throws IOException {
11 long start = System.currentTimeMillis(); 12 // 測試文件大小為3.12MB 13 // 運行耗時:21707毫秒 14 method1("E:\\EditPlus4.3安裝程序及說明.zip", "C:\\Users\\Administrator\\Desktop\\EditPlus4.3安裝程序及說明.zip"); 15 // 運行耗時:76毫秒 16 // method2("E:\\EditPlus4.3安裝程序及說明.zip", "C:\\Users\\Administrator\\Desktop\\EditPlus4.3安裝程序及說明.zip");
17 // 運行耗時:294毫秒 18 // method3("E:\\EditPlus4.3安裝程序及說明.zip", "C:\\Users\\Administrator\\Desktop\\EditPlus4.3安裝程序及說明.zip"); 19 // 運行耗時:54毫秒 20 // method4("E:\\EditPlus4.3安裝程序及說明.zip", "C:\\Users\\Administrator\\Desktop\\EditPlus4.3安裝程序及說明.zip"); 21 long end = System.currentTimeMillis();
22 System.out.println("運行耗時:" + (end - start) + "毫秒"); 23 } 24 25 // 基本字節流一次讀取一個字節復制文件 26 public static void method1(String src, String dest) throws IOException { 27 // 數據源 28 FileInputStream fis = new FileInputStream(src); 29 // 目的位置 30 FileOutputStream fos = new FileOutputStream(dest, true); 31 // read()方法返回值為獲取到的那個字符的ASCII碼值,若沒有獲取到,則返回-1 32 int byt = 0; 33 while ((byt = fis.read()) != -1) { 34 // write(int 35 // b)方法指的是將字節數據(主要因為read的返回值範圍是0~255和-1,而write此時傳參若為byte顯然不合適了)寫入(復制到)文件 36 // 經測試知,傳-1顯示了一段空格(不是Tab建),可以傳0~255和-1以外的int值,但用記事本打開是亂碼(以‘?‘表示) 37 fos.write(byt); 38 } 39 // 關閉輸出流 40 fos.close(); 41 // 關閉輸入流 42 fis.close(); 43 } 44 45 // 基本字節流一次讀取一個字節數組復制文件 46 public static void method2(String src, String dest) throws IOException { 47 // 數據源 48 FileInputStream fis = new FileInputStream(src); 49 // 目的位置 50 FileOutputStream fos = new FileOutputStream(dest); 51 // 定義單次讀取字節數組的大小,一般就寫1024 52 byte[] bys = new byte[1024]; 53 // read(byte[] bys)方法返回值為獲取到的字節個數,若沒有獲取到,則返回-1 54 int length = 0; 55 while ((length = fis.read(bys)) != -1) { 56 // write(byte[] bys,int off,int length)方法指的是從指定字節數組的指定位置開始寫入(復制到)文件 57 fos.write(bys, 0, length); 58 } 59 // 關閉輸出流 60 fos.close(); 61 // 關閉輸入流 62 fis.close(); 63 } 64 65 // 高效字節流一次讀取一個字節復制文件 66 public static void method3(String src, String dest) throws IOException { 67 // 數據源 68 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); 69 // 目的位置 70 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest)); 71 // read()方法返回值為獲取到的那個字符的ASCII碼值,若沒有獲取到,則返回-1 72 int byt = 0; 73 while ((byt = bis.read()) != -1) { 74 // write(int b)方法指的是將字節數據(主要因為read的返回值範圍是0~255和-1,而write此時傳參若為byte顯然不合適了)寫入(復制到)文件 75 // 經測試知,傳-1顯示了一段空格(不是Tab建),可以傳0~255和-1以外的int值,但用記事本打開是亂碼(以‘?‘表示) 76 bos.write(byt); 77 } 78 // 關閉輸出流 79 bos.close(); 80 // 關閉輸入流 81 bis.close(); 82 } 83 84 // 高效字節流一次讀取一個字節數組復制文件 85 public static void method4(String src, String dest) throws IOException { 86 // 數據源 87 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); 88 // 目的位置 89 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest)); 90 // 定義單次讀取字節數組的大小,一般就寫1024 91 byte[] bys = new byte[1024]; 92 // read(byte[] bys)方法返回值為獲取到的字節個數,若沒有獲取到,則返回-1 93 int length = 0; 94 while ((length = bis.read(bys)) != -1) { 95 // write(byte[] bys,int off,int length)方法指的是從指定字節數組的指定位置開始寫入(復制到)文件 96 bos.write(bys, 0, length); 97 } 98 // 關閉輸出流 99 bos.close(); 100 // 關閉輸入流 101 bis.close(); 102 } 103 }

四種復制文件的方法