日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 java綜合 正文

分析JAVA打印流的自動刷新

作者:ashuai~ 更新時間: 2024-07-14 java綜合

參考:https://blog.csdn.net/xshsjl/article/details/77076235
參考:https://blog.csdn.net/weixin_43369921/article/details/111397253

今天遇到了一個奇怪的事情,使用打印流,有一個自動刷新的參數,但設不設置結果貌似沒啥影響,下面來研究研究

public PrintStream(OutputStream out, boolean autoFlush)
public PrintWriter(OutputStream out/Writer, boolean autoFlush)

首先并不是所有的流都需要刷新,只有使用緩沖池的流才會需要刷新,如緩沖流。
flush方法有三種情況。

  1. OutputStream中的flush是一個空的方法。
  2. 有一些實現類中,沒有重寫flush方法,直接是繼承父類的flush方法。
  3. flush方法被重寫,如BufferedOutputStream緩沖輸出流

所以打印流中參數設置的流符合上述三種情況并切實使用緩沖池,才會涉及到刷新,當然OutputStream可有可無,其flush方法是象征性的,內容為空,以及有一些流的flush方法使用的是OutputStream的flush方法,也可有可無,但最好有,如DataOutputStream


同時,打印流實現自動刷新也是有條件的

  • 查看PrintStream構造器源碼
autoFlush – Whether the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written

autoFlush設置為true,只有在使用println或換行符\n才自動刷新

  • 查看PrintWrite構造器源碼
autoFlush – A boolean; if true, the println, printf, or format methods will flush the output buffer

autoFlush設置為true,只有使用println,printf,format才會執行自動刷新

原文鏈接:https://blog.csdn.net/djh20030101/article/details/139221555

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新