JAVA——緩衝流 (位元組緩衝流讀取檔案的速度比較 --使用緩衝流角度和使用陣列的角度)
阿新 • • 發佈:2020-09-01
package com.cskaoyan.io01.efficiency; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * @author [email protected] on * @version 1.0 * * 比較四種複製方式的效率 * * 1. 不使用緩衝流 * a. 一次複製一個位元組資料 * b. 一次複製一個位元組陣列的資料 * * 2. 使用緩衝流 * a. 一次複製一個位元組資料 * b. 一次複製一個位元組陣列的資料 * * * copyByte: 48902 * copyByteInBuf: 411 copyBytes: 40 copyBytesInBuf: 18 * * * */ public class Comparison { public static void main(String[] args) throws IOException { File src = new File("e:\\copy\\vedio.mp4"); File dest1 = new File("e:\\copy-vedio1.mp4"); File dest2 = new File("e:\\copy-vedio2.mp4"); File dest3 = new File("e:\\copy-vedio3.mp4"); File dest4 = new File("e:\\copy-vedio4.mp4"); //不用緩衝流,一次複製一個位元組 copyByte(src, dest1); //使用緩衝流,一次複製一個位元組 //copyByteInBuf(src, dest2); //不使用緩衝流,一次複製一個位元組陣列資料 //copyBytes(src, dest3); //使用緩衝流,一次複製一個位元組陣列資料 //copyBytesInBuf(src, dest4); } /** * 一次讀寫一個位元組 */ public static void copyByte(File srcFile, File destFile) throws IOException { //建立輸入流物件 FileInputStream fis = new FileInputStream(srcFile); //建立輸出流物件 FileOutputStream fos = new FileOutputStream(destFile); int readByte; //記錄當前時間的毫秒值 long start = System.currentTimeMillis(); //開始一個位元組一個位元組的複製 while ((readByte = fis.read()) != -1) { fos.write(readByte); } //記錄複製完成之後的一個時間的毫秒值 long end = System.currentTimeMillis(); System.out.println("copyByte: " + (end - start)); fis.close(); fos.close(); } /** * 一次讀寫一個位元組 但是用的是緩衝流 */ public static void copyByteInBuf(File srcFile, File destFile) throws IOException { //建立輸入流物件 FileInputStream fis = new FileInputStream(srcFile); BufferedInputStream bis = new BufferedInputStream(fis); //建立輸出流物件 FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream bos = new BufferedOutputStream(fos); int readByte; //開始一個位元組一個位元組的複製 long start = System.currentTimeMillis(); while ((readByte = bis.read()) != -1) { bos.write(readByte); } long end = System.currentTimeMillis(); System.out.println("copyByteInBuf: " + (end - start)); bis.close(); bos.close(); } /** * 一次讀寫一個位元組陣列 */ public static void copyBytes(File srcFile, File destFile) throws IOException { //建立輸入流物件 FileInputStream fis = new FileInputStream(srcFile); //建立輸出流物件 FileOutputStream fos = new FileOutputStream(destFile); int len; byte[] buffer = new byte[2048]; long start = System.currentTimeMillis(); //開始一個位元組一個位元組的複製 while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } long end = System.currentTimeMillis(); System.out.println("copyBytes: " + (end - start)); fis.close(); fos.close(); } /** * 一次讀寫一個位元組陣列,用緩衝流 */ public static void copyBytesInBuf(File srcFile, File destFile) throws IOException { //建立輸入流物件 FileInputStream fis = new FileInputStream(srcFile); BufferedInputStream bis = new BufferedInputStream(fis); //建立輸出流物件 FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream bos = new BufferedOutputStream(fos); int len; byte[] buffer = new byte[2048]; long start = System.currentTimeMillis(); //開始一個位元組一個位元組的複製 while ((len = bis.read(buffer)) != -1) { bos.write(buffer, 0, len); } long end = System.currentTimeMillis(); System.out.println("copyBytesInBuf: " + (end - start)); bis.close(); bos.close(); } }