11package io .reflectoring .fileswithstreams ;
22
33import java .io .IOException ;
4+ import java .nio .charset .StandardCharsets ;
45import java .nio .file .Files ;
56import java .nio .file .Path ;
67import java .nio .file .Paths ;
78import java .util .Arrays ;
89import java .util .List ;
910import java .util .Optional ;
11+ import java .util .Set ;
1012import java .util .jar .JarEntry ;
1113import java .util .jar .JarFile ;
14+ import java .util .regex .Pattern ;
15+ import java .util .stream .Collectors ;
1216import java .util .stream .Stream ;
1317
1418public 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
0 commit comments