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

    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM