目标文件为jdk-11_windows-x64_bin.exe
150 MB (158,293,352 字节)
一、使用基本流测试
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class demo01 { public static void main(String[] args) throws IOException { long start=System.currentTimeMillis(); String str1="D:\\迅雷下载\\jdk-11_windows-x64_bin.exe"; String str2="D:\\迅雷下载\\jdk-11_windows-x64_bin_01.exe"; FileInputStream fis = new FileInputStream(str1); FileOutputStream fos = new FileOutputStream(str2); byte[] bytes = new byte[1024]; int ten=0; while ((ten=fis.read(bytes))!=-1){ fos.write(bytes,0,ten); } fos.close(); fis.close(); long end=System.currentTimeMillis(); System.out.println("共耗时"+(end-start)); } }
共耗时2300
二、使用缓冲流
import java.io.*; public class demo02 { public static void main(String[] args) throws IOException { long start=System.currentTimeMillis(); String str1="D:\\迅雷下载\\jdk-11_windows-x64_bin.exe"; String str2="D:\\迅雷下载\\jdk-11_windows-x64_bin_02.exe"; FileInputStream fis = new FileInputStream(str1); FileOutputStream fos = new FileOutputStream(str2); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); byte[] bytes = new byte[1024]; int ten=0; while ((ten=bis.read(bytes))!=-1){ bos.write(bytes,0,ten); } bos.close(); bis.close(); long end=System.currentTimeMillis(); System.out.println("共耗时"+(end-start)); } }
共耗时623
计算机硬件不同,后台执行任务不同,会有差异,可以看出缓冲流速度大大优于基本流!