Skip to content

Instantly share code, notes, and snippets.

Last active January 31, 2018 05:37
What would you like to do?
testing the gist for blog
import java.nio.file.Files;
import java.nio.file.Paths;
public class WordCount {
public static void main(String[] args) throws FileNotFoundException {
* this method count number of words from the text file and finally print the number of words * * @param fileName
public static void countWords(String fileName) {
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
long wordCount = stream.flatMap(str -> Stream.of(str.split("[ ,.!?\r\n]"))).count();
System.out.println("Number of words in file : " + wordCount);
} catch (FileNotFoundException fnf) {
System.out.println("Exception Occurred" + fnf.getMessage());
} catch (IOException io) {
System.out.println("Exception Occurred" + io.getMessage());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment