對java IO BufferedWriter 使用的演示

import java.io.*;
/*
 對java IO BufferedWriter 使用的演示
*/

class BufferedDemo{
 static BufferedWriter bw;
 static FileWriter fw;
 public static void main(String[] args){
  try{
   fw = new FileWriter("g:/bufferData.txt");
   bw = new BufferedWriter(fw);
   for(int i =0; i<5; i++){
    bw.write("abcd");
    //插入兼容換行符
    bw.newLine();
    //用到緩衝區就要刷新
    bw.flush();
   }
   //關閉緩衝區,就是關閉緩衝區中的IO流對象
   bw.close();
  }
  catch(IOException e){
   System.out.println(e.toString() ) ; 
  }
  finally{
   if(bw != null){
    try{
     bw.close();
    }catch(IOException e){
     System.out.println(e.toString() ) ; 
     System.out.println("Can not close the Stream!");
    }
   }
  } 
 
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章