Initial commit of processing files with java streams code samples

This commit is contained in:
susetech
2020-09-12 12:16:11 +05:30
parent 1eabfebeb0
commit 00b00e6e77
15 changed files with 181 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -0,0 +1 @@
/target/

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>fileswithstreams</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@@ -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

View File

@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

View File

@@ -0,0 +1,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fileswithstreams</groupId>
<artifactId>fileswithstreams</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fileswithstreams</name>
<description>File management with streams</description>
</project>

View File

@@ -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<String> 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<String> lines = Files.lines(Path.of(filePath))) {
lines.forEach(System.out::println);
}
}
static void readLineByLineUsingBufferedReader() throws IOException {
try (Stream<String> lines = (Files.newBufferedReader(Paths.get(filePath)).lines())) {
lines.forEach(System.out::println);
}
}
static void readFilterCountUsingFiles() throws IOException {
try (Stream<String> 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<JarEntry> searchResult = jFile.stream().filter(file -> file.getName().contains("Matilda"))
.findAny();
System.out.println(searchResult.get());
}
}
static void readWithParallelStreamAndPrint() throws IOException {
List<String> lines = Files.readAllLines(Path.of(filePath));
try (Stream<String> stream = lines.parallelStream()) {
stream.forEach(System.out::println);
}
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1 @@
/home/u1026/workspace/Learn/junit-5-basics/src/main/resources/books/