缓冲流

作者: 只取一瓢爱了解 | 来源:发表于2020-07-01 22:50 被阅读0次

    概述

    缓冲流,也叫高效流,是对4个基本流的增强,所以也是4个流,按照数据类型分类:

    • 字节缓冲流:BufferedInputStream , BufferedOutputStream
    • 字符缓冲流:BufferedReader , BufferedWriter

    缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写效率

    字节缓冲流

    BufferedOutputStream 字节缓冲输出流

    public class Demo1 {
        public static void main(String[] args) throws IOException {
            // 创建FileOutputStream对象,绑定输出目的地
            FileOutputStream fos = new FileOutputStream("/Users/wym/f.txt");
            // 创建BufferedOutputStream,构造方法中传入FileOutputStream对象,提高FileOutputStream对象效率
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            //把数据写入到缓冲区中
            bos.write("数据写入到内部缓冲区中".getBytes());
            //释放资源(会先调用flush方法刷新数据)
            bos.close();
        }
    }
    

    BufferedInputStream 字节缓冲输入流

    public class Demo2 {
        public static void main(String[] args) throws IOException {
            // 创建FileInputStream对象,绑定要读取的数据源
            FileInputStream fis = new FileInputStream("/Users/wym/a.txt");
            // 创建BufferedInputStream对象,构造方法中传入FileInputStream对象,提高FileInputStream读取效率
            BufferedInputStream bis = new BufferedInputStream(fis);
            byte[] bytes = new byte[1024];
            int len;
            while((len = bis.read(bytes)) != -1){
                System.out.println(new String(bytes,0,len));
            }
            // 释放资源
            bis.close();
        }
    }
    

    字符缓冲流

    BufferedWriter 字符缓冲输出流

    public class Demo3 {
        public static void main(String[] args) throws IOException {
            BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/wym/g.txt"));
            for(int i = 0;i < 10;i++){
                bw.write("德布劳内");
                bw.newLine();
            }  
            bw.close();
        }
    }
    

    BufferedReader 字符缓冲输入流

    public class Demo4 {
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new FileWriter("/Users/wym/g.txt"));
            String line;
            while((line = br.readLine()) != null){
              System.out.println(line);
            }
            br.close();
        }
    }
    

    练习-对文本内容进行排序

    public class Sort {
        public static void main(String[] args) throws IOException {
            // 创建一个HashMap集合,key:存储每行文本的序号  value:存储每行的文本
            Map<String,String> map = new HashMap<>();
            // 创建字符缓冲输入流,绑定字符输入流
            BufferedReader br = new BufferedReader(new FileReader("/Users/wym/in.txt"));
            // 创建字符缓冲输出流,绑定字符输出流
            BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/wym/out.txt"));
            String line;
            // 使用readLine方法,逐行读取数据
            while((line = br.readLine()) != null){
                // 对读取的文本进行切割,获取序号和内容
                String[] arr = line.split("\\.");
                // 将切割好的序号和文本存入到map中
                map.put(arr[0],arr[1]);
            }
    
            // 遍历map集合,获取键值对
            for(String key : map.keySet()){
                String content = map.get(key);
                // 每一个键值对,拼接成一个文本行
                line = key + "." + content;
                // 拼接好的文本使用字符缓冲输出流中的write方法写出,并换行
                bw.write(line);
                bw.newLine();
            }
    
            // 释放资源
            bw.close();
            br.close();
        }
    }
    

    相关文章

      网友评论

          本文标题:缓冲流

          本文链接:https://www.haomeiwen.com/subject/ooekqktx.html