0

我试图弄清楚我将如何读取文件,然后计算某个字符串出现的次数。

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

    Test
    Test
    Test
    Test

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

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

4

4 回答 4

1

将此方法添加到您的类中,将您的 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;
}

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

于 2012-06-16T19:59:41.830 回答
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");

于 2012-06-16T20:00:56.017 回答
0

如果您必须将每个文件读入一个字符串,我建议您查看 String 方法拆分。

给它字符串代码'Test',它将返回一个字符串类型的数组 - 计算每行的元素数。把它们加起来得到你的总发生率。

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){
        }
    }
}
于 2012-06-16T20:06:38.227 回答
0

我会这样做:

  1. 打开并逐行读取文件,
  2. 检查一行包含给定单词的频率...
  3. 为此增加一个全球计数器..

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 ");
}
于 2016-05-10T18:22:02.420 回答