其他分享
首页 > 其他分享> > 记住使用flush()函数

记住使用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();
        }

    }
}

标签:函数,bytes,len,fileOutput,bufferedOutputStream,flush,new,记住
来源: https://www.cnblogs.com/bestjdg/p/12347451.html