猿问

如何查找某个单词在文本文件的哪一行,如果该单词存在于多行中,则保存行号?

我可以找到该单词的出现位置,但无法找到该单词所在的行号,以及有什么方法可以像数组列表一样保存行号?


  File f1=new File("input.txt")

  String[] words=null;  //Intialize the word Array

  FileReader fr = new FileReader(f1);  //Creation of File Reader object

  BufferedReader br = new BufferedReader(fr); 

  String s;     

  String input="Java";   // Input word to be searched

  int count=0;   //Intialize the word to zero

  while((s=br.readLine())!=null)   //Reading Content from the file

  {

     words=s.split(" ");  //Split the word using space

      for (String word : words) 

      {

             if (word.equals(input))   //Search for the given word

             {

               count++;    //If Present increase the count by one

             }

      }

  }

  if(count!=0)  //Check for count not equal to zero

  {

     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();

   }



   }


慕村225694
浏览 128回答 3
3回答

千万里不及你

有一个计数器并对每一行进行计数。&nbsp; &nbsp; long count = 0;&nbsp; &nbsp; long lineNumberCounter = 0;&nbsp; &nbsp; List<Long> lineNumbers = new ArrayList<>();&nbsp; &nbsp; try (BufferedReader b = new BufferedReader(new java.io.FileReader(new File(fileName)))) {&nbsp; &nbsp; &nbsp; &nbsp; String readLine = "";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Reading file using Buffered Reader");&nbsp; &nbsp; &nbsp; &nbsp; while ((readLine = b.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Here is line number counter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineNumberCounter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] words = readLine.split(" "); // Split the word using space&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(words));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String word : words) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Search for the given word&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (word.trim().equals(input)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++; // If Present increase the count by one&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Word " + input + " found in line " + lineNumberCounter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineNumbers.add(lineNumberCounter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Check for count not equal to zero&nbsp; &nbsp; if (count != 0) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The given word is present for " + count + " Times in the file");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The given word is not present in the file");&nbsp; &nbsp; }

蓝山帝景

我认为这会有所帮助。您所要做的就是跟踪行号,然后保存该单词可用的行File f1=new File("input.txt")String[] words=null;&nbsp; //Intialize the word ArrayFileReader fr = new FileReader(f1);&nbsp; //Creation of File Reader objectBufferedReader br = new BufferedReader(fr);&nbsp;String s;&nbsp; &nbsp; &nbsp;String input="Java";&nbsp; &nbsp;// Input word to be searchedint count=0;&nbsp; &nbsp;//Intialize the word to zero// for keeping track of the line numbersint lineNumber= 0;&nbsp;//arraylist to save the numbersList<int> lineNumberList = new ArrayList<>();while((s=br.readLine())!=null)&nbsp; &nbsp;//Reading Content from the file{&nbsp; // increase the line number as we move on to the next line&nbsp; lineNumber++;&nbsp;words=s.split(" ");&nbsp; //Split the word using space&nbsp; // this is required so that same line number won't be repeated on the arraylist&nbsp; boolean flag = true;&nbsp; for (String word : words)&nbsp;&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (word.equals(input))&nbsp; &nbsp;//Search for the given word&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count++;&nbsp; &nbsp; //If Present increase the count by one&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(flag){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lineNumberList.add(lineNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;flag=false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; }&nbsp;}if(count!=0)&nbsp; //Check for count not equal to zero&nbsp;{&nbsp;System.out.println("The given word is present for "+count+ " Times in the file");&nbsp;}&nbsp;else&nbsp; {&nbsp;System.out.println("The given word is not present in the file");&nbsp;}&nbsp;fr.close();&nbsp;}}

尚方宝剑之说

尝试使用LineNumberReader而不是BufferedReader. 它支持 BufferedReader 和 LineNumber。Javadoc 了解更多信息 - https://docs.oracle.com/javase/8/docs/api/java/io/LineNumberReader.html。例子 -LineNumberReader lineNumberReader =&nbsp;&nbsp; &nbsp; new LineNumberReader(new FileReader("c:\\data\\input.txt"));int data = lineNumberReader.read();while(data != -1){&nbsp; &nbsp; char dataChar = (char) data;&nbsp; &nbsp; data = lineNumberReader.read();&nbsp; &nbsp; // your word processing happens here&nbsp; &nbsp; int lineNumber = lineNumberReader.getLineNumber();}lineNumberReader.close();http://tutorials.jenkov.com/java-io/linenumberreader.html
随时随地看视频慕课网APP

相关分类

Java
我要回答