Java多线程下模拟文件下载

   将一个文件分为四个部分,采用多线程模拟下载。

public class Test3Download {
	public static void main(String[] args) {
		int fileLength = 0;
		// 1 创建文件对象
		File file = new File("C:\\123.mp4");
		// 2 拿到文件的长度
		if (file.exists()) {
			fileLength = (int) file.length();
		}
		// 拿到每个线程需要执行的长度
		int length = fileLength % 4 == 0 ? fileLength / 4 : fileLength / 4 + 1;
		// 如果 取余4 不等于0 每一个就多读取一个字节
		// 算一下最后一个需要读取的长度

		// 最后一块的长度
		int lastLength = fileLength - length * 3;

		// 启动线程

		DownLoad l1 = new DownLoad(0, length);// 26 0-25
		DownLoad l2 = new DownLoad(length, length);// 26-52 第一个lenth 起点
													// 第二个lenght 长度
		DownLoad l3 = new DownLoad(2 * length, length);
		DownLoad l4 = new DownLoad(3 * length, lastLength);

		// 启动线程
		l1.start();
		l2.start();
		l3.start();
		l4.start();

	}
}

class DownLoad extends Thread {
	// 需要什么? 需要确定开始的位置 和 读取的长度

	private int start;
	private int length;

	public DownLoad(int start, int length) {
		super();
		this.start = start;
		this.length = length;
	}

	@Override
	public void run() {
		// 随机读写流
		// 注意: 一个随机读写流不能同时去读和写,想要读和写,那就创建两个随机读写流对象
		RandomAccessFile read = null;
		RandomAccessFile write = null;
		try {
			System.out.println(Thread.currentThread().getName() + "开始复制...");
			read = new RandomAccessFile("C:\\123.mp4", "rw");
			write = new RandomAccessFile("D:\\1234.mp4", "rw");
			// 跳过指定的位置
			read.seek(start);
			write.seek(start); // 重点标注

			// 读取 数组的长度和需要读取的数据相同
			byte[] b = new byte[4096];
			int len = 0;
			int i = 0;

			while (true) {
				if (length - i < b.length) {
					read.read(b, 0, length - i);
					write.write(b, 0, length - i);
					break;
				} else {
					len = read.read(b);
					i += len;
					write.write(b, 0, len);
				}

			}

			System.out.println(Thread.currentThread().getName() + "复制完成");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (read != null) {
				try {
					read.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (write != null) {
				try {
					write.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、付费专栏及课程。

余额充值