客官,小板凳坐好,开始啦!

FileOutputStream类继承了OutputStream类,OutputStream类实现了Closeable, Flushable两个接口,在FileInputStream这一章节时候,我们已经介绍过了Closeable接口,在这里就不多言语了。那Flushable这个接口的作用呢?聊聊。
/**
* @since 1.5
*/
public interface Flushable {
/**
* 将缓冲区的数据刷新写入
*/
void flush() throws IOException;
}
其实,就是将缓冲区的流数据写入操作。
但是,注意这个方法被OutputStream重写:
public void flush() throws IOException {
}
OutputStream什么都没做!而FileOutputStream类继承了OutputStream类,并没有重写该方法;所以,也就没有刷新的功能,就是个摆设。其实是供其他流使用的,比如BufferedOutputStream类:
public synchronized void flush() throws IOException {
flushBuffer();
out.flush();
}
了解了flush()方法以后,我们就探讨一下FileOutputStream类的构造方法吧!
其实,FileOutputStream类的构造方法和FileInputStream很像,只是多了一个参数FileOutputStream(String name, boolean append)第二个参数,意思是追加的意思哦!(如果不填第二个参数,默认是false)
public FileOutputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : nul
981



被折叠的 条评论
为什么被折叠?



