最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)統(tǒng)計(jì)文件夾下所有文件的字?jǐn)?shù)

 更新時間:2024年03月25日 09:33:22   作者:林小果呀  
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)統(tǒng)計(jì)文件夾下所有文件的字?jǐn)?shù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

統(tǒng)計(jì)文件夾下所有.md文件的字?jǐn)?shù)

示例代碼

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

方法補(bǔ)充

除了上文的方法,小編還為大家整理了其他實(shí)現(xiàn)統(tǒng)計(jì)文件字?jǐn)?shù)的方法,希望對大家有所幫助

Java統(tǒng)計(jì)文檔的字?jǐn)?shù)

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 {
            // 創(chuàng)建文件對象
            File file = new File(filePath);
            
            // 創(chuàng)建Scanner對象,用于讀取文件內(nèi)容
            Scanner scanner = new Scanner(file);
            
            // 統(tǒng)計(jì)字符個數(shù)的變量
            int count = 0;
            
            // 逐行讀取文件內(nèi)容,并統(tǒng)計(jì)字符個數(shù)
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                count += line.replaceAll("\\s+", "").length();
            }
            
            // 輸出統(tǒng)計(jì)結(jié)果
            System.out.println("文檔的字?jǐn)?shù)是:" + count);
            
            // 關(guān)閉Scanner對象
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Java獲取文件字?jǐn)?shù)

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"; // 替換為要統(tǒng)計(jì)字?jǐn)?shù)的文件路徑

        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("字?jǐn)?shù): " + wordCount);
        System.out.println("空格數(shù): " + spaceCount);
        System.out.println("標(biāo)點(diǎn)符號數(shù): " + punctuationCount);
    }
}

用python統(tǒng)計(jì)一個文件夾下的所有文件的中文字?jǐn)?shù)

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) + "文件的中文字?jǐn)?shù)是:" + 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("累計(jì)文件個數(shù):" + str(listCount) + "個")
    print("累計(jì)中文字符:" + str(resultCount) + "個")

到此這篇關(guān)于Java實(shí)現(xiàn)統(tǒng)計(jì)文件夾下所有文件的字?jǐn)?shù)的文章就介紹到這了,更多相關(guān)Java統(tǒng)計(jì)文件字?jǐn)?shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot中@ComponentScan注解過濾排除不加載某個類的3種方法

    SpringBoot中@ComponentScan注解過濾排除不加載某個類的3種方法

    這篇文章主要給大家介紹了關(guān)于SpringBoot中@ComponentScan注解過濾排除不加載某個類的3種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用SpringBoot具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-07-07
  • Spring?Web?MVC基礎(chǔ)理論概念

    Spring?Web?MVC基礎(chǔ)理論概念

    Spring?Web?MVC是基于Servlet?API構(gòu)建的原始Web框架,從?開始就包在Spring框架中,Spring?Web?MVC是一個Web框,本文給大家介紹Spring?Web?MVC基礎(chǔ)理論,感興趣的朋友一起看看吧
    2024-08-08
  • Java異常處理 如何跟蹤異常的傳播路徑

    Java異常處理 如何跟蹤異常的傳播路徑

    這篇文章主要介紹了Java異常處理 如何跟蹤異常的傳播路徑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • 淺析Spring IOC 依賴查找你需要知道的幾種方式

    淺析Spring IOC 依賴查找你需要知道的幾種方式

    這篇文章主要介紹了淺析Spring IOC 依賴查找的幾種方式,Spring是Java面試中最??嫉?,學(xué)Java的小伙伴快來看看吧
    2021-08-08
  • 使用Spring Data Jpa的CriteriaQuery一個陷阱

    使用Spring Data Jpa的CriteriaQuery一個陷阱

    使用Spring Data Jpa的CriteriaQuery進(jìn)行動態(tài)條件查詢時,可能會遇到一個陷阱,當(dāng)條件為空時,查詢不到任何結(jié)果,并不是期望的返回所有結(jié)果。這是為什么呢?
    2020-11-11
  • Java計(jì)時器工具StopWatch的具體使用

    Java計(jì)時器工具StopWatch的具體使用

    計(jì)時器在很多地方都可以用到,本文主要介紹了Java計(jì)時器工具StopWatch的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 解決mybatis映射mapper.xml文件不編譯的問題

    解決mybatis映射mapper.xml文件不編譯的問題

    這篇文章主要介紹了解決mybatis映射mapper.xml文件不編譯的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • SpringBoot靜態(tài)方法調(diào)用Spring容器bean的三種解決方案

    SpringBoot靜態(tài)方法調(diào)用Spring容器bean的三種解決方案

    在SpringBoot中靜態(tài)方法調(diào)用Spring容器bean時出現(xiàn)的null值問題,本文就來介紹一下SpringBoot靜態(tài)方法調(diào)用Spring容器bean的三種解決方案,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • SpringAOP中基于注解實(shí)現(xiàn)通用日志打印方法詳解

    SpringAOP中基于注解實(shí)現(xiàn)通用日志打印方法詳解

    這篇文章主要介紹了SpringAOP中基于注解實(shí)現(xiàn)通用日志打印方法詳解,在日常開發(fā)中,項(xiàng)目里日志是必不可少的,一般有業(yè)務(wù)日志,數(shù)據(jù)庫日志,異常日志等,主要用于幫助程序猿后期排查一些生產(chǎn)中的bug,需要的朋友可以參考下
    2023-12-12
  • Spring Boot使用Thymeleaf + Gradle構(gòu)建war到Tomcat

    Spring Boot使用Thymeleaf + Gradle構(gòu)建war到Tomcat

    今天小編就為大家分享一篇關(guān)于Spring Boot使用Thymeleaf + Gradle構(gòu)建war到Tomcat,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12

最新評論

合作市| 蒙山县| 镶黄旗| 尼勒克县| 泉州市| 宁都县| 浦江县| 余姚市| 正镶白旗| 兰坪| 荣昌县| 宜昌市| 饶阳县| 贡嘎县| 广饶县| 武安市| 通山县| 侯马市| 云阳县| 佛学| 浪卡子县| 海丰县| 东台市| 瑞丽市| 荆门市| 图木舒克市| 蒙阴县| 洱源县| 宣威市| 陵川县| 长岛县| 隆子县| 武乡县| 淮南市| 衡东县| 类乌齐县| 大丰市| 灵川县| 都兰县| 黄骅市| 菏泽市|