io流通过缓冲区读取写入数据

import java.io.*;


public class Main {
   public static void main(String[] args)throws IOException{
      BufferedReader bd = null;
      BufferedWriter bw = null;
      try {
         bd = new BufferedReader(new FileReader("Demo.txt"));//Demo.txt文件中读取数据
         bw = new BufferedWriter(new FileWriter("Demo_copy.txt"));//写入到Demo_copy.txt         String line = null;
         while ((line = bd.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
         }
      }
      catch (IOException e){
         System.out.println("读写失败");
      }
      finally {
         if(bd!=null){
            try {
               bd.close();
            }
            catch (IOException e){
               System.out.println("读取关闭失败");
            }
         }
         if(bw!=null){
            try {
               bw.close();
            }
            catch (IOException e){
               System.out.println("写入关闭失败");
            }
         }
      }
   }
}

你可能感兴趣的:(进步)