FileUtil.java
2.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.skua.common.util;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;
/**
* @author sonin
* @date 2021/12/15 17:05
*/
public class FileUtil {
/**
* 创建文件夹
*
* @param path
* @return
*/
public static Boolean mkDirs(String path) {
File file = new File(path);
if (!file.exists()) {
return file.mkdirs();
}
return true;
}
/**
* 合并分片
*
* @param chunkFileList
* @param mergeFile
*/
public static void shardMerge(List<File> chunkFileList, File mergeFile) {
try {
// 有删 无创建
if (mergeFile.exists()) {
mergeFile.delete();
} else {
mergeFile.createNewFile();
}
// 排序
chunkFileList.sort((o1, o2) -> {
if (Integer.parseInt(o1.getName()) > Integer.parseInt(o2.getName())) {
return 1;
}
return -1;
});
byte[] b = new byte[1024];
RandomAccessFile writeFile = new RandomAccessFile(mergeFile, "rw");
for (File chunkFile : chunkFileList) {
RandomAccessFile readFile = new RandomAccessFile(chunkFile, "r");
int len;
while ((len = readFile.read(b)) != -1) {
writeFile.write(b, 0, len);
}
readFile.close();
}
writeFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除文件
*
* @param file
* @return
*/
public static boolean delFile(File file) {
if (!file.exists()) {
return false;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
delFile(f);
}
}
return file.delete();
}
}