BufferedReader 输出与BufferedWriter 输入的用法

时间:2021-12-16 16:50:17

public static void main(String[] args) {
  String content[] = {"好久不见了","最近好吗","常联系"};
     File file = new File("E:/workspace/Java从入门到精通/mynote.txt");
     try{
      FileWriter fw = new FileWriter(file);
      BufferedWriter bufferfw = new BufferedWriter(fw);
      for(int k=0;k<content.length;k++){
       bufferfw.write(content[k]);
       bufferfw.newLine();
      }
      bufferfw.close();
      fw.close();
     }catch(Exception e)
     {
      e.printStackTrace();
     }
    
     try{
      FileReader fr = new FileReader(file);
      BufferedReader bufferfr = new BufferedReader(fr);
      String s = null;
      int i=0;
      while((s=bufferfr.readLine())!=null)
      {
       i++;
       System.out.println("第"+i+"行:"+s);
      }
      bufferfr.close();
      fr.close();
     }catch(Exception e)
     {
      e.printStackTrace();
     }
 }

}