forked from fishsticks89/hw-topics-git-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirUtil.java
More file actions
46 lines (41 loc) · 1.2 KB
/
DirUtil.java
File metadata and controls
46 lines (41 loc) · 1.2 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
import java.io.File;
public class DirUtil {
static String removeBeginningSlash(String dir) {
if (dir.length() == 0)
return dir;
if (dir.charAt(0) == '/') {
return dir.substring(1);
}
return dir;
}
static String up(String dir) {
dir = removeBeginningSlash(dir);
final var split = dir.split("/");
StringBuffer combined = new StringBuffer();
for (int i = 0; i < split.length - 1; i++) {
combined.append(split[i]);
if (i != split.length - 2) {
combined.append("/");
}
}
return combined.toString();
}
static String last(String dir) {
final var split = dir.split("/");
return split[split.length - 1];
}
// deletes directories recursively (gets rid of the subfiles too)
public static void deleteDir(File dir) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteDir(file);
} else {
file.delete();
}
}
}
dir.delete();
}
}