IO流 Buffered 综合练习

 1 package com.yyq;
 2 import java.io.*;
 3 /*
 4  * 缓冲区中有一个读取一行的方法 (BufferedReader readline BufferedWriter newline)
 5  * readline方法返回的时候只返回回车符之前的数据内容
 6  * 并不返回回车符
 7  */
 8 public class BufferedTest2 {
 9 
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         copy_1();
13     }
14     // 使用缓冲区将文件拷贝
15     public static void copy_1(){
16         BufferedWriter bufw = null;
17         BufferedReader bufr = null;
18         try{
19             bufw = new BufferedWriter(new FileWriter("1_copy.txt"));
20             bufr = new BufferedReader(new FileReader("1.txt"));
21             String line = null;
22             // readline 返回的是有效数据
23             while((line = bufr.readLine())!=null){
24                 bufw.write(line);
25                 bufw.newLine();
26                 bufw.flush();
27             }
28         }
29         catch(IOException e){
30             throw new RuntimeException("失败了");
31         }
32         finally{
33             if(bufw!=null){
34                 try{
35                 bufw.close();
36                 }
37                 catch(Exception e){
38                     e.printStackTrace();
39                 }
40             }
41             if(bufr!=null){
42                 try{
43                 bufr.close();
44                 }
45                 catch(Exception e){
46                     e.printStackTrace();
47                 }
48             }
49         }
50     }
51 
52 }
原文地址:https://www.cnblogs.com/yangyongqian/p/5153020.html