Skip to content

Commit 13888f8

Browse files
committed
Full example code and resources
1 parent 00b00e6 commit 13888f8

File tree

6 files changed

+188
-27
lines changed

6 files changed

+188
-27
lines changed
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
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>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>fileswithstreams</groupId>
6+
<artifactId>fileswithstreams</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>fileswithstreams</name>
9+
<description>File management with streams</description>
10+
<dependencies>
11+
<dependency>
12+
<groupId>junit</groupId>
13+
<artifactId>junit</artifactId>
14+
<version>4.13</version>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
819
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.reflectoring.fileswithstreams;
2+
3+
public class Cake {
4+
private int id;
5+
private String name;
6+
private int price;
7+
8+
public Cake(int id, String name, int price) {
9+
this.id = id;
10+
this.name = name;
11+
this.price = price;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return "Cake [id=" + id + ", name=" + name + ", price=" + price + "]";
17+
}
18+
}

core-java/streams/fileswithstreams/src/main/java/io/reflectoring/fileswithstreams/FilesWithStreams.java

Lines changed: 145 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,100 @@
11
package io.reflectoring.fileswithstreams;
22

33
import java.io.IOException;
4+
import java.nio.charset.StandardCharsets;
45
import java.nio.file.Files;
56
import java.nio.file.Path;
67
import java.nio.file.Paths;
78
import java.util.Arrays;
89
import java.util.List;
910
import java.util.Optional;
11+
import java.util.Set;
1012
import java.util.jar.JarEntry;
1113
import java.util.jar.JarFile;
14+
import java.util.regex.Pattern;
15+
import java.util.stream.Collectors;
1216
import java.util.stream.Stream;
1317

1418
public class FilesWithStreams {
15-
public static final String fileName = "src/main/resources/paths.properties";
19+
1620
static String folderPath = "src/main/resources/books";
1721
static String filePath = "src/main/resources/books/bookIndex.txt";
18-
static String jarFile = "src/main/resources/books/books.zip";
22+
static String csvPath = "src/main/resources/cakes.csv";
23+
static String utfFilePath = "src/main/resources/input.txt";
24+
static String jarFile = "src/main/resources/books.zip";
1925

2026
public static void main(String[] args) throws IOException {
21-
// processWithStream();
22-
// readLineByLineUsingFiles();
27+
System.out.println("-------------------Files with Streams------------------ ");
28+
29+
System.out.println("-------------------Introductory Example------------------ ");
30+
31+
processWithStream();
32+
33+
System.out.println(
34+
"\r\n" + "-------------------Example 1 - Reading line by line from a file------------------ " + "\r\n");
35+
36+
readLineByLineUsingFiles();
37+
38+
System.out.println(
39+
"\r\n" + "-------------------Example 2 - Reading with Buffered Reader------------------" + "\r\n");
40+
2341
readLineByLineUsingBufferedReader();
24-
readFilterCountUsingFiles();
25-
// printJarFileContents();
26-
// printMatchingJarEntries();
27-
// readWithParallelStreamAndPrint();
42+
43+
System.out.println(
44+
"\r\n" + "-------------------Example 3 - Reading all lines from a file------------------ " + "\r\n");
45+
46+
readAllLinesUsingFiles();
47+
48+
System.out.println(
49+
"\r\n" + "...................Example 4 - Reading with parallel streams------------------ " + "\r\n");
50+
51+
readWithParallelStreamAndPrint();
52+
53+
System.out.println(
54+
"\r\n" + "-------------------Example 5 - Reading UTF-encoded file------------------ " + "\r\n");
55+
56+
readUtfEncodedFile();
57+
58+
System.out.println(
59+
"\r\n" + "-------------------Example 6 - Reading, Filtering and Counting------------------ " + "\r\n");
60+
61+
readFilterCountFromFile();
62+
63+
System.out.println("\r\n" + "-------------------Example 7 - Splitting Words------------------ " + "\r\n");
64+
65+
splitWordsFromFile();
66+
67+
System.out.println("\r\n" + "-------------------Example 8 - Loading from CSV - ------------------ " + "\r\n");
68+
69+
loadItemsFromCsvFile();
70+
71+
System.out.println("\r\n" + "...................Example 9 - Listing Directories------------------ " + "\r\n");
72+
73+
listDirectories();
74+
75+
System.out
76+
.println("\r\n" + "...................Example 10 - Listing Regular Files------------------ " + "\r\n");
77+
78+
listRegularFiles();
79+
80+
System.out.println(
81+
"\r\n" + "...................Example 11 - Walking Files Recursively------------------ " + "\r\n");
82+
83+
walkFilesRecursively();
84+
85+
System.out.println("\r\n" + "...................Example 12 - Finding Files------------------ " + "\r\n");
86+
87+
findFiles();
88+
89+
System.out.println(
90+
"\r\n" + "...................Example 13 - Printing JAR fie contents------------------ " + "\r\n");
91+
92+
printJarFileContents();
93+
94+
System.out.println("...................Example 14 - Printing Matching JAR entries------------------ " + "\r\n");
95+
96+
printMatchingJarEntries();
97+
2898
}
2999

30100
static void processWithStream() {
@@ -44,15 +114,76 @@ static void readLineByLineUsingBufferedReader() throws IOException {
44114
lines.forEach(System.out::println);
45115
}
46116
}
47-
48-
static void readFilterCountUsingFiles() throws IOException {
49-
try (Stream<String> lines = Files.lines(Path.of(filePath))) {
117+
118+
static void readAllLinesUsingFiles() throws IOException {
119+
List<String> strList = Files.readAllLines(Path.of(filePath));
120+
Stream<String> lines = strList.stream();
121+
lines.forEach(System.out::println);
122+
}
123+
124+
static void readUtfEncodedFile() throws IOException {
125+
try (Stream<String> lines = Files.lines(Path.of(utfFilePath), StandardCharsets.UTF_8)) {
126+
lines.forEach(System.out::println);
127+
}
128+
}
129+
130+
static void loadItemsFromCsvFile() throws IOException {
131+
Pattern pattern = Pattern.compile(",");
132+
try (Stream<String> lines = Files.lines(Path.of(csvPath))) {
133+
List<Cake> cakes = lines.skip(1).map(line -> {
134+
String[] arr = pattern.split(line);
135+
return new Cake(Integer.parseInt(arr[0]), arr[1], Integer.parseInt(arr[2]));
136+
}).collect(Collectors.toList());
137+
cakes.forEach(System.out::println);
138+
}
139+
}
140+
141+
static void readFilterCountFromFile() throws IOException {
142+
try (Stream<String> lines = Files.lines(Path.of(filePath))) {
50143
long i = lines.filter(line -> line.startsWith("A")).count();
51-
System.out.println("Count of lines starting with 'A' is " + i);
144+
System.out.println("The count of lines starting with 'A' is " + i);
145+
146+
}
147+
}
148+
149+
static void splitWordsFromFile() throws IOException {
150+
try (Stream<String> lines = Files.lines(Path.of(filePath))) {
151+
Stream<String> words = lines.flatMap(line -> Stream.of(line.split("\\W+")));
152+
Set<String> wordSet = words.collect(Collectors.toSet());
153+
System.out.println(wordSet);
154+
}
155+
}
156+
157+
static void listDirectories() throws IOException {
158+
try (Stream<Path> paths = Files.list(Path.of(folderPath))) {
159+
paths.filter(Files::isDirectory).forEach(System.out::println);
160+
}
161+
}
162+
163+
static void listRegularFiles() throws IOException {
164+
try (Stream<Path> paths = Files.list(Path.of(folderPath))) {
165+
paths.filter(Files::isRegularFile).forEach(System.out::println);
166+
}
167+
}
52168

169+
static void walkFilesRecursively() throws IOException {
170+
try (Stream<Path> stream = Files.walk(Path.of(folderPath))) {
171+
stream.filter(Files::isRegularFile).forEach(System.out::println);
53172
}
54173
}
55174

175+
static void findFiles() throws IOException {
176+
int depth = Integer.MAX_VALUE;
177+
try (Stream<Path> paths = Files.find(Path.of(folderPath), depth, (path, attr) -> {
178+
return attr.isRegularFile() && path.toString().endsWith(".pdf");
179+
})) {
180+
181+
paths.forEach(System.out::println);
182+
183+
}
184+
185+
}
186+
56187
static void printJarFileContents() throws IOException {
57188
try (JarFile jFile = new JarFile(jarFile)) {
58189
jFile.stream().forEach(file -> System.out.println(file));
@@ -70,10 +201,8 @@ static void printMatchingJarEntries() throws IOException {
70201
}
71202

72203
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);
204+
try (Stream<String> lines = (Files.lines(Path.of(filePath)).parallel())) {
205+
lines.forEach(System.out::println);
77206
}
78207
}
79208

39.3 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Cakes
2+
1, Pound Cake,100
3+
2, Red Velvet Cake,500
4+
3, Carrot Cake,300
5+
4, Sponge Cake,400
6+
5, Chiffon Cake,600
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
Master, you are the sole goal of human life
2-
We are all but slaves of wishes
3-
Putting bar to our advancement
4-
You are the one and only true lord who can bring us upto that level
1+
akarui _ あかるい _ bright

0 commit comments

Comments
 (0)