首页 > 其他 > 详细

IO流 Buffered 综合练习

时间:2016-01-23 13:01:40      阅读:159      评论:0      收藏:0      [点我收藏+]
 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 }

 

IO流 Buffered 综合练习

原文:http://www.cnblogs.com/yangyongqian/p/5153020.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!