IO流 拷贝文本文件

/*

 * 将C盘一个文本文件复制到D盘  

* 1、在D盘创建一个文件,用于存储C盘文件中的数据  

* 2、定义读取流和C盘文件关联  

* 3、通过不断的读写完成数据存储  

* 4、关闭资源

 */

import java.io.*;

public class CopyText {  

      public static void main(String[] args)  

        {  

                 copy_1();  

                 //copy_2();  

         }      

      /*

       * 从C盘读取一个字符,就往D盘写一个字符

       */

       public static void copy_1() throws IOException  

        {  

              //创建目的地

              FileWriter fw=new FileWriter("RuntimeDemo_copy.txt");   

              //与已有文件有关联  

               FileReader fr=new FileReader("RuntimeDemo.java");

                int ch=0;  

               while((ch=fr.read())!=-1)  

                {    

                      fw.write(ch);  

                 }  

               fw.close();  

               fr.close();

          }

      /*

                   public static void copy_2()

                  {   

                       FileWriter fw=null;  

                       FileWriter fr=null;  

                        try  

                           {   

                                   fw=new FileWriter("RuntimeDemo_copy.txt");  

                                   fr=new FileReader("RuntimeDemo.java");   

                                   char[]buf=new char[1024];    

                                   int len=0;    

                                  while((len=fr.read(buf))!=-1)   

                                        {     

                                                fw.write(buf,0,len);   

                                         }   

                              }   

                           catch(IOException e)  

                               {

                                    throw new RuntimeException("读写失败");   

                                }

                            finally

                               {

                                          try

                                             {  

                                                  if(fr!=null)    

                                                  fr.close();  

                                              }

                                          catch(IOException e)   

                                               {

                                               }

                                           try  

                                                 {    

                                                        if(fw!=null)      

                                                               fw.close();  

                                                   }   

                                            catch(IOException e)   

                                                  {

                                                   }

                                     }  

                        }

             */

}

posted @ 2015-05-08 13:10  冰林  阅读(211)  评论(0编辑  收藏  举报