| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,76 @@ | |||
| 1 | + package com.winterbe.java8.samples.misc; | ||
| 2 | + | ||
| 3 | + import java.io.BufferedReader; | ||
| 4 | + import java.io.BufferedWriter; | ||
| 5 | + import java.io.IOException; | ||
| 6 | + import java.nio.file.Files; | ||
| 7 | + import java.nio.file.Path; | ||
| 8 | + import java.nio.file.Paths; | ||
| 9 | + import java.util.List; | ||
| 10 | + | ||
| 11 | + /** | ||
| 12 | + * @author Benjamin Winterberg | ||
| 13 | + */ | ||
| 14 | + public class Files1 { | ||
| 15 | + | ||
| 16 | + public static void main(String[] args) throws IOException { | ||
| 17 | + testWalk(); | ||
| 18 | + testFind(); | ||
| 19 | + testList(); | ||
| 20 | + testLines(); | ||
| 21 | + testReader(); | ||
| 22 | + testWriter(); | ||
| 23 | + testReadWriteLines(); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + private static void testWriter() throws IOException { | ||
| 27 | + try (BufferedWriter writer = | ||
| 28 | + Files.newBufferedWriter(Paths.get("res", "output.js"))) { | ||
| 29 | + writer.write("print('Hello World');"); | ||
| 30 | + } | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + private static void testReader() throws IOException { | ||
| 34 | + try (BufferedReader reader = | ||
| 35 | + Files.newBufferedReader(Paths.get("res", "nashorn1.js"))) { | ||
| 36 | + System.out.println(reader.readLine()); | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + private static void testWalk() throws IOException { | ||
| 41 | + Path start = Paths.get("/Users/benny/Documents"); | ||
| 42 | + int maxDepth = 5; | ||
| 43 | + long fileCount = Files | ||
| 44 | + .walk(start, maxDepth) | ||
| 45 | + .filter(path -> String.valueOf(path).endsWith("xls")) | ||
| 46 | + .count(); | ||
| 47 | + System.out.format("XLS files found: %s", fileCount); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + private static void testFind() throws IOException { | ||
| 51 | + Path start = Paths.get("/Users/benny/Documents"); | ||
| 52 | + int maxDepth = 5; | ||
| 53 | + Files.find(start, maxDepth, (path, attr) -> | ||
| 54 | + String.valueOf(path).endsWith("xls")) | ||
| 55 | + .sorted() | ||
| 56 | + .forEach(System.out::println); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + private static void testList() throws IOException { | ||
| 60 | + Files.list(Paths.get("/usr")) | ||
| 61 | + .sorted() | ||
| 62 | + .forEach(System.out::println); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + private static void testLines() throws IOException { | ||
| 66 | + Files.lines(Paths.get("res", "nashorn1.js")) | ||
| 67 | + .filter(line -> line.contains("print")) | ||
| 68 | + .forEach(System.out::println); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + private static void testReadWriteLines() throws IOException { | ||
| 72 | + List<String> lines = Files.readAllLines(Paths.get("res", "nashorn1.js")); | ||
| 73 | + lines.add("print('foobar');"); | ||
| 74 | + Files.write(Paths.get("res", "nashorn1-modified.js"), lines); | ||
| 75 | + } | ||
| 76 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,9 +2,6 @@ | |||
| 2 | 2 | ||
| 3 | 3 | import java.io.IOException; | |
| 4 | 4 | import java.math.BigDecimal; | |
| 5 | - import java.nio.file.Files; | ||
| 6 | - import java.nio.file.Path; | ||
| 7 | - import java.nio.file.Paths; | ||
| 8 | 5 | import java.util.Arrays; | |
| 9 | 6 | import java.util.stream.IntStream; | |
| 10 | 7 | import java.util.stream.Stream; | |
@@ -15,49 +12,10 @@ | |||
| 15 | 12 | public class Streams6 { | |
| 16 | 13 | ||
| 17 | 14 | public static void main(String[] args) throws IOException { | |
| 18 | - // test1(); | ||
| 19 | - // test2(); | ||
| 20 | - // test3(); | ||
| 21 | - // test4(); | ||
| 22 | - // test5(); | ||
| 23 | - // test6(); | ||
| 24 | - // test7(); | ||
| 25 | - test8(); | ||
| 26 | - } | ||
| 27 | - | ||
| 28 | - private static void test8() throws IOException { | ||
| 29 | - Path start = Paths.get("/Users/benny/Documents"); | ||
| 30 | - int maxDepth = 5; | ||
| 31 | - long fileCount = | ||
| 32 | - Files | ||
| 33 | - .walk(start, maxDepth) | ||
| 34 | - .filter(path -> String.valueOf(path).endsWith("xls")) | ||
| 35 | - .count(); | ||
| 36 | - System.out.format("XLS files found: %s", fileCount); | ||
| 37 | - } | ||
| 38 | - | ||
| 39 | - private static void test7() throws IOException { | ||
| 40 | - Path start = Paths.get("/Users/benny/Documents"); | ||
| 41 | - int maxDepth = 5; | ||
| 42 | - Files | ||
| 43 | - .find(start, maxDepth, (path, attr) -> | ||
| 44 | - String.valueOf(path).endsWith("xls")) | ||
| 45 | - .sorted() | ||
| 46 | - .forEach(System.out::println); | ||
| 47 | - } | ||
| 48 | - | ||
| 49 | - private static void test6() throws IOException { | ||
| 50 | - Files | ||
| 51 | - .list(Paths.get("/usr")) | ||
| 52 | - .sorted() | ||
| 53 | - .forEach(System.out::println); | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - private static void test5() throws IOException { | ||
| 57 | - Files | ||
| 58 | - .lines(Paths.get("/foo", "bar")) | ||
| 59 | - .filter(line -> line.startsWith("A")) | ||
| 60 | - .forEach(System.out::println); | ||
| 15 | + test1(); | ||
| 16 | + test2(); | ||
| 17 | + test3(); | ||
| 18 | + test4(); | ||
| 61 | 19 | } | |
| 62 | 20 | ||
| 63 | 21 | private static void test4() { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments