How To Count Words Inside A Txt File Using Java NetBeans
In this Java Tutorial we will see How To Get The Number Of Words In A Text File Using For Loop + BufferedReader In Java NetBeans .
Project Source Code:
private void jButtonWordCountActionPerformed(java.awt.event.ActionEvent evt) {
String filePath = "C:\\Users\\omar\\Desktop\\file2.txt";
File file = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
Object[] lines = br.lines().toArray();
int wordCount = 0;
for(int i = 0; i < lines.length; i++){
String[] words = lines[i].toString().split(Pattern.quote(" "));
// get word count line by line
wordCount += words.length;
}
System.out.println(wordCount);
} catch (FileNotFoundException ex) {
Logger.getLogger(java_text_words_count.class.getName()).log(Level.SEVERE, null, ex);
}
}
Download Projects Source Code