-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathNewStreamMethods.java
87 lines (80 loc) · 2.77 KB
/
NewStreamMethods.java
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 nio2;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
*
* @author chengfeili
* Jun 22, 2017 2:24:36 PM
*
*/
public class NewStreamMethods {
/**
* The Files.walk(path) method returns a Stream<Path> object that traverses
* the directory in a depth-first, lazy manner. By lazy, we mean the set of
* elements is built and read while the directory is being traversed.
*
* Avoiding Circular Paths. the walk() method will not traverse symbolic
* links by default.
*/
public void walkingADirectory() {
Path path = Paths.get("/bigcats");
try {
Files.walk(path).filter(p -> p.toString().endsWith(".java")).forEach(System.out::println);
} catch (IOException e) {
// Handle file I/O exception...
}
}
public void searchingADirectory() {
Path path = Paths.get("/bigcats");
long dateFilter = 1420070400000l;
try {
Stream<Path> stream = Files.find(path, 10,
(p, a) -> p.toString().endsWith(".java") && a.lastModifiedTime().toMillis() > dateFilter);
stream.forEach(System.out::println);
} catch (Exception e) {
// Handle file I/O exception...
}
}
/**
* return a list of File objects representing the contents of the directory
* that are direct children of the parent.
*/
public void listingDirectoryContents() {
try {
Path path = Paths.get("ducks");
Files.list(path).filter(p -> !Files.isDirectory(p)).map(p -> p.toAbsolutePath())
.forEach(System.out::println);
} catch (IOException e) {
// Handle file I/O exception...
}
}
public void printingFileContents() throws IOException {
Path path = Paths.get("/fish/sharks.log");
try {
System.out.println(Files.lines(path).filter(s -> s.startsWith("WARN ")).map(s -> s.substring(5))
.collect(Collectors.toList()));
} catch (IOException e) {
// Handle file I/O exception...
}
Files.readAllLines(Paths.get("birds.txt")).forEach(System.out::println);
Files.lines(Paths.get("birds.txt")).forEach(System.out::println);
/**
* The first code snippet reads the entire file into memory and then
* performs a print operation on the resulting object. The second code
* snippet reads the lines lazily and prints them as they are being
* read. The advantage of the second code snippet is that it does not
* require the entire file to be stored in memory as it is being read.
*/
// Files.readAllLines(path).filter(s -> s.length() > 2).forEach(System.out::println);
Files.lines(path).filter(s -> s.length() > 2).forEach(System.out::println);
/**
* The first line does not compile because the filter() operation cannot
* be applied to a Collection without first converting it to a Stream
* using the stream() method.
*/
}
}