Skip to content

Instantly share code, notes, and snippets.

@frangarcia
Created February 13, 2020 08:34
Show Gist options
  • Save frangarcia/bd14e1b78fff120e7328751bb458e698 to your computer and use it in GitHub Desktop.
Save frangarcia/bd14e1b78fff120e7328751bb458e698 to your computer and use it in GitHub Desktop.
Counting words in properties file
List<String> files = ["file1.properties", "file2.properties", "file3.properties"]
Long countWords(File propertiesFile) {
Long totalWords = 0
try {
InputStream input = new FileInputStream(propertiesFile)
Properties prop = new Properties();
if (input == null) {
System.out.println("Sorry, unable to find " + propertiesFile);
return;
}
prop.load(input);
prop.each { key, value ->
totalWords += countWords(value)
}
} catch (IOException ex) {
ex.printStackTrace();
}
return totalWords
}
Long countWords(String text) {
return new StringTokenizer(text).countTokens()
}
Long totalWords = 0
files.each {
Long thisFileWords = countWords(new File(it))
println "${it}: ${thisFileWords}"
totalWords += thisFileWords
}
println "Total words: ${totalWords}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment