Skip to content

Commit 00b00e6

Browse files
committed
Initial commit of processing files with java streams code samples
1 parent 1eabfeb commit 00b00e6

15 files changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="test" value="true"/>
17+
<attribute name="optional" value="true"/>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
22+
<attributes>
23+
<attribute name="test" value="true"/>
24+
<attribute name="maven.pomderived" value="true"/>
25+
</attributes>
26+
</classpathentry>
27+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
28+
<attributes>
29+
<attribute name="module" value="true"/>
30+
<attribute name="maven.pomderived" value="true"/>
31+
</attributes>
32+
</classpathentry>
33+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
34+
<attributes>
35+
<attribute name="maven.pomderived" value="true"/>
36+
</attributes>
37+
</classpathentry>
38+
<classpathentry kind="output" path="target/classes"/>
39+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>fileswithstreams</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=11
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
12+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
13+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
14+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
15+
org.eclipse.jdt.core.compiler.release=disabled
16+
org.eclipse.jdt.core.compiler.source=11
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>fileswithstreams</groupId>
4+
<artifactId>fileswithstreams</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>fileswithstreams</name>
7+
<description>File management with streams</description>
8+
</project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Pride and Prejudice- pride-and-prejudice.pdf
2+
Anne of Avonlea - anne-of-avonlea.pdf
3+
Anne of Green Gables - anne-of-green-gables.pdf
4+
Matilda - Matilda.pdf
5+
Why Icebergs Float - Why-Icebergs-Float.pdf
Binary file not shown.

0 commit comments

Comments
 (0)