文件拷贝效率问题

来源:互联网 发布:mac的扑克游戏在哪里 编辑:程序博客网 时间:2024/04/30 03:52
Java代码  收藏代码
  1. package jonavin.io;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9.   
  10. public class IOUtil {  
  11.   
  12.     /** 
  13.      * 文件拷贝-- 一个字节一个字节拷贝 
  14.      * @param srcFile 
  15.      * @param destFile 
  16.      */  
  17.     public static void copyFileByByte(File srcFile,File destFile) throws IOException{  
  18.         if(!srcFile.exists()){  
  19.             throw new IllegalArgumentException("文件:"+srcFile+"不存在!");  
  20.         }  
  21.         if(!srcFile.isFile()){  
  22.             throw new IllegalArgumentException(srcFile+"不是文件!");  
  23.         }  
  24.         FileInputStream in = new FileInputStream(srcFile);//源文件  
  25.         FileOutputStream out = new FileOutputStream(destFile);//目标文件  
  26.         int b;  
  27.         while ((b = in.read()) != -1) {  
  28.             out.write(b);//写入一个字节的低八位  
  29.         }  
  30.         in.close();  
  31.         out.close();  
  32.     }  
  33.     /** 
  34.      * 拷贝文件一次读取多个字节 
  35.      */  
  36.     public static void copyFileByByteBuf(File srcFile,File destFile) throws IOException{  
  37.         if(!srcFile.exists()){  
  38.             throw new IllegalArgumentException("文件:"+srcFile+"不存在!");  
  39.         }  
  40.         if(!srcFile.isFile()){  
  41.             throw new IllegalArgumentException(srcFile+"不是文件!");  
  42.         }  
  43.         FileInputStream in = new FileInputStream(srcFile);//源文件  
  44.         FileOutputStream out = new FileOutputStream(destFile);//目标文件  
  45.         int b;  
  46.         byte[] buf = new byte[8*1024];//开辟8K的缓存  
  47.         while ((b = in.read(buf))!=-1) {  
  48.             out.write(buf, 0, b);  
  49.         }  
  50.         in.close();  
  51.         out.close();  
  52.     }  
  53.     /** 
  54.      * 通过缓冲流copy文件 
  55.      * @throws IOException 
  56.      */  
  57.     public static void copyFileByBuffed(File srcFile,File destFile)throws IOException{  
  58.         if(!srcFile.exists()){  
  59.             throw new IllegalArgumentException("文件:"+srcFile+"不存在!");  
  60.         }  
  61.         if(!srcFile.isFile()){  
  62.             throw new IllegalArgumentException(srcFile+"不是文件!");  
  63.         }  
  64.         FileInputStream in = new FileInputStream(srcFile);//源文件  
  65.         FileOutputStream out = new FileOutputStream(destFile);//目标文件  
  66.         BufferedInputStream inbuf = new BufferedInputStream(in);  
  67.         BufferedOutputStream outbuf = new BufferedOutputStream(out);  
  68.         int b;  
  69.         while ((b = inbuf.read())!=-1) {  
  70.             outbuf.write(b);  
  71.         }  
  72.         in.close();  
  73.         out.close();  
  74.         inbuf.close();  
  75.         outbuf.close();  
  76.     }  
  77.     /** 
  78.      * 通过缓冲流copy文件 缓冲流一次读取多个字节 
  79.      * @throws IOException 
  80.      */  
  81.     public static void copyFileByBuffedBuf(File srcFile,File destFile)throws IOException{  
  82.         if(!srcFile.exists()){  
  83.             throw new IllegalArgumentException("文件:"+srcFile+"不存在!");  
  84.         }  
  85.         if(!srcFile.isFile()){  
  86.             throw new IllegalArgumentException(srcFile+"不是文件!");  
  87.         }  
  88.         FileInputStream in = new FileInputStream(srcFile);//源文件  
  89.         FileOutputStream out = new FileOutputStream(destFile);//目标文件  
  90.         BufferedInputStream inbuf = new BufferedInputStream(in);  
  91.         BufferedOutputStream outbuf = new BufferedOutputStream(out);  
  92.         int b;  
  93.         byte[] buf = new byte[8*1024];  
  94.         while ((b = inbuf.read(buf))!=-1) {  
  95.             outbuf.write(buf,0,b);  
  96.         }  
  97.         in.close();  
  98.         out.close();  
  99.         inbuf.close();  
  100.         outbuf.close();  
  101.     }  
  102.       
  103.     /** 
  104.      * 文件拷贝 
  105.      * @param srcpath 
  106.      * @param destpath 
  107.      * @throws IOException 
  108.      */  
  109.     public static void copyFile(String srcpath,String destpath)throws IOException{  
  110.         copyFileByBuffedBuf(new File(srcpath), new File(destpath));  
  111.     }  
  112. }  

   
调用 : IOUtil.copyFile("C:\\apache-tomcat-7.0.57.zip", "c:\\ret.dat");//源文件大小9M

copyFileByByte  ----》 62229 毫秒
copyFileByByteBuf -----》 97 毫秒
copyFileByBuffed -----》 457 毫秒
copyFileByBuffedBuf ----》 28 毫秒

0 0