diff --git a/core-java/streams/fileswithstreams/.classpath b/core-java/streams/fileswithstreams/.classpath new file mode 100644 index 0000000..36b1772 --- /dev/null +++ b/core-java/streams/fileswithstreams/.classpath @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core-java/streams/fileswithstreams/.gitignore b/core-java/streams/fileswithstreams/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/core-java/streams/fileswithstreams/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/core-java/streams/fileswithstreams/.project b/core-java/streams/fileswithstreams/.project new file mode 100644 index 0000000..2de4f97 --- /dev/null +++ b/core-java/streams/fileswithstreams/.project @@ -0,0 +1,23 @@ + + + fileswithstreams + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/core-java/streams/fileswithstreams/.settings/org.eclipse.jdt.core.prefs b/core-java/streams/fileswithstreams/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..2bd4e77 --- /dev/null +++ b/core-java/streams/fileswithstreams/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,16 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=11 diff --git a/core-java/streams/fileswithstreams/.settings/org.eclipse.m2e.core.prefs b/core-java/streams/fileswithstreams/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/core-java/streams/fileswithstreams/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/core-java/streams/fileswithstreams/pom.xml b/core-java/streams/fileswithstreams/pom.xml new file mode 100644 index 0000000..47bd68f --- /dev/null +++ b/core-java/streams/fileswithstreams/pom.xml @@ -0,0 +1,8 @@ + + 4.0.0 + fileswithstreams + fileswithstreams + 0.0.1-SNAPSHOT + fileswithstreams + File management with streams + \ No newline at end of file diff --git a/core-java/streams/fileswithstreams/src/main/java/io/reflectoring/fileswithstreams/FilesWithStreams.java b/core-java/streams/fileswithstreams/src/main/java/io/reflectoring/fileswithstreams/FilesWithStreams.java new file mode 100644 index 0000000..1d1b1f4 --- /dev/null +++ b/core-java/streams/fileswithstreams/src/main/java/io/reflectoring/fileswithstreams/FilesWithStreams.java @@ -0,0 +1,80 @@ +package io.reflectoring.fileswithstreams; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.stream.Stream; + +public class FilesWithStreams { + public static final String fileName = "src/main/resources/paths.properties"; + static String folderPath = "src/main/resources/books"; + static String filePath = "src/main/resources/books/bookIndex.txt"; + static String jarFile = "src/main/resources/books/books.zip"; + + public static void main(String[] args) throws IOException { + // processWithStream(); + // readLineByLineUsingFiles(); + readLineByLineUsingBufferedReader(); + readFilterCountUsingFiles(); + // printJarFileContents(); + // printMatchingJarEntries(); + // readWithParallelStreamAndPrint(); + } + + static void processWithStream() { + List cities = Arrays.asList("London", "Sydney", "Colombo", "Cairo", "Beijing"); + cities.stream().filter(a -> a.startsWith("C")).map(String::toUpperCase).sorted().forEach(System.out::println); + + } + + static void readLineByLineUsingFiles() throws IOException { + try (Stream lines = Files.lines(Path.of(filePath))) { + lines.forEach(System.out::println); + } + } + + static void readLineByLineUsingBufferedReader() throws IOException { + try (Stream lines = (Files.newBufferedReader(Paths.get(filePath)).lines())) { + lines.forEach(System.out::println); + } + } + + static void readFilterCountUsingFiles() throws IOException { + try (Stream lines = Files.lines(Path.of(filePath))) { + long i = lines.filter(line -> line.startsWith("A")).count(); + System.out.println("Count of lines starting with 'A' is " + i); + + } + } + + static void printJarFileContents() throws IOException { + try (JarFile jFile = new JarFile(jarFile)) { + jFile.stream().forEach(file -> System.out.println(file)); + + } + } + + static void printMatchingJarEntries() throws IOException { + try (JarFile jFile = new JarFile(jarFile)) { + Optional searchResult = jFile.stream().filter(file -> file.getName().contains("Matilda")) + .findAny(); + System.out.println(searchResult.get()); + + } + } + + static void readWithParallelStreamAndPrint() throws IOException { + + List lines = Files.readAllLines(Path.of(filePath)); + try (Stream stream = lines.parallelStream()) { + stream.forEach(System.out::println); + } + } + +} diff --git a/core-java/streams/fileswithstreams/src/main/resources/books/bookIndex.txt b/core-java/streams/fileswithstreams/src/main/resources/books/bookIndex.txt new file mode 100644 index 0000000..8dd1f28 --- /dev/null +++ b/core-java/streams/fileswithstreams/src/main/resources/books/bookIndex.txt @@ -0,0 +1,5 @@ +Pride and Prejudice- pride-and-prejudice.pdf +Anne of Avonlea - anne-of-avonlea.pdf +Anne of Green Gables - anne-of-green-gables.pdf +Matilda - Matilda.pdf +Why Icebergs Float - Why-Icebergs-Float.pdf diff --git a/core-java/streams/fileswithstreams/src/main/resources/books/fiction/adults/pride-and-prejudice.pdf b/core-java/streams/fileswithstreams/src/main/resources/books/fiction/adults/pride-and-prejudice.pdf new file mode 100644 index 0000000..533227e Binary files /dev/null and b/core-java/streams/fileswithstreams/src/main/resources/books/fiction/adults/pride-and-prejudice.pdf differ diff --git a/core-java/streams/fileswithstreams/src/main/resources/books/fiction/kids/Matilda.pdf b/core-java/streams/fileswithstreams/src/main/resources/books/fiction/kids/Matilda.pdf new file mode 100644 index 0000000..7998221 Binary files /dev/null and b/core-java/streams/fileswithstreams/src/main/resources/books/fiction/kids/Matilda.pdf differ diff --git a/core-java/streams/fileswithstreams/src/main/resources/books/fiction/kids/anne-of-avonlea.pdf b/core-java/streams/fileswithstreams/src/main/resources/books/fiction/kids/anne-of-avonlea.pdf new file mode 100644 index 0000000..1c330fd Binary files /dev/null and b/core-java/streams/fileswithstreams/src/main/resources/books/fiction/kids/anne-of-avonlea.pdf differ diff --git a/core-java/streams/fileswithstreams/src/main/resources/books/fiction/kids/anne-of-green-gables.pdf b/core-java/streams/fileswithstreams/src/main/resources/books/fiction/kids/anne-of-green-gables.pdf new file mode 100644 index 0000000..8a4dd3b Binary files /dev/null and b/core-java/streams/fileswithstreams/src/main/resources/books/fiction/kids/anne-of-green-gables.pdf differ diff --git a/core-java/streams/fileswithstreams/src/main/resources/books/non-fiction/Why-Icebergs-Float.pdf b/core-java/streams/fileswithstreams/src/main/resources/books/non-fiction/Why-Icebergs-Float.pdf new file mode 100644 index 0000000..66157d4 Binary files /dev/null and b/core-java/streams/fileswithstreams/src/main/resources/books/non-fiction/Why-Icebergs-Float.pdf differ diff --git a/core-java/streams/fileswithstreams/src/main/resources/input.txt b/core-java/streams/fileswithstreams/src/main/resources/input.txt new file mode 100644 index 0000000..7fae436 --- /dev/null +++ b/core-java/streams/fileswithstreams/src/main/resources/input.txt @@ -0,0 +1,4 @@ +Master, you are the sole goal of human life +We are all but slaves of wishes +Putting bar to our advancement +You are the one and only true lord who can bring us upto that level \ No newline at end of file diff --git a/core-java/streams/fileswithstreams/src/main/resources/paths.properties b/core-java/streams/fileswithstreams/src/main/resources/paths.properties new file mode 100644 index 0000000..fb67a82 --- /dev/null +++ b/core-java/streams/fileswithstreams/src/main/resources/paths.properties @@ -0,0 +1 @@ +/home/u1026/workspace/Learn/junit-5-basics/src/main/resources/books/ \ No newline at end of file