带缓冲的输入/输入流

  • BufferedInputStream类 与 BufferedOutputStream类 BufferedInputStream类 可以对所有InputStream的子类进行缓冲区的包装,以达到 性能的优化BufferedOutputStream类 中的 flush()方法 被用来把缓冲区的字节 写入到文件中,并 清空缓存BufferedInputStream类的构造方法:构造方法 介绍 BufferedInputStream(FileInputStream fileInputStream); 创建一个 带有32个字节的缓冲输入流BufferedInputStream(FileInputStream fileInputStream , int size);指定的大小来创建缓冲输入流BufferedOutputStream类的构造方法:构造方法 介绍 BufferedOutputStream(FileOutputStream fileOutputStream); 创建一个 带有32个字节的缓冲输出流BufferedOutputStream(FileOutputStream fileOutputStream , int size);指定的大小来创建缓冲输出流BufferedInputStream类 与 BufferedOutputStream类 实例:
import java.io.*;

public class Demo4 {
    public static void main(String[] args) {
        /**
         * 缓冲字节输入流(BufferedInputStream)
         * 特点:提高效率
         */
        File file = new File("C:\\JAVA_API_1.7中文.chm");
        BufferedInputStream bufferedInputStream = null;//创建缓冲字节流
        FileInputStream fileInputStream = null;
        long stare = System.currentTimeMillis();//获得当前流开始时的毫秒值
        try {
            fileInputStream=new FileInputStream(file);
            bufferedInputStream = new BufferedInputStream(fileInputStream);//将文件字节流包装成缓冲字节流
            byte by[] = new byte[1024];//缓冲区字节数组(这个缓冲区与Buffered不同)
            while ((bufferedInputStream.read(by))!=-1){//使用缓冲字节流读取数据

            }
            long end = System.currentTimeMillis();//获得当前流结束时的毫秒值
            System.out.println("运行经历的毫秒数:"+(end - stare));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream!=null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        /**
         * 缓冲字节输出流(BufferedOutputStream)
         * 特点:提高效率
         */
        File file1 = new File("C:\\Word.txt");
        BufferedOutputStream bufferedOutputStream = null;//创建缓冲字节输出流
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream=new FileOutputStream(file1);
            bufferedOutputStream=new BufferedOutputStream(fileOutputStream);//将文件输出流包装到缓冲字节输出流

            String str = "深山踏红叶,耳畔闻鹭鸣。";
            byte by[] = str.getBytes();
            bufferedOutputStream.write(by);
            // 使用缓冲字节输出流时,要多进行刷新操作,避免等待,有数据时就将数据写入文件当中
            bufferedOutputStream.flush();//刷新(强制将缓冲区数据写入文件中,即使缓冲区没有写满)

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedOutputStream!=null){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Original: https://www.cnblogs.com/TeaTracing/p/16120054.html
Author: TeaTracing
Title: 带缓冲的输入/输入流

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/621970/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 主从复制架构直接转换MGR(manual)

    IP port role info 192.168.188.81 3316 node1 master 192.168.188.82 3316 node2 slave1 192.16…

    数据库 2023年6月16日
    0219
  • zabbix自定义监控进程和日志

    自定义监控 进程 日志 mysql主从状态 mysql主从延迟 自定义监控 进程 [root@client ~]# cd /usr/local/etc/ [root@client …

    数据库 2023年6月14日
    091
  • ArrayList扩容机制

    1.构造函数 有三种 说第一种无参构造,默认初始容量为10 2.add函数 /** * 将指定的元素追加到此列表的末尾。 */ public boolean add(E e) { …

    数据库 2023年6月16日
    091
  • Linux_连接工具_SecureCRT的使用教程

    什么是SecureCRT? SecureCRT是一款支持 SSH2、SSH1、Telnet、Telnet/SSH、Relogin、Serial、TAPI、RAW 等协议的终端仿真程…

    数据库 2023年6月11日
    0118
  • flowable 查询、完成、作废、删除 任务

    /** * 查询我的任务 * from fhadmin.cn * @param USERNAME * @return 返回任务列表 */ protected List findMy…

    数据库 2023年6月6日
    0210
  • CSS速学!!

    padding:内边距 缩写:缩写: padding:值; 上下左右的内边距一样 padding:值1 值2; 值1代表上下内边距,值2代表左右内边距 padding:值1 值2 …

    数据库 2023年6月16日
    076
  • 注解

    注解概述 从 JDK5 开始,Java 增加对 元数据的支持,也就是注解,注解与注释是有一定区别的,可以把注解理解…

    数据库 2023年6月15日
    096
  • [LeetCode]3. 无重复字符的最长子串

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: “abcabcbb”输出: 3解释: 因为无重复字符的最长子串是 &#…

    数据库 2023年6月9日
    066
  • Java面试题(六)–Redis

    1 Redis基础篇 1、简单介绍一下Redis优点和缺点? 优点: 1、本质上是一个 Key-Value 类型的内存数据库,很像memcached 2、整个数据库统统加载在内存当…

    数据库 2023年6月16日
    087
  • MySql用户与权限控制

    MySql用户与权限控制 — 刷新权限命令 # — 刷新mysql权限命令 flush privileges; 用户管理 1、查看用户 #查看用户 USE mysql…

    数据库 2023年6月11日
    085
  • podman(无根用户管理podman)

    用户操作在允许没有root特权的用户运行Podman之前,管理员必须安装或构建Podman并完成以下配置cgroup V2Linux内核功能允许用户限制普通用户容器可以使用的资源,…

    数据库 2023年6月14日
    092
  • 希望腿上的伤快点好

    明天去星巴克泡一会儿想把一些课程关联到的课程学习一下 Original: https://www.cnblogs.com/ukzq/p/16747859.htmlAuthor: D…

    数据库 2023年6月11日
    090
  • centos系统下mysql的配置

    配置文件路径 /etc/my.cnf Hole yor life get everything if you never give up. Original: https://ww…

    数据库 2023年6月9日
    069
  • MySQL之存储引擎、基本数据类型及约束条件

    一、存储引擎 数据库存储引擎是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建、查询、更新和删除数据。不同的存储引擎提供不同的存储机制、索引技巧、锁定水平等功能,…

    数据库 2023年5月24日
    072
  • GROUP BY 后获取每一组最新的一条记录

    最近有一种需求,一张订单可能有多个支付单,这就要求我们拿到每一张订单的最新支付单。具体思路如下: [En] Recently, there is a demand that the…

    数据库 2023年5月24日
    071
  • 绿色安装MySQL5.7版本—-配置my.ini文件注意事项

    简述绿色安装MySQL5.7版本以及配置my.ini文件注意事项 前言 由于前段时间电脑重装,虽然很多软件不在C盘,但是由于很多注册表以及关联文件被删除,很多软件还需要重新配置甚至…

    数据库 2023年5月24日
    097
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球