Java:IO流的综合用法(从键盘录入数据并打印在控制台上)

 1 import java.io.*;
 2 public class IOTestDouble
 3 {
 4     public static void main(String[] args)throws Exception
 5     {
 6         BufferedReader bufr = null;
 7         BufferedWriter bufw = null;
 8         try
 9         {
10             bufr = new BufferedReader(new InputStreamReader(System.in));//读入流路径为键盘录入
11             bufw = new BufferedWriter(new OutputStreamWriter(System.out));//输出流路径为控制台输出
12             String line = null;
13             //从键盘读取数据
14             while((line = bufr.readLine()) != null)
15             {
16                 bufw.newLine();
17                 if(line.equals("quit"))
18                     break;
19             //将数据打印在控制台上
20                 bufw.write(line);
21             }
22         }
23         catch(IOException ie)
24         {
25             throw new RuntimeException("读入读取流异常");
26         }
27         finally
28         {
29             try
30             {
31                if(bufr!=null)
32                   bufr.close();
33             }
34             catch(IOException ie)
35             {
36                throw new RuntimeException("读取流关闭异常");    
37             }
38             try
39             {
40                if(bufw!=null)
41                   bufw.close();
42             }
43             catch(IOException ie)
44             {
45                throw new RuntimeException("输出流关闭异常");    
46             }
47         }
48     }
49 }

 

posted @ 2015-08-13 22:01  XYQ全哥  阅读(233)  评论(0编辑  收藏  举报