-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFile.java
More file actions
62 lines (54 loc) · 1.88 KB
/
Copy pathMainFile.java
File metadata and controls
62 lines (54 loc) · 1.88 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
package ru.javawebinar.basejava;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class MainFile {
public static void main(String[] args) {
File rootDir = new File(".");
printDirectoryDeeply(rootDir, "");
}
public static void printDirectoryDeeply(File dir, String indent) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
System.out.println(indent + file.getName());
} else if (file.isDirectory()) {
System.out.println(indent + "- " + file.getName());
printDirectoryDeeply(file, " " + indent);
}
}
}
}
public static Collection<File> getFiles(File rootDir) {
Set<File> fileSet = new HashSet<>();
if (rootDir == null || rootDir.listFiles() == null) {
return fileSet;
}
for (File entry : Objects.requireNonNull(rootDir.listFiles())) {
if (entry.isFile()) {
fileSet.add(entry);
} else {
fileSet.addAll(getFiles(entry));
}
}
return fileSet;
}
private static void filesWalk(String rootDir) throws IOException {
Files.walk(Paths.get("."))
.filter(Files::isRegularFile)
.map(Path::getFileName)
.forEach(System.out::println);
}
private static void filesFind(String rootDir) throws IOException {
Files.find(Paths.get("."), Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile())
.map(Path::getFileName)
.forEach(System.out::println);
}
}