`
JavaSam
  • 浏览: 934112 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA IO 字节流实现文件copy效率比较

    博客分类:
  • JAVA
阅读更多
package jonavin.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IOUtil {

	/**
	 * 文件拷贝-- 一个字节一个字节拷贝
	 * @param srcFile
	 * @param destFile
	 */
	public static void copyFileByByte(File srcFile,File destFile) throws IOException{
		if(!srcFile.exists()){
			throw new IllegalArgumentException("文件:"+srcFile+"不存在!");
		}
		if(!srcFile.isFile()){
			throw new IllegalArgumentException(srcFile+"不是文件!");
		}
		FileInputStream in = new FileInputStream(srcFile);//源文件
		FileOutputStream out = new FileOutputStream(destFile);//目标文件
		int b;
		while ((b = in.read()) != -1) {
			out.write(b);//写入一个字节的低八位
		}
		in.close();
		out.close();
	}
	/**
	 * 拷贝文件一次读取多个字节
	 */
	public static void copyFileByByteBuf(File srcFile,File destFile) throws IOException{
		if(!srcFile.exists()){
			throw new IllegalArgumentException("文件:"+srcFile+"不存在!");
		}
		if(!srcFile.isFile()){
			throw new IllegalArgumentException(srcFile+"不是文件!");
		}
		FileInputStream in = new FileInputStream(srcFile);//源文件
		FileOutputStream out = new FileOutputStream(destFile);//目标文件
		int b;
		byte[] buf = new byte[8*1024];//开辟8K的缓存
		while ((b = in.read(buf,0,buf.length))!=-1) {
			out.write(buf, 0, b);
		}
		in.close();
		out.close();
	}
	/**
	 * 通过缓冲流copy文件
	 * @throws IOException
	 */
	public static void copyFileByBuffed(File srcFile,File destFile)throws IOException{
		if(!srcFile.exists()){
			throw new IllegalArgumentException("文件:"+srcFile+"不存在!");
		}
		if(!srcFile.isFile()){
			throw new IllegalArgumentException(srcFile+"不是文件!");
		}
		FileInputStream in = new FileInputStream(srcFile);//源文件
		FileOutputStream out = new FileOutputStream(destFile);//目标文件
		BufferedInputStream inbuf = new BufferedInputStream(in);
		BufferedOutputStream outbuf = new BufferedOutputStream(out);
		int b;
		while ((b = inbuf.read())!=-1) {
			outbuf.write(b);
		}
		in.close();
		out.close();
		inbuf.close();
		outbuf.close();
	}
	/**
	 * 通过缓冲流copy文件 缓冲流一次读取多个字节
	 * @throws IOException
	 */
	public static void copyFileByBuffedBuf(File srcFile,File destFile)throws IOException{
		if(!srcFile.exists()){
			throw new IllegalArgumentException("文件:"+srcFile+"不存在!");
		}
		if(!srcFile.isFile()){
			throw new IllegalArgumentException(srcFile+"不是文件!");
		}
		FileInputStream in = new FileInputStream(srcFile);//源文件
		FileOutputStream out = new FileOutputStream(destFile);//目标文件
		BufferedInputStream inbuf = new BufferedInputStream(in);
		BufferedOutputStream outbuf = new BufferedOutputStream(out);
		int b;
		byte[] buf = new byte[8*1024];
		while ((b = inbuf.read(buf,0,buf.length))!=-1) {
			outbuf.write(buf,0,b);
		}
		in.close();
		out.close();
		inbuf.close();
		outbuf.close();
	}
	
	/**
	 * 文件拷贝
	 * @param srcpath
	 * @param destpath
	 * @throws IOException
	 */
	public static void copyFile(String srcpath,String destpath)throws IOException{
		copyFileByBuffedBuf(new File(srcpath), new File(destpath));
	}
}

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

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

 

2
1
分享到:
评论
4 楼 四书五经 2014-12-25  
   博主这个程序基本没错,但是你的测试有很大问题,测试的时候不应该用同一个文件来测试,而是应该用不同的文件来测试,因为同一文件测试,除了第一次,后面的读取会有缓存导致的误差,所有你这个结果不准确的。
而且,最后其实不管是管道还是第三方包(apache io common 包)还是字节, 结果应该都是fileInputStream + buffer(类似你这里的copyFileByByteBuf)是最快的。
3 楼 随便小屋 2014-12-25  
      
2 楼 Even521 2014-12-25  
@Test 
public void testFileChanne(){ 
  FileInputStream fi = null; 
  FileOutputStream fo = null; 
  FileChannel in = null; 
  FileChannel out = null; 
  try { 
   fi = new FileInputStream("d:\\ff_copy.txt"); 
   fo = new FileOutputStream("d:\\ff_copy3.txt"); 
   in = fi.getChannel();//得到对应的文件通道 
   out = fo.getChannel();//得到对应的文件通道 
   in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道 
  } catch (IOException e) { 
   e.printStackTrace(); 
  } finally { 
   try { 
    fi.close(); 
    in.close(); 
    fo.close(); 
    out.close(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
  } 
1 楼 jilin 2014-12-24  
通过管道拷贝更快

相关推荐

Global site tag (gtag.js) - Google Analytics