77import java .nio .file .Path ;
88import java .nio .file .Paths ;
99import java .util .List ;
10+ import java .util .stream .Collectors ;
1011import java .util .stream .Stream ;
1112
1213/**
@@ -26,25 +27,26 @@ public static void main(String[] args) throws IOException {
2627 }
2728
2829 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 ()
3234 .filter (line -> line .contains ("print" ))
3335 .count ();
3436 System .out .println (countPrints );
3537 }
3638 }
3739
3840 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 )) {
4143 writer .write ("print('Hello World');" );
4244 }
4345 }
4446
4547 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 )) {
4850 System .out .println (reader .readLine ());
4951 }
5052 }
@@ -53,10 +55,11 @@ private static void testWalk() throws IOException {
5355 Path start = Paths .get ("" );
5456 int maxDepth = 5 ;
5557 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 );
6063 }
6164 }
6265
@@ -65,26 +68,36 @@ private static void testFind() throws IOException {
6568 int maxDepth = 5 ;
6669 try (Stream <Path > stream = Files .find (start , maxDepth , (path , attr ) ->
6770 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 );
6976 }
7077 }
7178
7279 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 );
7587 }
7688 }
7789
7890 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" ))) {
8092 stream
8193 .filter (line -> line .contains ("print" ))
94+ .map (String ::trim )
8295 .forEach (System .out ::println );
8396 }
8497 }
8598
8699 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" ));
88101 lines .add ("print('foobar');" );
89102 Files .write (Paths .get ("res" , "nashorn1-modified.js" ), lines );
90103 }
0 commit comments