filePieceSize = 1024*1024*64;
}
将路径转换为文件对象,再计算将分割多少块:
File file = filePath.toFile();
int howManyParts = (int) Math.ceil(file.length() / (double)filePieceSize);
初始化输入输出流,出错输出错误信息,返回false,获得当前目录:
DataInputStream fileReader = null;
try {
fileReader = new DataInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件找不到!");
return false;
}
DataOutputStream fileWriter;
Path dir = filePath.getParent();
接下来读取文件,并且分别输出到各个part文件中:
int readLength = -1;
long total = 0;
try {
for (int i = 1; i <= howManyParts ; i++){
//新建文件part i
Path temp = Files.createFile(dir.resolve(filePath.getFileName() + ".part" + i));
//搭建输出流
fileWriter = new DataOutputStream(new FileOutputStream(temp.toFile()));
//读取文件并输出
while ( (readLength = fileReader.read(buffer)) != -1){
fileWriter.write(buffer,0,readLength);
fileWriter.flush();
total += readLength;
if (total == filePieceSize){
total = 0;
break;
}
}
//part i的文件已经输出完毕,关闭流
fileWriter.close();
}
//读取完毕,关闭输入流
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO错误!");
return false;
}
该函数已经实现完毕,接下来测试(由于电影Fury有14G。。太大了。。还是换个吧):

我是大哥大第5集,有729M,大概能分个12个part吧。
public static void main(String[] args) throws IOException {
double before = System.currentTimeMillis();
Path bigboss = Paths.get("D:\\Video\\我是大哥大\\我是大哥大.Kyou.kara.Ore.wa.Ep05.Chi_Jap.HDTVrip.1280X720.mp4");
FileSlice.slice(bigboss,-1);
double after = System.currentTimeMillis();
System.out.println("分割文件我是大哥大.Kyou.kara.Ore.wa.Ep05.Chi_Jap.HDTVrip.1280X720.mp4," + Files.size(bigboss) + "字节,总用时" + (after - before) + "ms" );
}
运行结果:
分割文件我是大哥大.Kyou.kara.Ore.wa.Ep05.Chi_Jap.HDTVrip.1280X720.mp4,765321889字节,总用时16335.0ms

速度还是挺慢的。。 下次还是换成多线程来实现,再来测试下速度。在单线程情况下一个普通的40分钟日剧都要15-30s左右,要是mkv格式的电影都要好久了。。不过其实极限应该不在CPU中执行的速度,而是在硬盘IO中,如果是普通硬盘那么就算是多线程也应该提速不了多少。。
### 文件拼接
这个就很简单了,和分割相反就OK。 直接上完整代码:
public static boolean glue(Path filePath, int howManyParts){
if (!Files.exists(filePath)){
return false;
}
//获取原始文件名
String filename = getOriginalFileName(filePath.getFileName().toString());
if (filename == null){
System.out.println("传入part文件名解析出错!");
return false;
}
//初始化缓冲区
byte [] buffer = new byte[1024 * 8];
//获取文件存储的路径
Path dir = filePath.getParent();
try {
DataInputStream fileReader = null;
//创建原始文件
Files.createFile(dir.resolve(filename));
//搭建原始文件输出流
DataOutputStream fileWriter = new DataOutputStream(new FileOutputStream(dir.resolve(filename).toFile()));
int readLength = -1;
for (int i = 1; i <= howManyParts ; i++){
//得到part i文件路径
Path temp = dir.resolve(filename + ".part" + i);
//搭建输入流
fileReader = new DataInputStream(new FileInputStream(temp.toFile()));
//读取文件并输出
while ( (readLength = fileReader.read(buffer)) != -1){
fileWriter.write(buffer,0,readLength);
fileWriter.flush();
}
//part i的文件已经读入完毕,关闭流
fileReader.close();
}
//写入完毕,关闭输出流
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
最后
这份文档从构建一个键值数据库的关键架构入手,不仅带你建立起全局观,还帮你迅速抓住核心主线。除此之外,还会具体讲解数据结构、线程模型、网络框架、持久化、主从同步和切片集群等,帮你搞懂底层原理。相信这对于所有层次的Redis使用者都是一份非常完美的教程了。
CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】
整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~
你的支持,我的动力;祝各位前程似锦,offer不断!!!
、线程模型、网络框架、持久化、主从同步和切片集群等,帮你搞懂底层原理。相信这对于所有层次的Redis使用者都是一份非常完美的教程了。
[外链图片转存中…(img-BIHxt2aj-1630379106504)]
CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】
整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~
你的支持,我的动力;祝各位前程似锦,offer不断!!!