package io;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
- 缓冲区写出字节时的缓冲区问题
- @author W
*/
public class BufferedOutputStream_flush {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("bos.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
String str = "Hello";
byte[] data = str.getBytes("UTF-8");
bos.write(data);
/*
* void flush()
* 将缓冲区中的字节一次性写出。
* 频繁调用flush方法会导致实际发生的写次数提高
* 从而降低些效率,但是可以提高写出数据效率的及时性。
*/
bos.flush();
System.out.println("写出完毕!");
/*
* 缓冲流关闭时会自动调用一次flush方法
*/
bos.close();
}
}