flush in java.io.FileWriter

后端 未结 9 1176
清酒与你
清酒与你 2020-11-30 07:33

I have a question in my mind that, while writing into the file, before closing is done, should we include flush()??. If so what it will do exactly? dont streams auto flush??

9条回答
  •  渐次进展
    2020-11-30 07:49

    Another usecase for flushing in program is writing progress of longrunning job into file (so it can be stopped and restarted later. You want to be sure that data is safe on the drive.

    while (true) {
      computeStuff();
      progresss += 1;
      out.write(String.format("%d", progress));
      out.flush();
    }
    out.close();
    

提交回复
热议问题