Using flush() before close()

前端 未结 4 1308
说谎
说谎 2020-11-30 02:34

As per the java docs, invoking close() on any java.io Streams automatically invokes flush(). But I have seen in lot of examples, even in production codes, developers have ex

4条回答
  •  一生所求
    2020-11-30 02:49

    Developer get into a habit of calling flush() after writing something which must be sent.

    IMHO Using flush() then close() is common when there has just been a write e.g.

    // write a message
    out.write(buffer, 0, size);
    out.flush();
    
    // finished
    out.close();
    

    As you can see the flush() is redundant, but means you are following a pattern.

提交回复
热议问题