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

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

本文详细探讨了Java中的FileOutputStream类,讲解了它如何继承InputStream并实现两个接口,重点在于write方法的不同重载形式以及构造方法中‘append’参数的作用。通过实例解析了如何根据该参数决定是否追加数据到已有文件,以及异常处理机制。
最低0.47元/天 解锁文章
367





