-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathNio.java
More file actions
53 lines (44 loc) · 1.71 KB
/
Nio.java
File metadata and controls
53 lines (44 loc) · 1.71 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
import static java.util.stream.Collectors.*;
import java.util.stream.*;
import java.nio.file.*;
import java.nio.*;
import java.io.*;
import java.util.IntSummaryStatistics;
public class Nio {
public static void main(String...args) throws IOException {
System.out.println("\n----->first 5 java file names:");
Files.list(Paths.get("."))
.map(Path::getFileName) // still a path
.map(Path::toString) // convert to Strings
.filter(name -> name.endsWith(".java"))
.sorted() // sort them alphabetically
.limit(5) // first 5
.forEach(System.out::println);
/*
System.out.println("\n----->Print the code:");
Files.lines(Paths.get("Nio.java"))
.map(String::trim)
.filter(s -> !s.isEmpty())
.forEach(System.out::println);
/*-/
System.out.println("\n----->Average line length:");
System.out.println(
Files.lines(Paths.get("Nio.java"))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(averagingInt(String::length))
);
IntSummaryStatistics stats = Files.lines(Paths.get("Nio.java"))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(summarizingInt(String::length));
System.out.println(stats.getAverage());
System.out.println("count=" + stats.getCount());
System.out.println("max=" + stats.getMax());
System.out.println("min=" + stats.getMin());
*/
/*String str = Stream.of("hello", "java", "8")
.collect(joining("-"));
System.out.println(str);*/
}
}