SoFunction
Updated on 2025-03-08

Java implements counting the word count of all files in a folder

Count the number of words in all .md files in the folder

Sample code

import .*;
import ;
import .*;
import ;
import ;

public class WordCounter {
    private static final Pattern WORD_PATTERN = ("[a-zA-Z]+|[\u4e00-\u9fa5]");
    private static long totalWords = 0;

    public static void main(String[] args) throws IOException {
        Path startPath = ("path/to/your/directory"); // replace with your directory
        (startPath, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (().endsWith(".md")) {
                    totalWords += countWords(file);
                }
                return ;
            }

            private long countWords(Path file) throws IOException {
                long count = 0;
                try (BufferedReader reader = (file, StandardCharsets.UTF_8)) {
                    String line;
                    while ((line = ()) != null) {
                        count += WORD_PATTERN.split(line).length;
                    }
                }
                return count;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
                ("Visited directory: " + dir + ", total words: " + totalWords);
                return ;
            }
        });
        ("Total words in all .md files: " + totalWords);
    }
}

Method supplement

Java statistics on the word count of a document

import ;
import ;
import ;

public class WordCount {
    public static void main(String[] args) {
        // Read the document path        String filePath = "path/to/your/";
        
        try {
            // Create file object            File file = new File(filePath);
            
            // Create a Scanner object to read the file contents            Scanner scanner = new Scanner(file);
            
            // Variables that count characters            int count = 0;
            
            // Read the file content line by line and count the number of characters            while (()) {
                String line = ();
                count += ("\\s+", "").length();
            }
            
            // Output statistics            ("The word count of the document is:" + count);
            
            // Close the Scanner object            ();
        } catch (FileNotFoundException e) {
            ();
        }
    }
}

Java gets the number of file words

import ;
import ;
import ;

public class WordCount {
    public static void main(String[] args) {
        String filename = ""; // Replace with the file path to count the number of words
        int wordCount = 0;
        int spaceCount = 0;
        int punctuationCount = 0;

        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = ()) != null) {
                String[] words = ("\\s+");
                wordCount += ;
                spaceCount +=  - 1;
                
                for (char c : ()) {
                    if ((c)) {
                        spaceCount++;
                    } else if ((c) || (c)) {
                        // do nothing
                    } else {
                        punctuationCount++;
                    }
                }
            }
        } catch (IOException e) {
            ();
        }

        ("Word count: " + wordCount);
        ("Number of spaces: " + spaceCount);
        ("Punctuation number: " + punctuationCount);
    }
}

Use python to count the number of Chinese characters in all files in a folder

import os

DirPath = 'D:/Download/docs'
resultArray = []
listCount = 0
content = ''
resultCount = 0


def check_contain_chinese(check_str, fileName):
    countResult = 0
    for ch in check_str:
        if u'\u4e00' &lt;= ch &lt;= u'\u9fff':
            countResult += 1
    (countResult)
    print(str(fileName) + "The Chinese word count of the file is:" + str(countResult) + '\n')


if __name__ == "__main__":
    for item in (DirPath):
        print(DirPath + '/' + item)
        listCount += 1
        f = open(DirPath + '/' + item, 'r', encoding='utf-8')
        content = ()
        check_contain_chinese(content, item)

    for num in resultArray:
        resultCount += num
    print("Cumulative number of files:" + str(listCount) + "indivual")
    print("Accumulated Chinese characters:" + str(resultCount) + "indivual")

This is the article about the word count of all files in the Java implementation statistics folder. For more related Java statistics files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!