读取文件中出现次数最多的字符串

package kryoDemo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class ReadFile {

	public static String readFile(File file) {
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tmpStr = null;
			// 一次读入一行,直到读入null为文件结束
			while ((tmpStr = reader.readLine()) != null) {
				sb.append(System.lineSeparator() + tmpStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
				}
			}
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		File file = new File("E:/errlog.txt");
		if (!file.isFile() || !file.exists()) {
			throw new RuntimeException("not File Or file is not exist");
		}
		String str = readFile(file);
		String[] strArray = str.split(" |,");
		Map<String, Integer> m = new LinkedHashMap<String, Integer>(str.length());
		for (String detail : strArray) {
			m.put(detail, m.containsKey(detail) ? m.get(detail) + 1 : 1);
		}
		String maxStr = null;
		Integer maxNum = 0;
		for (Entry<String, Integer> entry : m.entrySet()) {
			if (entry.getValue() > maxNum) {
				maxNum = entry.getValue();
				maxStr = entry.getKey();
			}
		}
		System.out.println(maxStr);
	}

}

```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_WORD_LEN 100 // 最大单词长度 #define TOP_N 10 // 前N个出现次数最多的单词 // 单词结构体 typedef struct { char word[MAX_WORD_LEN]; // 单词 int count; // 出现次数 } Word; // 比较函数,用于qsort排序 int cmp(const void *a, const void *b) { return ((Word *)b)->count - ((Word *)a)->count;} int main() { char filename[100]; // 文件名 printf("请输入文件名:"); scanf("%s", filename); FILE *fp = fopen(filename, "r"); // 打开文件 if (fp == NULL) { printf("文件打开失败!\n"); return 0; } Word *words = (Word *)malloc(sizeof(Word) * 1000); // 动态分配内存 int wordCount = 0; // 单词数量 char word[MAX_WORD_LEN]; // 临时存储单词 int len = 0; // 单词长度 char c; // 临时存储字符 while ((c = fgetc(fp)) != EOF) { // 逐个字符读取文件 if (isalpha(c)) { // 如果是字母 if (len < MAX_WORD_LEN - 1) { // 如果单词长度未超过最大长度 word[len++] = tolower(c); // 转换为小写字母并存储 } } else if (len > 0) { // 如果不是字母且单词长度大于0 word[len] = '\0'; // 添加字符串结束符 int i; for (i = 0; i < wordCount; i++) { // 查找单词是否已存在 if (strcmp(words[i].word, word) == 0) { // 如果已存在 words[i].count++; // 出现次数加1 break; } } if (i == wordCount) { // 如果不存在 strcpy(words[wordCount].word, word); // 存储单词 words[wordCount].count = 1; // 出现次数为1 wordCount++; // 单词数量加1 } len = 0; // 重置单词长度 } } fclose(fp); // 关闭文件 qsort(words, wordCount, sizeof(Word), cmp); // 按出现次数排序 printf("出现次数前%d的单词:\n", TOP_N); int i; for (i = 0; i < TOP_N && i < wordCount; i++) { // 输出前N个单词 printf("%s\t%d\n", words[i].word, words[i].count); } free(words); // 释放内存 return 0; } ``` --相关问题--: 1. 如何统计一个文文本文件出现次数最多的前十个汉字
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值