forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
70 lines (55 loc) · 2.46 KB
/
Main.java
File metadata and controls
70 lines (55 loc) · 2.46 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
package modern.challenge;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws IOException {
Path startPath = Paths.get("D:/learning");
System.out.println("Find all files ending with the '.properties' extension and following symbolic links: ");
Stream<Path> resultAsStream1
= Files.find(
startPath,
Integer.MAX_VALUE,
(path, attr) -> path.toString().endsWith(".properties"),
FileVisitOption.FOLLOW_LINKS
);
resultAsStream1.forEach(System.out::println);
System.out.println("\n\nFind all regular files whose names start with 'application': ");
Stream<Path> resultAsStream2
= Files.find(
startPath,
Integer.MAX_VALUE,
(path, attr) -> attr.isRegularFile()
&& path.getFileName().toString().startsWith("application")
);
resultAsStream2.forEach(System.out::println);
System.out.println("\n\nFind all directories created after 16 March 2019: ");
Stream<Path> resultAsStream3
= Files.find(
startPath,
Integer.MAX_VALUE,
(path, attr) -> attr.isDirectory()
&& attr.creationTime().toInstant().
isAfter(LocalDate.of(2019, 3, 16).atStartOfDay().
toInstant(ZoneOffset.UTC))
);
resultAsStream3.forEach(System.out::println);
System.out.println("\n\nFind all Java files: ");
Stream<Path> resultAsStream4 = fetchFilesMatching(startPath, "glob:**/*.java");
resultAsStream4.forEach(System.out::println);
}
private static Stream<Path> fetchFilesMatching(Path root, String syntaxPattern)
throws IOException {
final PathMatcher matcher = root.getFileSystem().getPathMatcher(syntaxPattern);
return Files.find(
root, Integer.MAX_VALUE, (path, attr)
-> matcher.matches(path) && !attr.isDirectory()
);
}
}