码迷,mamicode.com
首页 > 其他好文 > 详细

IO流 拷贝文本文件

时间:2015-05-08 14:44:35      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:

/*

 * 将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)   

                                                  {

                                                   }

                                     }  

                        }

             */

}

IO流 拷贝文本文件

标签:

原文地址:http://www.cnblogs.com/binglin/p/4487414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!