IO流(文本文件读取练习)

 1 package com.yyq;
 2 import java.io.*;
 3 /*
 4  * 读取文件中的内容,输出到控制台上,输出到一个文件中
 5  */
 6 public class IODemo1 {
 7     public static void main(String[] args) {
 8         FileReader fr = null;
 9         FileWriter fw = null;
10         try{
11             //1.定义输入输出流
12             fr = new FileReader("FileWriterDemo.java");
13             fw = new FileWriter("1.txt");
14             // 定义缓冲区
15             char[] buf = new char[1024];
16             //文件处理操作的基本模式。。
17             int len = 0;
18             while((len = fr.read(buf))!=-1){
19                 System.out.println(new String(buf,0,len));
20                 fw.write(new String(buf,0,len).toCharArray());
21                 fw.flush();
22             }
23         }
24         catch(Exception e){
25             e.printStackTrace();
26         }
27         finally{
28             try{
29                 fr.close();
30             }
31             catch(IOException e){
32                 e.printStackTrace();
33             }
34         }
35     }
36 }

 

posted @ 2016-01-23 09:54  HuberyQian  阅读(1074)  评论(0编辑  收藏  举报