Skip to content

Commit 32c8deb

Browse files
committed
using Files.newBufferedReader
1 parent ebcb95f commit 32c8deb

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/05_converting_to_streams.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,22 @@ Suppose we want to iterate over a file and count the number of lines with one or
3030

3131
```java
3232
//Sample.java
33-
import java.util.*;
34-
import java.io.*;
33+
import java.nio.file.*;
3534

3635
public class Sample {
3736
public static void main(String[] args) {
3837
try {
39-
final var filePath = "./Sample.java";
38+
final var filePath = "./Sample.java";
4039
final var wordOfInterest = "public";
4140

42-
try (var reader = new BufferedReader(new FileReader(filePath))) {
41+
try (var reader = Files.newBufferedReader(Paths.get(filePath))) {
4342
String line = "";
4443
long count = 0;
4544

4645
while((line = reader.readLine()) != null) {
4746
if(line.contains(wordOfInterest)) {
48-
count++;
49-
}
47+
count++;
48+
}
5049
}
5150

5251
System.out.println(String.format("Found %d lines with the word %s", count, wordOfInterest));

0 commit comments

Comments
 (0)