使用字节缓冲流写入文件 BufferedOutputStream

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.Buffer;

/**
 * 使用字节缓冲流写入文件
 *BufferedOutputStream
 * @author cg
 */

public class Demo05 {
    public static void main(String[] args) throws IOException {
        //1.创建字节输出缓存区
        FileOutputStream fos = new FileOutputStream("c:\\Users\\cheng\\Desktop\\code\\buffer.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //2.写入文件
        for (int i = 0; i < 10; i++) {
            bos.write("hello,world\r\n".getBytes());//写入8k缓存区
            bos.flush();//刷新到硬盘
        }
        //3.关闭(内部调用flush方法)
        bos.close();
    }
}

posted @ 2026-03-17 19:17  果子同志  阅读(13)  评论(0)    收藏  举报