記住使用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