记住使用flush()函数

在进行IO中的输出流的时候,有时会遇到输出的文件中没有被写入内容,这时可能是由于没使用flush()函数

故,可以这样写:

public class demo1 {
public static void main(String[] args) {
File fileOutput = new File("D:\workspaceIDEA\demo1\src\com\stream\aa.txt");
String inputPath = "D:\workspaceIDEA\demo1\src\com\stream\ss.txt";
try {
if (!fileOutput.exists()){
fileOutput.createNewFile();
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(inputPath));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(fileOutput));
int len;
byte[] bytes = new byte[1024];
while ((len=bufferedInputStream.read(bytes))!=-1){
bufferedOutputStream.write(bytes,0,len);
//调用flush函数
bufferedOutputStream.flush();

}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

posted @ 2020-02-22 21:51  一起学编程  阅读(3395)  评论(0)    收藏  举报