Java中文件的拷贝

一、字节流的基本知识:

   1.输入流:InputStream (in ) 输出流:OutputStream (out)

   2.输入流的基本方法:

        *int b = in.read();                                       //读取一个字节

        *int len =in.read(byte [ ] buf);                   //读取一个字节数组

        *int len = in.read(byte [ ] buf,int start, int size)  //指定从字节组的哪里开始读

   3.输出流的基本方法:(与输入流相对应)

        *out.write();                                     

        *out.write(byte [ ] buf);                   

        *out.write(byte [ ] buf,int start, int size)  


二、我们分别用单字节的方法,批量字节的方法,以及带缓冲区的方法来拷贝文件,比较一下各自的性能


源代码:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileCopy {
	//单字节拷贝方法
	public void copyByByte(File srcFile,File desFile) throws IOException{
		try {
			
			if(!srcFile.exists()){           //如果文件不存在,抛出文件不存在的异常
				throw new IllegalArgumentException(srcFile+"不存在");
			}
			
			if(!srcFile.isFile())         //如何给定的路径不是文件,抛出不是文件的异常
				throw new IllegalArgumentException(srcFile+"不是文件");
			
			InputStream is = new FileInputStream(srcFile);
			
			OutputStream os = new FileOutputStream(desFile);
			
			int b=0;
			
			while((b=is.read())!=-1){  //直到读到文件末尾时结束读
				os.write(b);
			}
			
			
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		}
	}
	
	//批量字节拷贝
	public void copyByBufByte(File srcFile,File desFile) throws IOException{
		
		try {
			
			if(!srcFile.exists()){
				throw new IllegalArgumentException(srcFile+"不存在");
			}
			
			if(!srcFile.isFile())
				throw new IllegalArgumentException(srcFile+"不是文件");
			
			InputStream is = new FileInputStream(srcFile);
			OutputStream os = new FileOutputStream(desFile);
			
			byte [] buf = new byte[1024*4];
			int len =0;
			
			while((len=is.read(buf, 0, buf.length))!=-1){
				os.write(buf, 0, len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
public void copyByBuffered(File srcFile,File desFile) throws IOException{
		
	
	if(!srcFile.exists()){
		throw new IllegalArgumentException(srcFile+"不存在");
	}
	
	if(!srcFile.isFile())
		throw new IllegalArgumentException(srcFile+"不是文件");
	
		FileInputStream is =  new FileInputStream(srcFile);
		
		BufferedInputStream bis = new BufferedInputStream(is);
		
		FileOutputStream os = new FileOutputStream(desFile);
		BufferedOutputStream bos = new BufferedOutputStream(os);
		
		int len = bis.read();
		
		while(len!=-1){
			bos.write(len);
			len = bis.read();
		}
}

	public static void main(String[] args) {
		
		FileCopy copy = new FileCopy();
		File srcFile =new File("e:\\演员.mp3");
		File desFile = new File("e:\\1演员.mp3");
		File desFile1 = new File("e:\\2演员.mp3");
		File desFile2 = new File("e:\\3演员.mp3");
		try {
			//分别计算一下各自的拷贝时间
			long start1 = System.currentTimeMillis();
                        copy.copyByByte(srcFile, desFile);
                        long end1 =System.currentTimeMillis();
                        System.out.print("单字节拷贝所花的时间:");
                        System.out.println(end1-start1);

                        long start2 = System.currentTimeMillis();
                        copy.copyByBufByte(srcFile, desFile1);
                        long end2 =System.currentTimeMillis();
                        System.out.print("批量字节拷贝所花的时间:");
                        System.out.println(end2-start2);


                        long start3 = System.currentTimeMillis();
                        copy.copyByBuffered(srcFile, desFile2);
                        long end3 =System.currentTimeMillis();
                        System.out.print("带缓冲字节拷贝所花的时间:");
                        System.out.println(end3-start3);
			
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

运行结果:



通过运行结果可知:批量字节处理程序所花时间最少!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值