import java.io.*;
class DownThread extends Thread {
//定义字节数组(取水的竹筒)的长度
private final int BUFF_LEN = 32;
//定义读取的起始点
private long start;
//定义读取的结束点
private long end;
//读取文件对应的输入流
private InputStream is;
//将读取到的字节输出到raf中
private RandomAccessFile raf;
//构造器,传入输入流,输出流和读取起始点、结束点
public DownThread(long start, long end, InputStream is, RandomAccessFile raf) {
//输出该线程负责读取的字节位置
System.out.println(start + "---->" + end);
this.start = start;
this.end = end;
this.is = is;
this.raf = raf;
}
public void run() {
try {
is.skip(start);
raf.seek(start);
//定义读取输入流内容的的缓存数组(竹筒)
byte[] buff = new byte[BUFF_LEN];
//本线程负责读取文件的大小
long contentLen = end - start;
//定义最多需要读取几次就可以完成本线程的读取
long times = contentLen / BUFF_LEN + 4;
//实际读取的字节数
int hasRead = 0;
for (int i = 0; i < times; i++) {
hasRead = is.read(buff);
//如果读取的字节数小于0,则退出循环!
if (hasRead < 0) {
break;
}
raf.write(buff, 0, hasRead);
}
} catch (Exception ex) {
ex.printStackTrace();
}
//使用finally块来关闭当前线程的输入流、输出流
finally {
try {
if (is != null) {
is.close();
}
if (raf != null) {
raf.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
public class MutilDown {
public static void main(String[] args) {
final int DOWN_THREAD_NUM = 4;
final String OUT_FILE_NAME = "d:/copy勇敢的心.rmvb";
InputStream[] isArr = new InputStream[DOWN_THREAD_NUM];
RandomAccessFile[] outArr = new RandomAccessFile[DOWN_THREAD_NUM];
try {
isArr[0] = new FileInputStream("d:/勇敢的心.rmvb");
long fileLen = getFileLength(new File("d:/勇敢的心.rmvb"));
System.out.println("文件的大小" + fileLen);
//以输出文件名创建第一个RandomAccessFile输出流
outArr[0] = new RandomAccessFile(OUT_FILE_NAME, "rw");
//创建一个与文件相同大小的空文件
for (int i = 0; i < fileLen; i++) {
outArr[0].write(0);
}
//每线程应该读取的字节数
long numPerThred = fileLen / DOWN_THREAD_NUM;
//整个文件整除后剩下的余数
long left = fileLen % DOWN_THREAD_NUM;
for (int i = 0; i < DOWN_THREAD_NUM; i++) {
//为每个线程打开一个输入流、一个RandomAccessFile对象,
//让每个线程分别负责读取文件的不同部分。
if (i != 0) {
isArr[i] = new FileInputStream("d:/勇敢的心.rmvb");
//以指定输出文件创建多个RandomAccessFile对象
outArr[i] = new RandomAccessFile(OUT_FILE_NAME, "rw");
}
if (i == DOWN_THREAD_NUM - 1) {
//最后一个线程读取指定numPerThred+left个字节
new DownThread(i * numPerThred, (i + 1) * numPerThred
+ left, isArr[i], outArr[i]).start();
} else {
//每个线程负责读取一定的numPerThred个字节
new DownThread(i * numPerThred, (i + 1) * numPerThred,
isArr[i], outArr[i]).start();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static long getFileLength(File file) {
long length = 0;
//获取文件的长度
long size = file.length();
length = size;
return length;
}
}
分享到:
相关推荐
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
下单系统的Spnigboot和微信小程序实现(全栈微信小程式下单)
该项目是一款基于Java的智能文件管家设计源码,涵盖102个文件,包括29个Java源文件、27个类文件、19个XML配置文件、10个YAML文件、8个列表文件、4个属性文件、4个JAR包文件以及1个Git忽略文件。该系统旨在提供高效便捷的文件管理解决方案。
基于YoloV8的简单目标检测和跟踪,使用KMNET进行鼠标移动(处理多目标移动抖动,处理鼠标平滑移动)
本项目是一款基于Vue和JavaScript开发的心旅途个性化推荐旅游平台设计源码,整合了513个Java文件、76个PNG图片、70个XML配置文件、62个JavaScript文件、42个Vue组件文件、28个CSS样式文件、22个HTML文件、18个YAML配置文件、16个属性文件、11个Vue模板文件,总计919个文件。平台采用现代化前端技术堆栈,旨在为用户提供个性化的旅游推荐服务。
AutoLine是一个基于Python的通用自动化测试开源平台,包含了657个文件,涵盖228个PNG图片、209个CSS样式、95个JavaScript脚本、39个Python源代码、21个HTML文件、19个XML文件、14个GIF图片、6个DS_Store文件、5个文本文件、4个Markdown文件。该平台的设计源码由多种编程语言编写,旨在提供灵活高效的自动化测试解决方案。
微信小程序图像裁剪工具_ e-cropper
【作品名称】:基于MATLAB的答题卡识别系统。带一个GUI可视化界面,通过输入答题卡旋转校正,边缘检测,霍夫曼变换检测答题卡填涂区域 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: 传统的阅卷方式为流水线的手工作业方式。这样的方式存在很多的问题,因为很容易受到阅卷者的主观因素的影响,从而产生一定的偏差。所以很多人就讨论如何将人为的因素降低到最低,来确保考生的考试成绩的公平公正和准确。 基于MATLAB的答题卡识别系统。带一个GUI可视化界面,通过输入答题卡旋转校正,边缘检测,霍夫曼变换检测答题卡填涂区域,分割,识别属于ABCD等,通过和实现设置好的标准答案excel对比,从而得出最终分数 【资源声明】:本资源作为“参考资料”而不是“定制需求”,代码只能作为参考,不能完全复制照搬。需要有一定的基础能够看懂代码,能够自行调试代码并解决报错,能够自行添加功能修改代码。
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
本项目深入解析并实现了基于Java核心技术的Nacos配置中心,包含2707个文件,涵盖2180个Java源文件、177个JavaScript文件、52个XML文件、35个SCSS文件、22个PEM文件、20个属性文件、18个Markdown文件、16个Protocol Buffers文件、12个JSON文件、11个字体文件。项目涉及多种语言和技术,旨在提供一个全面的配置中心解决方案。
枝晶生长Comsol仿真模型。 锂枝晶生长过程的 枝晶生长Comsol仿真模型。 锂枝晶生长过程的枝晶形貌,温度场耦合,应力场,浓度场,电势场。 C++程序,基于元胞自动机法模拟枝晶生长,能实现任意角度(偏心正方算法),同时采用LBM考虑了对流作用对枝晶生长的影响
本项目为apple_pro客户关系管理系统的组件化开发源码,采用Python、CSS、HTML和JavaScript等多种语言编写,总计包含1078个文件。其中,Python源文件254个,Python编译后文件244个,CSS样式文件65个,HTML模板61个,JavaScript脚本40个,以及其他类型文件如LESS、SCSS、XML、PNG等。该系统通过组件化设计,旨在提升客户关系管理的效率与用户体验。
微信小程序日历插件_Calendar
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
另一个小型购物中心。Litemall=Spring Boot后端+Vue管理员前端+微信小程序用户前端+Vue用户移动端_stemall
该项目为基于GitHub的ESPnet语音处理工具包设计源码,包含10633个文件,涵盖Shell脚本、Python、MATLAB、C++等多种编程语言。文件类型包括2872个shell脚本、2303个YAML配置文件、1662个Python脚本、1567个配置文件、306个Markdown文件、223个Perl脚本、39个文本文件、35个Bash脚本、27个PNG图片、21个补丁文件。该项目定期更新,适用于语音处理领域的研究与开发。
该项目是一款基于Python开发的pyecharts可视化图表库源码,包含166个文件,涵盖了121个Python源文件、12个HTML文件、9个JSON文件、6个PNG图片文件、4个Markdown文件、3个文本文件、2个YAML文件以及少量配置和管理文件。该库旨在提供强大的数据可视化解决方案,适用于各种数据分析与展示需求。
STM32软件学习资料GPS与GPRSSTM32软件学习资料GPS与GPRS
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据