BufferedInput&OutputStream
- 高级流
- 需要依托低级流(FileInputStream&FileOutputStream)
- 字节流
-
使用原因:基本流(FileInputOutputStreamTest)读写文件的速度相对较慢
@Test public void FileInputOutputStreamTest() { long startTime = System.currentTimeMillis(); try{ fis = new FileInputStream("D:/jdk1.8中文版.CHM"); fos = new FileOutputStream("D:/jdk1.8中文版_copy.CHM"); int len; //读写速度较慢 while ((len = fis.read()) != -1){ fos.write(len); } } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } long endTime = System.currentTimeMillis(); //时间太长,不测试了 System.out.println("花费时间:" + (endTime-startTime) + "ms"); }
-
改进:使用BufferedInputStream&BufferedOutputStream
@Test public void BufferedInputOutputStreamTest(){ long startTime = System.currentTimeMillis(); try{ fis = new FileInputStream("D:/jdk1.8中文版.CHM"); fos = new FileOutputStream("D:/jdk1.8中文版_copy.CHM"); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); int buf; //改进(使用缓冲流) while((buf = bis.read()) != -1){ bos.write(buf); } } catch (IOException e) { e.printStackTrace(); }finally { try { fis.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } long endTime = System.currentTimeMillis(); //2208ms System.out.println("花费时间:" + (endTime-startTime) + "ms"); }
-
再次改进:自定义缓冲数组
@Test public void BufferedInputOutputStreamTest2(){ long startTime = System.currentTimeMillis(); try{ fis = new FileInputStream("D:/jdk1.8中文版.CHM"); fos = new FileOutputStream("D:/jdk1.8中文版_copy.CHM"); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); //再改进 int len = 0; byte[] buf = new byte[1024*8]; while ((len = bis.read(buf)) != -1){ bos.write(buf, 0, len); } } catch (IOException e) { e.printStackTrace(); }finally { try { fis.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } long endTime = System.currentTimeMillis(); //157ms System.out.println("花费时间:" + (endTime-startTime) + "ms"); }
-
对比基本流和缓冲流的读写速度
-
使用基本流复制一个文件
@Test public void copyImgTest1() throws IOException{ long startTime = System.currentTimeMillis(); FileInputStream fis = new FileInputStream("D:/jdk1.8中文版.CHM"); FileOutputStream fos = new FileOutputStream("D:/jdk1.8中文版_copy.CHM"); byte[] buf = new byte[1024]; int len; //读并写 while ((len = fis.read(buf)) != -1){ fos.write(buf,0,len); } long endTime = System.currentTimeMillis(); System.out.println("复制成功!\n用时:" + (endTime-startTime) + "ms"); }
-
使用缓冲流复制相同文件
@Test public void copyImgTest2() throws IOException{ long startTime = System.currentTimeMillis(); FileInputStream fis = new FileInputStream("D:/jdk1.8中文版.CHM"); FileOutputStream fos = new FileOutputStream("D:/jdk1.8中文版_copy.CHM"); byte[] buf = new byte[1024]; int len; BufferedOutputStream bos = new BufferedOutputStream(fos); BufferedInputStream bis = new BufferedInputStream(fis); //通过BufferedInput&OutputStream读写 while ((len = bis.read(buf)) != -1){ bos.write(buf, 0, len); } long endTime = System.currentTimeMillis(); System.out.println("复制成功!\n用时:" + (endTime-startTime) + "ms"); }
-
结果对比
-
基本流使用438ms
-
缓冲流使用154ms
-
-