Affichage des articles dont le libellé est Count Words In Text File Using Java. Afficher tous les articles
Affichage des articles dont le libellé est Count Words In Text File Using Java. Afficher tous les articles

Java Count Words In Text File

How To Count Words Inside A Txt File Using Java NetBeans

Count Words In Text File Using Java



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);
        }
        
    }