计算字符串在文件中出现的次数

时间:2012-06-16 19:52:19

标签: java string filereader

我正在试图找出如何读取文件,然后计算某个字符串出现的次数。

这就是我的文件,它是一个.txt:

    Test
    Test
    Test
    Test

我希望该方法返回它在文件中的次数。关于如何做到这一点的任何想法?我主要需要第一部分的帮助。因此,如果我在搜索字符串“Test”,我希望它返回4。

先谢谢了!希望我提供足够的信息!

4 个答案:

答案 0 :(得分:0)

将此方法添加到您的类中,将FileInputStream传递给它,它应该返回文件中的单词数。请记住,这是区分大小写的。

public int countWord(String word, FileInputStream fis) {
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String readLine = "";
int count = 0;
while((readLine = in.readLine()) != null) {
String words = readLine.split(" ");
for(String s : words) {
if(s.equals(word)) count++;
}
return count;
}

现在只是写了,而且没有经过测试,所以让我知道它是否有效。另外,如果这是一个家庭作业问题,请确保你理解我做了什么。

答案 1 :(得分:0)

你在这里:

public int countStringInFile(String stringToLookFor, String fileName){
  int count = 0;
  try{
    FileInputStream fstream = new FileInputStream(fileName);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine()) != null)   {
      int startIndex = strLine.indexOf(stringToLookFor);
      while (startIndex != -1) {
        count++;
        startIndex = base.indexOf(stringToLookFor, 
                                  startIndex +stringToLookFor.length());
      }
    }
    in.close();
  }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
  }
  return count;
}

用法:int count = countStringInFile("SomeWordToLookFor", "FileName");

答案 2 :(得分:0)

如果您已将每个文件中的读取内容转换为字符串,我建议您查看String方法拆分。

给它字符串代码'Test',它将返回一个string类型的数组 - 计算每行的元素数。总结它们以获得总发生率。

import java.io.*;

public class StringCount {
    public static void main(String args[]) throws Exception{

        String testString = "Test";
        String filePath = "Test.txt";
        String strLine;
        int numRead=0;

        try {
            FileInputStream fstream = new FileInputStream(filePath);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            while ((strLine = br.readLine()) != null)   {
                strLine = strLine + " ";
                String [] strArry = strLine.split(testString);

                if (strArry.length > 1) {
                     numRead = numRead + strArry.length - 1;
                }
                else {
                     if (strLine == testString) {
                         numRead++;
                     }
                }
            }

            in.close();

            System.out.println(testString + " was found " + numRead + " times.");

        }catch (Exception e){
        }
    }
}

答案 3 :(得分:0)

我会这样做:

  1. 逐行打开并阅读文件
  2. 检查一行如何包含给定的单词......
  3. 为此增加全球反击..
  4. public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("Test.txt"));
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the subtring to look for: ");
        String word = sc.next();
        String line = in.readLine();
        int count = 0;
        do {
        count += (line.length() - line.replace(word, "").length()) / word.length();
        line = in.readLine();
        } while (line != null);
        System.out.print("There are " + count + " occurrences of " + word + " in ");
    }