|
| 1 | +package io.reflectoring.fileswithstreams; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.nio.file.Paths; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.jar.JarEntry; |
| 11 | +import java.util.jar.JarFile; |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +public class FilesWithStreams { |
| 15 | + public static final String fileName = "src/main/resources/paths.properties"; |
| 16 | + static String folderPath = "src/main/resources/books"; |
| 17 | + static String filePath = "src/main/resources/books/bookIndex.txt"; |
| 18 | + static String jarFile = "src/main/resources/books/books.zip"; |
| 19 | + |
| 20 | + public static void main(String[] args) throws IOException { |
| 21 | + // processWithStream(); |
| 22 | + // readLineByLineUsingFiles(); |
| 23 | + readLineByLineUsingBufferedReader(); |
| 24 | + readFilterCountUsingFiles(); |
| 25 | + // printJarFileContents(); |
| 26 | + // printMatchingJarEntries(); |
| 27 | + // readWithParallelStreamAndPrint(); |
| 28 | + } |
| 29 | + |
| 30 | + static void processWithStream() { |
| 31 | + List<String> cities = Arrays.asList("London", "Sydney", "Colombo", "Cairo", "Beijing"); |
| 32 | + cities.stream().filter(a -> a.startsWith("C")).map(String::toUpperCase).sorted().forEach(System.out::println); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + static void readLineByLineUsingFiles() throws IOException { |
| 37 | + try (Stream<String> lines = Files.lines(Path.of(filePath))) { |
| 38 | + lines.forEach(System.out::println); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + static void readLineByLineUsingBufferedReader() throws IOException { |
| 43 | + try (Stream<String> lines = (Files.newBufferedReader(Paths.get(filePath)).lines())) { |
| 44 | + lines.forEach(System.out::println); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + static void readFilterCountUsingFiles() throws IOException { |
| 49 | + try (Stream<String> lines = Files.lines(Path.of(filePath))) { |
| 50 | + long i = lines.filter(line -> line.startsWith("A")).count(); |
| 51 | + System.out.println("Count of lines starting with 'A' is " + i); |
| 52 | + |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + static void printJarFileContents() throws IOException { |
| 57 | + try (JarFile jFile = new JarFile(jarFile)) { |
| 58 | + jFile.stream().forEach(file -> System.out.println(file)); |
| 59 | + |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + static void printMatchingJarEntries() throws IOException { |
| 64 | + try (JarFile jFile = new JarFile(jarFile)) { |
| 65 | + Optional<JarEntry> searchResult = jFile.stream().filter(file -> file.getName().contains("Matilda")) |
| 66 | + .findAny(); |
| 67 | + System.out.println(searchResult.get()); |
| 68 | + |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + static void readWithParallelStreamAndPrint() throws IOException { |
| 73 | + |
| 74 | + List<String> lines = Files.readAllLines(Path.of(filePath)); |
| 75 | + try (Stream<String> stream = lines.parallelStream()) { |
| 76 | + stream.forEach(System.out::println); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments