package com.winterbe.java8.samples.misc;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * @author Benjamin Winterberg
 */
public class Files1 {

    public static void main(String[] args) throws IOException {
        testWalk();
        testFind();
        testList();
        testLines();
        testReader();
        testWriter();
        testReadWriteLines();
        testReaderLines();
    }

    private static void testReaderLines() throws IOException {
        try (BufferedReader reader =
                     Files.newBufferedReader(Paths.get("res", "nashorn1.js"))) {
            long countPrints = reader.lines()
                    .filter(line -> line.contains("print"))
                    .count();
            System.out.println(countPrints);
        }
    }

    private static void testWriter() throws IOException {
        try (BufferedWriter writer =
                     Files.newBufferedWriter(Paths.get("res", "output.js"))) {
            writer.write("print('Hello World');");
        }
    }

    private static void testReader() throws IOException {
        try (BufferedReader reader =
                     Files.newBufferedReader(Paths.get("res", "nashorn1.js"))) {
            System.out.println(reader.readLine());
        }
    }

    private static void testWalk() throws IOException {
        Path start = Paths.get("/Users/benny/Documents");
        int maxDepth = 5;
        long fileCount = Files
                .walk(start, maxDepth)
                .filter(path -> String.valueOf(path).endsWith("xls"))
                .count();
        System.out.format("XLS files found: %s", fileCount);
    }

    private static void testFind() throws IOException {
        Path start = Paths.get("/Users/benny/Documents");
        int maxDepth = 5;
        Files.find(start, maxDepth, (path, attr) ->
                String.valueOf(path).endsWith("xls"))
                .sorted()
                .forEach(System.out::println);
    }

    private static void testList() throws IOException {
        Files.list(Paths.get("/usr"))
                .sorted()
                .forEach(System.out::println);
    }

    private static void testLines() throws IOException {
        Files.lines(Paths.get("res", "nashorn1.js"))
                .filter(line -> line.contains("print"))
                .forEach(System.out::println);
    }

    private static void testReadWriteLines() throws IOException {
        List<String> lines = Files.readAllLines(Paths.get("res", "nashorn1.js"));
        lines.add("print('foobar');");
        Files.write(Paths.get("res", "nashorn1-modified.js"), lines);
    }
}

@@ -230,16 +230,48 @@
 il.List;
+%0Aimport java.util.stream.Stream;
 %0A%0A/**%0A *
@@ -1532,38 +1532,16 @@
 hs.get(%22
-/Users/benny/Documents
 %22);%0A    
@@ -1574,47 +1574,40 @@
     
-long fileCount = Files%0A                
+try (Stream%3CPath%3E stream = Files
 .wal
@@ -1624,17 +1624,60 @@
 axDepth)
+) %7B
 %0A
+            long fileCount = stream%0A    
         
@@ -1723,34 +1723,34 @@
 path).endsWith(%22
-xl
+.j
 s%22))%0A           
@@ -1746,32 +1746,36 @@
                 
+   
+ 
 .count();%0A      
@@ -1760,32 +1760,36 @@
       .count();%0A
+    
         System.o
@@ -1803,10 +1803,9 @@
 at(%22
-XL
+J
 S fi
@@ -1832,16 +1832,26 @@
 Count);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -1941,30 +1941,8 @@
 et(%22
-/Users/benny/Documents
 %22);%0A
@@ -1970,24 +1970,51 @@
 = 5;%0A       
+ try (Stream%3CPath%3E stream =
  Files.find(
@@ -2097,14 +2097,17 @@
 th(%22
-xl
+.j
 s%22))
+) %7B
 %0A   
@@ -2107,36 +2107,38 @@
 ) %7B%0A            
-    
+stream
 .sorted()%0A      
@@ -2130,33 +2130,16 @@
 sorted()
-%0A                
 .forEach
@@ -2153,32 +2153,42 @@
 .out::println);%0A
+        %7D%0A
     %7D%0A%0A    priva
@@ -2233,32 +2233,59 @@
 eption %7B%0A       
+ try (Stream%3CPath%3E stream =
  Files.list(Path
@@ -2298,16 +2298,19 @@
 %22/usr%22))
+) %7B
 %0A       
@@ -2314,20 +2314,22 @@
         
-    
+stream
 .sorted(
@@ -2329,33 +2329,16 @@
 sorted()
-%0A                
 .forEach
@@ -2352,32 +2352,42 @@
 .out::println);%0A
+        %7D%0A
     %7D%0A%0A    priva
@@ -2437,24 +2437,53 @@
 on %7B%0A       
+ try (Stream%3CString%3E stream =
  Files.lines
@@ -2511,30 +2511,47 @@
 shorn1.js%22))
+) %7B
 %0A     
+       stream%0A
            .
@@ -2541,32 +2541,41 @@
 ream%0A           
+         
 .filter(line -%3E 
@@ -2614,16 +2614,20 @@
         
+    
 .forEach
@@ -2645,24 +2645,34 @@
 ::println);%0A
+        %7D%0A
     %7D%0A%0A    p

Add proper try/with statements