FileInputStream复制文件,实时显示速度

package test;

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

public class DownTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		File iFile = new File("F:\\新建文件夹\\肖申克的救赎.重制版.国英双语.1994.中英字幕£CMCT暮雨潇潇\\[肖申克的救赎(国英双语)].The.Shawshank.Redemption.1994.BluRay.720p.x264.AC3.2Audios-CMCT.mkv");
		File oFile = new File("d:\\test\\[肖申克的救赎(国英双语)].The.Shawshank.Redemption.1994.BluRay.720p.x264.AC3.2Audios-CMCT.mkv");
		FileInputStream fis = null;
		FileOutputStream fos = null;
		long s = 0; //记录开始时间
		long sta = 0;//记录每秒开始时间
		long end = 0;//记录每次写入时间
		long oldlength = 0;//写入前文件大写
		long newlength = 0;//记录1秒后写入大小
		byte[] buf = new byte[1024];// 定义临时数组
		int tlength = 0;//定义临时变量保存长度。
		try {
			fis = new FileInputStream(iFile);
			fos = new FileOutputStream(oFile);
			s = sta = System.currentTimeMillis();//得到当前时间毫秒数
			oldlength = oFile.length();//得到旧文件长度
			while((tlength = fis.read(buf)) != -1){
				fos.write(buf, 0, tlength);
				end = System.currentTimeMillis();
				if(end - sta > 1024){
					sta = end;
					newlength = oFile.length();
					System.out.println("每秒钟传送"+ (newlength - oldlength) / (1024 * 1024) + "M");
					oldlength = newlength;
				}
			}
			System.out.println("总共耗时" + (end - s) / (1000 *60) + "分" + ((end - s) % (1000 *60)) / 1000 + "秒");
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			try {
				fos.flush();
				fos.close();
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值