zoukankan      html  css  js  c++  java
  • 使用带缓冲区的输入输出流的速度会大幅提高

    过滤流:

     bufferedOutputStream

     bufferedInputStream

    用于给节点流增加一个缓冲的功能。

    在VM的内部建立一个缓冲区,数据先写入缓冲区,等到缓冲区的数据满了之后再一次性写出,效率很高。

    使用带缓冲区的输入输出流的速度会大幅提高,缓冲区越大,效率越高。(这是典型的牺牲空间换时间)

    切记:使用带缓冲区的流,如果数据数据输入完毕,使用flush方法将缓冲区中的内容一次性写入到外部数据源。用close()也可以达到相同的效果,因为每次close都会使用flush。一定要注意关闭外部的过滤流。

     1 package TomTexts;
     2 import java.io.*;
     3 public class TomTexts_34 {
     4 
     5     public static void main(String[] args) {
     6           String file1,file2 ;
     7           int ch = 0 ;
     8           file1 = "readme.txt" ;
     9           file2="readme.bak";
    10           try  {
    11                    FileInputStream fis = new FileInputStream(file1);
    12                    FileOutputStream fos=new FileOutputStream(file2);
    13                    int size=fis.available();
    14                    System.out.println("字节有效数:"+size);
    15                 while ((ch=fis.read())!=-1){
    16                     System.out.write(ch);
    17                     fos.write(ch);
    18                  }
    19                    fis.close();
    20                    fos.close();
    21           } 
    22     catch (IOException e){
    23                    System.out.println(e.toString());
    24               } 
    25     }
    26 
    27 }
  • 相关阅读:
    穷举和迭代
    for循环练习题
    case when then else end 用法
    如何将数据库账号(用户)解锁
    比赛安排
    How to spend you day ?
    异常-问题型
    重载和重写的区别
    new关键字的理解-问题型
    源辰项目-1
  • 原文地址:https://www.cnblogs.com/borter/p/9425043.html
Copyright © 2011-2022 走看看