使用字节流复制文件


  本文标签:Java

  以前在学校,为了准备某个证书考试,预习的时候写的  。没什么技术含量,主要是熟悉一下,j2se中基本控件的操作,以及事件的绑定,等等  。

  Example:

  1. import java.io.*;  
  2. import java.util.Scanner;  
  3. /**   
  4. * java使用字节流复制图像   
  5. * @author Administrator   
  6. *@time 2011年6月9日 19:55:10   
  7. */public class MyTest2 {  
  8.       private String filename;  //文件名  
  9.        private  double  filesize;       //文件大小  
  10.      public static void main(String agrs[]){  
  11.       MyTest2 tt2=new MyTest2();  
  12.       String frompath="E:\\qq.png";  
  13.       String topath="F:\\qq.png";  
  14.       if(tt2.CheckFile(frompath, topath)){  
  15.       tt2.CopyFile(frompath, topath);  
  16.         }  
  17.     }  
  18.     //复制文件  
  19.     public void  CopyFile(String frompath,String topath){  
  20.         File file1=new File(frompath);//源文件路径  
  21.         File file2=new File(topath);//目标文件路径  
  22.         filename=file1.getName();  
  23.         filesize=(file1.length())/1024/1024;  
  24.         System.out.println("********************文件属性********************");  
  25.         System.out.println("源文件路径:"+frompath);  
  26.         System.out.println("目标文件路径:"+topath);  
  27.         System.out.println("文件名称:"+filename);
  28.         System.out.println("文件大小:"+filesize+" MB");  
  29.         int ch=0;  
  30.         try{  
  31.             FileInputStream fin=new FileInputStream(file1);  
  32.             FileOutputStream fout=new FileOutputStream(file2);  
  33.             ch=fin.read();  
  34.             System.out.println("开始复制!");  
  35.           long startTime=System.currentTimeMillis();   //获取开始时间  
  36.             while(ch!=-1){  
  37.                 fout.write(ch);  
  38.                 ch=fin.read();  
  39.             }  
  40.             long endTime=System.currentTimeMillis(); //获取结束时间  
  41.             System.out.println("程序运行时间: "+(endTime-startTime)+"ms");  
  42.             System.out.println("复制完毕!");  
  43.             //关闭流  
  44.             fin.close();  
  45.               fout.close();  
  46.         }  
  47. catch(Exception e){  
  48.             System.err.println("Error:  "+e);  
  49.        }  
  50.        }  
  51.    //验证文件是否存在  
  52.     public boolean CheckFile(String frompath,String topath){  
  53.         File file1=new File(frompath);//源文件路径  
  54.         File file2=new File(topath);//目标文件路径  
  55.         if(!file1.exists()){  
  56.                     //文件不存在  
  57.                System.out.println("源文件不存在,请检查路径是否正确!");  
  58.             return false;  
  59.         }  
  60. else{  
  61.             if(file2.exists()){  
  62.                 System.out.println("目标文件已经存在,请选择  覆盖/取消  ?");  
  63.                 System.out.println("1: 覆盖    2:取消");  
  64.                 try{  
  65.                     Scanner sc=new Scanner(System.in);  
  66.                     int a=sc.nextInt();  
  67.                     if(a==1){  
  68.                         System.out.println("你输入的是1,操作将继续,目标文件将被覆盖  。");  
  69.                         return true;  
  70.                     }else if(a==2){  
  71.                         System.out.println("您输入了2,操作将取消  。");  
  72.                         return false;  
  73.                     }  
  74. else{  
  75.                     System.out.println("输入无效  。  。;");  
  76.                     CheckFile(frompath, topath);  
  77.                return false; }  
  78.                 }  
  79.  catch(Exception ee){  
  80.                     System.out.println("输入无效  。  。;");  
  81.                     CheckFile(frompath, topath);  
  82.                     return false;  
  83.                 }   
  84.            }  
  85.         }  
  86.         return false;    }  

  原文链接:http://www.cnblogs.com/whynever/archive/2011/12/17/2291163.html