统计一个字符串在文本文件中的出现次数

代码实现:

package com.jn.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class StringTimes {
	 public static int getKeyStringCount(String str, String key) {
         int count = 0;
         int index = 0;
         while((index = str.indexOf(key,index))!=-1){
             index = index + key.length();
             count++;
         }
         return count;
     }  

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file = new File("D:/设计模式.txt");
		FileInputStream fis = null;
		try{
			fis = new FileInputStream(file);
			int len = 0;
			byte[] buf = new byte[1024];
			String str = null;
			while((len = fis.read(buf)) !=-1){
				str = new String(buf, 0, len,"GBK");
			}
			Scanner sc  = new Scanner(System.in);
			System.out.println("请输入你要查询字符串:");
			 String key = sc.nextLine();
             int count = getKeyStringCount(str,key);
             System.out.println("文件中此字符串出现次数为:"+count+"次");
		}catch(FileNotFoundException e){
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

  运行及结果:

请输入你要查询字符串:
设计模式
文件中此字符串出现次数为:10次

  

时间: 2024-08-30 13:51:20

统计一个字符串在文本文件中的出现次数的相关文章

统计一个字符串中的单词的个数,并打印各个单词

/*测试数据:Shen zhen is a beautiful city!*/ /*运行结果:Word:6 Shen zhen is a beautiful city!*/ #include<stdio.h> #define SIZE 1000 void wordCount(char *str) { int count = 0, flag = 0; char *p = str; while (*p != '\0'){ while (*p == 32){ if (*(p + 1) == 0){/

字符串之“统计一个字符串中单词的个数”

题目:统计一个字符串中单词的个数 输入一行字符,统计其中有多少个单词,单词之间用空格分隔开 输入:my name is jacky 输出:the number of word is 4 代码如下: #include <stdio.h> int main(int argc, char *argv[]) { char str[80]; int i=0,num=0,flag=0; char c; gets(str); while((c=str[i])!='\0') { if(c==' ') flag

黑马程序员——统计一个字符串中各个字符出现的次数

统计一个字符串中各个字符出现的次数 import java.util.Iterator; import java.util.Set; import java.util.TreeMap; public class TreeMapDemo { //统计一个字符串中相应字符出现的次数 public static void main(String[] args) { // String s = "aagfagdlkerjgavpofjmvglk我是你的"; //调用自定义方法来 统计相应字符出

统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数。

/** * A:案例演示 * 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数. * [email protected]#$%^ * 分析:字符串是有字符组成的,而字符的值都是有范围的,通过范围来判断是否包含该字符 * 如果包含就让计数器变量自增 */ public static void main(String[] args) { String s = "[email protected]#$%^"; int big = 0; int smal

java怎么实现统计一个字符串中字符出现的次数

问题:假设字符串仅仅保护a-z 的字母,java怎么实现统计一个字符串中字符出现的次数?而且,如果压缩后的字符数不小于原始字符数,则返回. 处理逻辑:首先拆分字符串,以拆分出的字符为key,以字符出现次数为value,存入Map中. 源码如下: 1 import java.util.HashMap; 2 import java.util.Iterator; 3 import java.util.Map; 4 5 public class TestCompress { 6 7 public sta

统计一个字符串中第一次出现且频率最高的字符

统计一个字符串中第一次出现且频率最高的字符. public static char statMostRateChar(String str) { if (str != null && !"".equals(str)) { int charsStat[] = new int[128]; int charsFirstIdx[] = new int[128]; int strLen = str.length(); for (int ch = 0; ch < 128;ch

读取一个文件,给定一个字符串,判断这个字符串在文件中出现的次数

读取一个文件,给定一个字符串,判断这个字符串在文件中出现的次数,面试笔试经常遇到的问题 public class CountStringTest { public static void main(String[] args) { try { //统计E盘下面test.txt中的q字符出现的次数 System.out.println("E盘下面test.txt中的q字符出现的次数为:"); System.err.println(count("E:\\test.txt"

用c语言统计一个字符串中有多少个数字字符

用c语言统计一个字符串中有多少个数字字符. #include<stdio.h>int main(){    char ch;     int count=0;    while((ch=getchar())!='\n')     {        if(ch>'0'&&ch<'9')              count++;     }     printf("%d\n",count);     return 0; }

统计一个字符串中英文字母、空格、数字和其它字符的个数

1 package demo; 2 import java.util.Scanner; 3 /** 4 * 统计一个字符串中英文字母.空格.数字和其它字符的个数 5 */ 6 public class Statistics1 { 7 public static void main(String[]args){ 8 int i; 9 int LetterCount = 0; 10 int SpaceCount = 0; 11 int NumberCount = 0; 12 int OtherCou