欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(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ù)的方法,希望對(duì)大家有所幫助

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)建文件對(duì)象
            File file = new File(filePath);
            
            // 創(chuàng)建Scanner對(duì)象,用于讀取文件內(nèi)容
            Scanner scanner = new Scanner(file);
            
            // 統(tǒng)計(jì)字符個(gè)數(shù)的變量
            int count = 0;
            
            // 逐行讀取文件內(nèi)容,并統(tǒng)計(jì)字符個(gè)數(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對(duì)象
            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)符號(hào)數(shù): " + punctuationCount);
    }
}

用python統(tǒng)計(jì)一個(gè)文件夾下的所有文件的中文字?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ì)文件個(gè)數(shù):" + str(listCount) + "個(gè)")
    print("累計(jì)中文字符:" + str(resultCount) + "個(gè)")

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

相關(guān)文章

  • Springboot添加支付接口

    Springboot添加支付接口

    這篇文章主要介紹了springboot如何添加支付接口,幫助大家更好得理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下
    2021-04-04
  • Java實(shí)現(xiàn)微信登錄并獲取用戶信息功能(開發(fā)流程)

    Java實(shí)現(xiàn)微信登錄并獲取用戶信息功能(開發(fā)流程)

    這篇文章主要介紹了Java實(shí)現(xiàn)微信登錄并獲取用戶信息功能(開發(fā)流程),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • java8中:: 用法示例(JDK8雙冒號(hào)用法)

    java8中:: 用法示例(JDK8雙冒號(hào)用法)

    這篇文章主要給大家介紹了關(guān)于java8 中的:: 用法(JDK8雙冒號(hào)用法)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Spring MVC 關(guān)于controller的字符編碼問(wèn)題

    Spring MVC 關(guān)于controller的字符編碼問(wèn)題

    在使用springMVC框架構(gòu)建web應(yīng)用,客戶端常會(huì)請(qǐng)求字符串、整型、json等格式的數(shù)據(jù),通常使用@ResponseBody注解使 controller回應(yīng)相應(yīng)的數(shù)據(jù)而不是去渲染某個(gè)頁(yè)面。
    2017-03-03
  • java -jar命令的具體使用

    java -jar命令的具體使用

    java -jar命令是一種方便快捷地在命令行中運(yùn)行Java可執(zhí)行jar文件的方法,本文主要介紹了java -jar命令的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • java基本教程之java線程等待與java喚醒線程 java多線程教程

    java基本教程之java線程等待與java喚醒線程 java多線程教程

    這篇文章主要介紹了對(duì)線程等待/喚醒方法,文中使用了多個(gè)示例,大家參考使用吧
    2014-01-01
  • 基于Java網(wǎng)絡(luò)編程和多線程的多對(duì)多聊天系統(tǒng)

    基于Java網(wǎng)絡(luò)編程和多線程的多對(duì)多聊天系統(tǒng)

    這篇文章主要介紹了基于Java網(wǎng)絡(luò)編程和多線程的多對(duì)多聊天系統(tǒng),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java網(wǎng)絡(luò)編程的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • java多線程并發(fā)executorservice(任務(wù)調(diào)度)類

    java多線程并發(fā)executorservice(任務(wù)調(diào)度)類

    這篇文章主要介紹了線程并發(fā)ScheduledExecutorService類,設(shè)置 ScheduledExecutorService ,2秒后,在 1 分鐘內(nèi)每 10 秒鐘蜂鳴一次
    2014-01-01
  • springboot項(xiàng)目啟動(dòng)后執(zhí)行方法的三種方式

    springboot項(xiàng)目啟動(dòng)后執(zhí)行方法的三種方式

    有時(shí)項(xiàng)目需求,需要項(xiàng)目啟動(dòng)的時(shí)候向數(shù)據(jù)庫(kù)中查詢一下系統(tǒng)屬性,或者需要加載某個(gè)特定的方法,下面這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目啟動(dòng)后執(zhí)行方法的三種方式,需要的朋友可以參考下
    2022-06-06
  • Java Scala的隱式轉(zhuǎn)換詳解

    Java Scala的隱式轉(zhuǎn)換詳解

    隱式轉(zhuǎn)換是在Scala編譯器進(jìn)行類型匹配時(shí),如果找不到合適的類型,那么隱式轉(zhuǎn)換會(huì)讓編譯器在作用范圍內(nèi)自動(dòng)推導(dǎo)出來(lái)合適的類型。本文通過(guò)代碼示例介紹了Scala的隱式轉(zhuǎn)換,感興趣的小伙伴可以參考閱讀
    2023-04-04

最新評(píng)論