Flush()和Close()的方法的区别
Close()方法
* 具备刷新的功能,在关闭流之前,就会先刷新缓冲区,将缓冲区的字节全部刷新到文件上,在关闭流。
flush()方法
* 可以刷新,并且刷新之后可以继续写,而close刷新之后就不能继续写了。
package com.fenqing.Stream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class demo6_flushAndClose {
public static void main(String[] args) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("xxx.txt"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("xxxcopy.txt"));
int b;
while((b=bis.read())!=-1){
bos.write(b);
// bos.flush(); //3
// bos.close(); //4 报错
}
// bis.close(); //1
// bos.close();
// bos.flush(); //2
}
}