java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java统计文件字数

Java实现统计文件夹下所有文件的字数

作者:林小果呀

这篇文章主要为大家详细介绍了如何使用Java实现统计文件夹下所有文件的字数,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

统计文件夹下所有.md文件的字数

示例代码

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.regex.Pattern;

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

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

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

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

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

方法补充

除了上文的方法,小编还为大家整理了其他实现统计文件字数的方法,希望对大家有所帮助

Java统计文档的字数

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class WordCount {
    public static void main(String[] args) {
        // 读取文档路径
        String filePath = "path/to/your/document.txt";
        
        try {
            // 创建文件对象
            File file = new File(filePath);
            
            // 创建Scanner对象,用于读取文件内容
            Scanner scanner = new Scanner(file);
            
            // 统计字符个数的变量
            int count = 0;
            
            // 逐行读取文件内容,并统计字符个数
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                count += line.replaceAll("\\s+", "").length();
            }
            
            // 输出统计结果
            System.out.println("文档的字数是:" + count);
            
            // 关闭Scanner对象
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Java获取文件字数

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class WordCount {
    public static void main(String[] args) {
        String filename = "example.txt"; // 替换为要统计字数的文件路径

        int wordCount = 0;
        int spaceCount = 0;
        int punctuationCount = 0;

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

        System.out.println("字数: " + wordCount);
        System.out.println("空格数: " + spaceCount);
        System.out.println("标点符号数: " + punctuationCount);
    }
}

用python统计一个文件夹下的所有文件的中文字数

import os

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


def check_contain_chinese(check_str, fileName):
    countResult = 0
    for ch in check_str:
        if u'\u4e00' <= ch <= u'\u9fff':
            countResult += 1
    resultArray.append(countResult)
    print(str(fileName) + "文件的中文字数是:" + str(countResult) + '\n')


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

    for num in resultArray:
        resultCount += num
    print("累计文件个数:" + str(listCount) + "个")
    print("累计中文字符:" + str(resultCount) + "个")

到此这篇关于Java实现统计文件夹下所有文件的字数的文章就介绍到这了,更多相关Java统计文件字数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文