普通IO和BufferIO的区别

本文深入探讨了Java中普通IO与BufferIO的区别,解释了BufferIO如何提供更高的性能和效率,通过缓冲区提升数据读写速度。讨论了在不同场景下选择普通IO或BufferIO的考量因素,并提供了实际示例来展示两者在处理文件操作时的差异。
                                                                      普通IO和BufferIO的区别
package cn.qqjx.io;

import java.io.*;

public class IOApplication {

    public static void main(String[] args) throws Exception {

        String path = "E:\\workspace\\io\\a.txt";
        byte[] data = "0123456789\n".getBytes();
        //testBasicIO(path,data);
        testBufferIO(path,data);

    }

    //测试普通IO
    //IO往文件写的时候,一旦关掉电源写的内容没有进入磁盘,还在内核的缓存区 故写入文件失败
    public static void  testBasicIO(String path,byte[]data) throws Exception {
        File file = new File(path);
        FileOutputStream fos = new FileOutputStream(file);
        while (true){
            fos.write(data);
        }

    }

    //测试BufferIO
    //jvm 8kb的字节数组 调用内核的syscall write(8kb byte[])
    public static  void testBufferIO(String path,byte[]data) throws Exception {
        File file = new File(path);
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        while (true){
            out.write(data);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值