-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
35 lines (32 loc) · 1.07 KB
/
Main.java
File metadata and controls
35 lines (32 loc) · 1.07 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
import java.io.File;
public class Main {
public static void main(String[] args) {
File dir = new File("C:/AmericasCardroom");
try {
System.out.printf("Общий размер директории и вложенных файлов: %d байт", getDirSize(dir));
} catch (IllegalArgumentException iAE) {
System.out.println(iAE.getMessage());
}
}
static long getDirSize(File dir) throws IllegalArgumentException {
long size = 0;
if (dir == null) {
throw new IllegalArgumentException("Директория пуста");
}
if (dir.isFile()) {
size = dir.length();
} else {
File[] subFiles = dir.listFiles();
if (subFiles != null) {
for (File file : subFiles) {
if (file.isFile()) {
size += file.length();
} else {
size += getDirSize(file);
}
}
}
}
return size;
}
}