Java实现统计文件夹下所有文件的字数
更新时间:2024年03月25日 09:33:22 作者:林小果呀
这篇文章主要为大家详细介绍了如何使用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统计文件字数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot中@ComponentScan注解过滤排除不加载某个类的3种方法
这篇文章主要给大家介绍了关于SpringBoot中@ComponentScan注解过滤排除不加载某个类的3种方法,文中通过实例代码介绍的非常详细,对大家学习或者使用SpringBoot具有一定的参考学习价值,需要的朋友可以参考下2023-07-07
使用Spring Data Jpa的CriteriaQuery一个陷阱
使用Spring Data Jpa的CriteriaQuery进行动态条件查询时,可能会遇到一个陷阱,当条件为空时,查询不到任何结果,并不是期望的返回所有结果。这是为什么呢?2020-11-11
SpringBoot静态方法调用Spring容器bean的三种解决方案
在SpringBoot中静态方法调用Spring容器bean时出现的null值问题,本文就来介绍一下SpringBoot静态方法调用Spring容器bean的三种解决方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧2025-01-01
Spring Boot使用Thymeleaf + Gradle构建war到Tomcat
今天小编就为大家分享一篇关于Spring Boot使用Thymeleaf + Gradle构建war到Tomcat,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2018-12-12


最新评论