Java-IO(各種流的拷貝)
阿新 • • 發佈:2022-04-10
拷貝
- 元資料——>目的地
拷貝方式:
- 單個 位元組/字元 的拷貝
- 按照 位元組/字元 陣列拷貝
測試
- 單個位元組和位元組陣列讀取時間
- 非緩衝與帶緩衝的讀取時間
package com.bigdat.java.day25; import java.io.*; /* 測試 單個位元組和位元組陣列讀取時間 非緩衝與帶緩衝的讀取時間 */ public class CopyDemo2 { public static void main(String[] args) { // oneTest(); 單個位元組讀取視屏的時間為2107904毫秒 // shuZuTest(); 陣列讀取視屏的時間為2392毫秒 // OneBufferText(); 緩衝單位元組讀取視屏的時間為:9503毫秒 // shuzuBufferText(); 緩衝位元組陣列讀取視屏的時間為:547毫秒 } //測試單個字元讀取一段視屏的時間 public static void oneTest(){ //建立讀取物件 FileInputStream fileInputStream = null; //建立寫出物件 FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream("D:\\我被改名了\\17day24 位元組流速度對比,編碼表 2.mp4"); fileOutputStream = new FileOutputStream("D:\\我被改名了\\copy1.mp4"); Long start = System.currentTimeMillis(); //開始讀取 int len = 0; while((len = fileInputStream.read()) != -1){ fileOutputStream.write(len); } long end = System.currentTimeMillis(); System.out.println("單個位元組讀取視屏的時間為"+(end-start)+"毫秒"); } catch (IOException e) { e.printStackTrace(); }finally { try { fileOutputStream.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } //測試位元組陣列copy視屏的時間 public static void shuZuTest(){ //建立讀取物件 FileInputStream fileInputStream = null; //建立寫出物件 FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream("D:\\我被改名了\\17day24 位元組流速度對比,編碼表 2.mp4"); fileOutputStream = new FileOutputStream("D:\\我被改名了\\copy2.mp4"); Long start = System.currentTimeMillis(); //開始讀取 int len = 0; byte[] bytes = new byte[1024]; while((len = fileInputStream.read(bytes)) != -1){ fileOutputStream.write(bytes, 0, len); } long end = System.currentTimeMillis(); System.out.println("陣列讀取視屏的時間為"+(end-start)+"毫秒"); } catch (IOException e) { e.printStackTrace(); }finally { try { fileOutputStream.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } //緩衝流單個位元組讀取視屏測試時間 public static void OneBufferText(){ //建立讀入位元組的物件 BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try { bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\我被改名了\\17day24 位元組流速度對比,編碼表 2.mp4")); bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D:\\我被改名了\\copy3.mp4")); //開始時間 long start = System.currentTimeMillis(); //開始寫入到程式中 int len = 0; while((len = bufferedInputStream.read()) != -1){ bufferedOutputStream.write(len); } long end = System.currentTimeMillis(); System.out.println("緩衝單位元組讀取視屏的時間為:"+(end-start)); } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedOutputStream.close(); bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void shuzuBufferText(){ //建立讀入位元組的物件 BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try { bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\我被改名了\\17day24 位元組流速度對比,編碼表 2.mp4")); bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D:\\我被改名了\\copy4.mp4")); //開始時間 long start = System.currentTimeMillis(); //開始寫入到程式中 int len = 0; byte[] bytes = new byte[1024]; while((len = bufferedInputStream.read(bytes)) != -1){ bufferedOutputStream.write(bytes, 0, len); } long end = System.currentTimeMillis(); System.out.println("緩衝位元組陣列讀取視屏的時間為:"+(end-start)+"毫秒"); } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedOutputStream.close(); bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
package com.bigdat.java.day25; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* 將 D盤中的p.txt 檔案拷貝一份到 D 盤中的 q.txt 檔案 */ public class CopyDemo1 { public static void main(String[] args) { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { //建立檔案輸入流 fileInputStream = new FileInputStream("D:\\p.txt"); //建立檔案的輸出流 fileOutputStream = new FileOutputStream("D:\\w.txt"); //開始讀取檔案 //方法一:一個位元組一個位元組的讀 // int len = 0; // while ((len = fileInputStream.read()) !=-1){ // fileOutputStream.write(len); // } //方式二:利用位元組陣列讀取 byte[] bytes = new byte[1024]; int len = 0; while ((len = fileInputStream.read(bytes)) != -1){ System.out.println(len); fileOutputStream.write(bytes, 0, len); } System.out.println("複製成功!"); } catch (IOException e) { e.printStackTrace(); }finally { try { fileOutputStream.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }