1 package com.yhqtv.demo05.Writer; 2 3 import java.io.FileWriter; 4 5 /* 6 * @author XMKJ yhqtv.com Email:yhqtv@qq.com 7 * @create 2020-05-13-9:09 8 * 9 */ 10 /* 11 flush方法和close方法的區別 12 - flush :刷新緩沖區,流對象可以繼續使用。 13 - close: 先刷新緩沖區,然后通知系統釋放資源。流對象不可以再被使用了。 14 */ 15 public class Demo02CloseAndFlush { 16 public static void main(String[] args) throws Exception { 17 // 1.創建FileWriter對象,構造方法中綁定要寫入數據的目的地 18 FileWriter fw=new FileWriter("C:\\666\\6hello.txt"); 19 // 2.使用FileWriter中的方法write,把數據寫入到內存緩沖區中(字符轉換為字節的過程) 20 //void write(int c)寫入單個字符 21 fw.write(97); 22 //3.使用FileWriter中的方法flush,把內存緩沖區中的數據,刷新到文件中 23 fw.flush(); 24 //刷新之后流可以繼續使用 25 fw.write(98); 26 27 //4.釋放資源(會先把內存緩沖區中的數據刷新到文件中) 28 fw.close(); 29 30 } 31 }
flush方法和close方法的區別
- flush :刷新緩沖區,流對象可以繼續使用。
- close: 先刷新緩沖區,然后通知系統釋放資源。流對象不可以再被使用了。
