|
7 | 7 | import java.nio.file.Path;
|
8 | 8 | import java.nio.file.Paths;
|
9 | 9 | import java.util.List;
|
| 10 | +import java.util.stream.Stream; |
10 | 11 |
|
11 | 12 | /**
|
12 | 13 | * @author Benjamin Winterberg
|
@@ -49,34 +50,37 @@ private static void testReader() throws IOException {
|
49 | 50 | }
|
50 | 51 |
|
51 | 52 | private static void testWalk() throws IOException {
|
52 |
| - Path start = Paths.get("/Users/benny/Documents"); |
| 53 | + Path start = Paths.get(""); |
53 | 54 | int maxDepth = 5;
|
54 |
| - long fileCount = Files |
55 |
| - .walk(start, maxDepth) |
56 |
| - .filter(path -> String.valueOf(path).endsWith("xls")) |
57 |
| - .count(); |
58 |
| - System.out.format("XLS files found: %s", fileCount); |
| 55 | + try (Stream<Path> stream = Files.walk(start, maxDepth)) { |
| 56 | + long fileCount = stream |
| 57 | + .filter(path -> String.valueOf(path).endsWith(".js")) |
| 58 | + .count(); |
| 59 | + System.out.format("JS files found: %s", fileCount); |
| 60 | + } |
59 | 61 | }
|
60 | 62 |
|
61 | 63 | private static void testFind() throws IOException {
|
62 |
| - Path start = Paths.get("/Users/benny/Documents"); |
| 64 | + Path start = Paths.get(""); |
63 | 65 | int maxDepth = 5;
|
64 |
| - Files.find(start, maxDepth, (path, attr) -> |
65 |
| - String.valueOf(path).endsWith("xls")) |
66 |
| - .sorted() |
67 |
| - .forEach(System.out::println); |
| 66 | + try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr) -> |
| 67 | + String.valueOf(path).endsWith(".js"))) { |
| 68 | + stream.sorted().forEach(System.out::println); |
| 69 | + } |
68 | 70 | }
|
69 | 71 |
|
70 | 72 | private static void testList() throws IOException {
|
71 |
| - Files.list(Paths.get("/usr")) |
72 |
| - .sorted() |
73 |
| - .forEach(System.out::println); |
| 73 | + try (Stream<Path> stream = Files.list(Paths.get("/usr"))) { |
| 74 | + stream.sorted().forEach(System.out::println); |
| 75 | + } |
74 | 76 | }
|
75 | 77 |
|
76 | 78 | private static void testLines() throws IOException {
|
77 |
| - Files.lines(Paths.get("res", "nashorn1.js")) |
78 |
| - .filter(line -> line.contains("print")) |
79 |
| - .forEach(System.out::println); |
| 79 | + try (Stream<String> stream = Files.lines(Paths.get("res", "nashorn1.js"))) { |
| 80 | + stream |
| 81 | + .filter(line -> line.contains("print")) |
| 82 | + .forEach(System.out::println); |
| 83 | + } |
80 | 84 | }
|
81 | 85 |
|
82 | 86 | private static void testReadWriteLines() throws IOException {
|
|
0 commit comments