Created
February 13, 2020 08:34
-
-
Save frangarcia/bd14e1b78fff120e7328751bb458e698 to your computer and use it in GitHub Desktop.
Counting words in properties file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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