1
1
package io .reflectoring .fileswithstreams ;
2
2
3
3
import java .io .IOException ;
4
+ import java .nio .charset .StandardCharsets ;
4
5
import java .nio .file .Files ;
5
6
import java .nio .file .Path ;
6
7
import java .nio .file .Paths ;
7
8
import java .util .Arrays ;
8
9
import java .util .List ;
9
10
import java .util .Optional ;
11
+ import java .util .Set ;
10
12
import java .util .jar .JarEntry ;
11
13
import java .util .jar .JarFile ;
14
+ import java .util .regex .Pattern ;
15
+ import java .util .stream .Collectors ;
12
16
import java .util .stream .Stream ;
13
17
14
18
public class FilesWithStreams {
15
- public static final String fileName = "src/main/resources/paths.properties" ;
19
+
16
20
static String folderPath = "src/main/resources/books" ;
17
21
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" ;
19
25
20
26
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
+
23
41
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
+
28
98
}
29
99
30
100
static void processWithStream () {
@@ -44,15 +114,76 @@ static void readLineByLineUsingBufferedReader() throws IOException {
44
114
lines .forEach (System .out ::println );
45
115
}
46
116
}
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 ))) {
50
143
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
+ }
52
168
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 );
53
172
}
54
173
}
55
174
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
+
56
187
static void printJarFileContents () throws IOException {
57
188
try (JarFile jFile = new JarFile (jarFile )) {
58
189
jFile .stream ().forEach (file -> System .out .println (file ));
@@ -70,10 +201,8 @@ static void printMatchingJarEntries() throws IOException {
70
201
}
71
202
72
203
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 );
77
206
}
78
207
}
79
208
0 commit comments