Skip to content

Commit

Permalink
Fix Sonar issue
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav-nath committed Jul 26, 2022
1 parent dbb5e08 commit 3e23bff
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/com/codecafe/java8/streams/usecases/ReadFromFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

// Read a file and count the occurrence of a word in it
public class ReadFromFile {

public static void main(String[] args) throws IOException {
String path = "C:\\Users\\Abhinav\\git\\java-8\\src\\com\\codecafe\\java8\\streams\\usecases\\hosts";
String path = "src/com/codecafe/java8/streams/usecases/hosts";
String searchWord = "localhost";
System.out.printf("occurrence of word [%s] = %d", searchWord, Files.lines(Paths.get(path))
.filter(line -> line.contains(searchWord))
.count());
long count = 0;

try (Stream<String> lines = Files.lines(Paths.get(path))) {
count = lines.filter(line -> line.contains(searchWord))
.count();
}

System.out.printf("occurrence of word [%s] = %d", searchWord, count);
}

}
}

0 comments on commit 3e23bff

Please sign in to comment.