java 文件分割与合并
import java.io.*;
import java.util.Arrays;
public class FileDivisionMergeDemo {
/**
* 文件分割
*
* @param url 文件
* @param cutSize 每个文件的大小
* @param fileName 文件名称
* @param address 分割到的地址
* @param suffix 后缀
*/
private static void division(String url, long cutSize, String fileName, String address, String suffix) {
File targetFile = new File(url);
// 判断文件是否为空
if (targetFile == null || !targetFile.exists()) return;
if (fileName == null) {
String[] strArray = targetFile.getName().split("\\.");
Arrays.copyOf(strArray, strArray.length - 1);
fileName = String.join("", strArray);
}
if (address == null) {
String a = targetFile.getAbsolutePath();
address = a.split(targetFile.getName())[0] + fileName + "\\";
}
if (suffix == null) {
suffix = ".tmp";
}
File file = new File(address);
if (!file.exists()) {
file.mkdirs();
}
// 分成几个文件
int num = (int) ((targetFile.length() % cutSize == 0) ? (targetFile.length() / cutSize) : ((targetFile.length() / cutSize) + 1));
try {
// 获取文件流 并将文件进入缓存流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(targetFile));
BufferedOutputStream out = null;
int count = 0;
int len = -1;
byte[] bytes = null;
for (int i = 0; i < num; i++) {
out = new BufferedOutputStream(new FileOutputStream(address + fileName + i + suffix));
// 每个byte数组最多1024 求要处理几次
if (cutSize > 1024) {
count = (int) (cutSize / 1024);
bytes = new byte[1024];
} else {
count = 1;
bytes = new byte[(int) cutSize];
}
while (count > 0 && (len = bis.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
count--;
}
if ((cutSize % 1024) != 0) {
bytes = new byte[(int) (cutSize % 1024)];
len = bis.read(bytes);
out.write(bytes, 0, len);
out.flush();
out.close();
}
out.close();
}
System.out.println("分割完成");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 合并文件
*
* @param url 文件夹路径
* @param name 要合成的文件名称
* @param address 合成后需要放到的文件路径
* @param isDelete 是否删除缓存文件
*/
private static void merge(String url, String name, String address, Boolean isDelete) {
File file = new File(url);
if (!file.exists()) {
System.out.println("文件夹地址不存在");
return;
}
if (!file.canRead()) {
System.out.println("文件地址不可读");
return;
}
if (new File(url + name).exists()) {
System.out.println("目标文件已存在");
return;
}
if (isDelete == null) {
isDelete = true;
}
File[] files = file.listFiles();
System.out.println(files.length);
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(url + "/../" + name));
for (File f : files) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
byte[] bytes = new byte[1024];
int len = -1;
while ((len = bis.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
bis.close();
//删除缓存文件
if (isDelete) {
if (f.isFile() && f.exists()) {
f.delete();
}
}
}
//删除缓存夹
if (isDelete) {
File driFile = new File(url);
if (driFile.exists()) {
driFile.delete();
}
}
out.close();
System.out.println("合并完成");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
// 分割文件
// division("F:\\测试\\开不了口演唱会.mp4",1024*1024*3,null,null,".tmp");
// 合并文件
merge("F:\\测试\\开不了口演唱会mp4", "1.mp4", null, true);
}
}
效果图