客官,小板凳坐好,开始啦!
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 ?