Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中搜索多个文件中的word并显示结果,而无需重复路径?_Java_Android - Fatal编程技术网

如何在java中搜索多个文件中的word并显示结果,而无需重复路径?

如何在java中搜索多个文件中的word并显示结果,而无需重复路径?,java,android,Java,Android,我有Stack中的代码,我刚刚编辑了它,以便能够在path中的多个文件中搜索一个单词,但当我在TextView中显示结果时,我会得到重复的路径(如下图),我希望得到的结果是: /示例/path/to/folder/file1.txt 第1行:单词 第2行:单词 /示例/path/to/folder/file2.txt 第5行:单词 第13行:单词 而不是这样: /示例/path/to/folder/file1.txt 第1行:单词 /示例/path/to/folder/file1.txt 第5行

我有Stack中的代码,我刚刚编辑了它,以便能够在path中的多个文件中搜索一个单词,但当我在TextView中显示结果时,我会得到重复的路径(如下图),我希望得到的结果是:

/示例/path/to/folder/file1.txt 第1行:单词 第2行:单词 /示例/path/to/folder/file2.txt 第5行:单词 第13行:单词

而不是这样:

/示例/path/to/folder/file1.txt 第1行:单词 /示例/path/to/folder/file1.txt 第5行:单词 /示例/path/to/folder/file1.txt 第5行:单词

这是我的代码:

textview1.setText(""); 
  
  File folder = new File("/sdcard/Alarms/");
   File[] brr = folder.listFiles();
  
  
  for (File f1 : brr) {
  
      String[] words=null;  
   
   try {
   
      FileReader fr = new FileReader(f1);  
   
      BufferedReader br = new BufferedReader(fr); 
   
      String s;     
      String input="salut";   
   
      int count=0;   
  
      while((s=br.readLine())!=null)   
      {
         words=s.split(" ");  
   count++;
          for (String word : words) 
          {
                 if (word.equals(input))   
     
                 {
      
      
      textview1.append(f1.getPath()+"\n"+"line"+ " "+String.valueOf(count) + ": " + word + "\n");
                    
                 }
          }
      }
      if(count!=0)  
      {
         System.out.println("The given word is present for "+count+ " Times in the file");
      }
      else
      {
         System.out.println("The given word is not present in the file");
      }
      
         fr.close();
} catch (Exception e) {
e.printStackTrace();
}

}
       

有什么建议吗?

您正在计算包含单词的行数,而不是单词在文件中出现的次数。你有两个选择

  • 如果只想对包含多次出现的word的行计数一次,则需要打断if语句并在for循环外显示文本
  • 如果要精确计算单词的出现次数,则需要为单词的出现使用不同的计数器,并且在if语句之后仍然显示,但在if块内递增计数
例如,处理第二种情况的一种方法可能是

    lIndex = 0;
    while((s=br.readLine())!=null) {
        wcount = 0;
        lIndex++; // line to check
        words = s.split(" ");  
        for (String word : words) {
            if (word.equals(input)) {
                // [one more] occurrence of word found
                wcount++;
            }
        }
        if (wcount > 0) {
            textview1.append(f1.getPath()+"\nline " + lIndex ": " + word + " occurs " + wcount + " times\n");
        }
    }

给我举个小例子,兄弟,我试过了,我很困惑,如果你给我举个小例子,我会很感激的。我已经添加了一个小例子,其余的你应该能够理解。哦,非常感谢兄弟,不,我明白