2021.6.14笔记 缓冲流

缓冲流的原理

在这里插入图片描述


字节缓冲输出流

在这里插入图片描述

public class Buffer {
    public static void main(String[] args) throws IOException {
        FileOutputStream fops = new FileOutputStream("F:\\test\\aa.txt");
        BufferedOutputStream bops = new BufferedOutputStream(fops);
        bops.write("小新妮妮风间".getBytes());
        bops.flush();
    }
}

字节缓冲输入流

在这里插入图片描述在这里插入图片描述

public class BufferIps {
    public static void main(String[] args) throws IOException {
        FileInputStream fips = new FileInputStream("K:\\aa\\cc.txt");
        BufferedInputStream bips = new BufferedInputStream(fips);
        int len = 0;
        while((len = bips.read()) != -1) {
            System.out.println(len);
        }
        bips.close();   //自动关闭fips
    }
}
public class BufferIps {
    public static void main(String[] args) throws IOException {
        FileInputStream fips = new FileInputStream("F:\\test\\aa.txt");
        BufferedInputStream bips = new BufferedInputStream(fips);
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = bips.read(bytes)) != -1) {
            System.out.println(new String(bytes,0,len));
        }
    }
}

字符缓冲输出流

在这里插入图片描述

在这里插入图片描述

public class BufferedWriterC {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("F:\\test\\aa.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        for (int i = 0; i < 10; i++) {
            bw.write("小新妮妮风间");
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }
}

ps:弄了半天代码报错,问题就是命名橙BufferedWritter了,算是个教训


字符缓冲输入流

在这里插入图片描述
在这里插入图片描述

public class BufferedReaderC {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("F:\\test\\aa.txt"));
        String line;
        while((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}

练习:对文本的内容进行排序

在这里插入图片描述

public class PracticeSort {
    public static void main(String[] args) throws IOException {
        HashMap<String,String> mapString = new HashMap<>();
        BufferedReader br = new BufferedReader(new FileReader("F:\\test\\aa.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\test\\bb.txt"));
        String line;
        while((line = br.readLine()) != null) {
            String[] split = line.split("\\.");
            mapString.put(split[0],split[1]);
        }
        for (String key : mapString.keySet()) {
            String value = mapString.get(key);
            line = key + "." + value;
            bw.write(line);
            bw.newLine();
        }
        bw.close();
        br.close();
    }
}

感觉前面的map的方法都有些模糊了。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值