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 .Collectors ;
10
11
import java .util .stream .Stream ;
11
12
12
13
/**
@@ -26,25 +27,26 @@ public static void main(String[] args) throws IOException {
26
27
}
27
28
28
29
private static void testReaderLines () throws IOException {
29
- try (BufferedReader reader =
30
- Files .newBufferedReader (Paths .get ("res" , "nashorn1.js" ))) {
31
- long countPrints = reader .lines ()
30
+ Path path = Paths .get ("res/nashorn1.js" );
31
+ try (BufferedReader reader = Files .newBufferedReader (path )) {
32
+ long countPrints = reader
33
+ .lines ()
32
34
.filter (line -> line .contains ("print" ))
33
35
.count ();
34
36
System .out .println (countPrints );
35
37
}
36
38
}
37
39
38
40
private static void testWriter () throws IOException {
39
- try ( BufferedWriter writer =
40
- Files .newBufferedWriter (Paths . get ( "res" , "output.js" ) )) {
41
+ Path path = Paths . get ( "res/output.js" );
42
+ try ( BufferedWriter writer = Files .newBufferedWriter (path )) {
41
43
writer .write ("print('Hello World');" );
42
44
}
43
45
}
44
46
45
47
private static void testReader () throws IOException {
46
- try ( BufferedReader reader =
47
- Files .newBufferedReader (Paths . get ( "res" , "nashorn1.js" ) )) {
48
+ Path path = Paths . get ( "res/nashorn1.js" );
49
+ try ( BufferedReader reader = Files .newBufferedReader (path )) {
48
50
System .out .println (reader .readLine ());
49
51
}
50
52
}
@@ -53,10 +55,11 @@ private static void testWalk() throws IOException {
53
55
Path start = Paths .get ("" );
54
56
int maxDepth = 5 ;
55
57
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 );
58
+ String joined = stream
59
+ .map (String ::valueOf )
60
+ .filter (path -> path .endsWith (".js" ))
61
+ .collect (Collectors .joining ("; " ));
62
+ System .out .println ("walk(): " + joined );
60
63
}
61
64
}
62
65
@@ -65,26 +68,36 @@ private static void testFind() throws IOException {
65
68
int maxDepth = 5 ;
66
69
try (Stream <Path > stream = Files .find (start , maxDepth , (path , attr ) ->
67
70
String .valueOf (path ).endsWith (".js" ))) {
68
- stream .sorted ().forEach (System .out ::println );
71
+ String joined = stream
72
+ .sorted ()
73
+ .map (String ::valueOf )
74
+ .collect (Collectors .joining ("; " ));
75
+ System .out .println ("find(): " + joined );
69
76
}
70
77
}
71
78
72
79
private static void testList () throws IOException {
73
- try (Stream <Path > stream = Files .list (Paths .get ("/usr" ))) {
74
- stream .sorted ().forEach (System .out ::println );
80
+ try (Stream <Path > stream = Files .list (Paths .get ("" ))) {
81
+ String joined = stream
82
+ .map (String ::valueOf )
83
+ .filter (path -> !path .startsWith ("." ))
84
+ .sorted ()
85
+ .collect (Collectors .joining ("; " ));
86
+ System .out .println ("list(): " + joined );
75
87
}
76
88
}
77
89
78
90
private static void testLines () throws IOException {
79
- try (Stream <String > stream = Files .lines (Paths .get ("res" , " nashorn1.js" ))) {
91
+ try (Stream <String > stream = Files .lines (Paths .get ("res/ nashorn1.js" ))) {
80
92
stream
81
93
.filter (line -> line .contains ("print" ))
94
+ .map (String ::trim )
82
95
.forEach (System .out ::println );
83
96
}
84
97
}
85
98
86
99
private static void testReadWriteLines () throws IOException {
87
- List <String > lines = Files .readAllLines (Paths .get ("res" , " nashorn1.js" ));
100
+ List <String > lines = Files .readAllLines (Paths .get ("res/ nashorn1.js" ));
88
101
lines .add ("print('foobar');" );
89
102
Files .write (Paths .get ("res" , "nashorn1-modified.js" ), lines );
90
103
}
0 commit comments