通过字节流复制大文件内容到指定的文件

package com.neusoft.copyfile;


/**
 * @time  2014-8-15 上午10:32:46
 * @author new
 * @function 复制文件内容到指定的文件
 * 
 */

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

/**
 * 
 */
public class CopyFileByStream {

	public static void main(String[] args) throws IOException {
		
		long start=System.currentTimeMillis();
		
		CopyFileByStream cFileByStream=new CopyFileByStream();
		// 创建一个 FileInputStream对象 
		String oldFileName="123.avi";
		String newFileName="copy_"+oldFileName;
		File oldFile=new File(oldFileName);
		File newFile=new File(newFileName);
		if (oldFile.exists()) {
			if (!newFile.exists()) {
				cFileByStream.copyFile(oldFile, newFile);
			}else {
				System.out.println("老大,"+newFileName+"文件已经存在,我马上着手删除!");
				newFile.delete();
				cFileByStream.copyFile(oldFile, newFile);
				System.out.println("老大,"+newFileName+"文件已经准备好,请用!");
			}
		}else {
			System.out.println("老大,你说的那个文件我没找到啊,我停下啦,您再找找,小弟我无能为力了!");
		}
        long end=System.currentTimeMillis();
        System.out.println((end-start)/1000.0+"秒");
		System.out.println("结束");
	}
	
	/**
	 * @function 复制文件
	 * @param oldFile
	 * @param newFile
	 * @throws IOException
	 */
	public void copyFile(File oldFile,File newFile) throws IOException{
		FileInputStream fInputStream=new  FileInputStream(oldFile);
		FileOutputStream fOutputStream=new FileOutputStream(newFile,true);
		// 确定文件的大小
		//int fileSize = fInputStream.available();
		
		byte[] strByte = new byte[1024*1024*2];
		System.out.println("设置的缓冲区大小:"+strByte.length/1024.0+"K");
		int len=0;
		int count=0;
        while((len=fInputStream.read(strByte))!= -1){
           // String str1=new String(strByte);
            System.out.println(len/1024.0+"K 缓冲区循环第"+(++count)+"次");
            fOutputStream.write(strByte,0,len);     
        }
        
        fOutputStream.close();
        fInputStream.close();
	}

}
控制台显示结果:
老大,copy_123.avi文件已经存在,我马上着手删除!
设置的缓冲区大小:2048.0K
2048.0K 缓冲区循环第1次
2048.0K 缓冲区循环第2次

2048.0K 缓冲区循环第3次

.......

2048.0K 缓冲区循环第77次
2048.0K 缓冲区循环第78次
2048.0K 缓冲区循环第79次
415.140625K 缓冲区循环第80次
老大,copy_123.avi文件已经准备好,请用!
3.484秒
结束

转载于:https://www.cnblogs.com/JZZ1026/p/4417337.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值