简体   繁体   English

如何找到一个单词在文本文件的哪一行中,以及该单词是否存在于多行中,保存行号?

[英]How to find a word is in which line of a text file and if the word exists in multiple line save the line number?

I could find the occurrence of the word but couldn't locate which line numbers is the word present and any way to save the line numbers like in arraylist? 我可以找到该单词的出现,但找不到该单词存在的行号,也无法像在arraylist中那样保存行号?

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


   }

Try using LineNumberReader instead of BufferedReader . 尝试使用LineNumberReader而不是BufferedReader It supports BufferedReader and LineNumber. 它支持BufferedReader和LineNumber。

Javadocs for more information - https://docs.oracle.com/javase/8/docs/api/java/io/LineNumberReader.html . Javadoc获取更多信息- https://docs.oracle.com/javase/8/docs/api/java/io/LineNumberReader.html

Example - 范例-

LineNumberReader lineNumberReader = 
    new LineNumberReader(new FileReader("c:\\data\\input.txt"));

int data = lineNumberReader.read();
while(data != -1){
    char dataChar = (char) data;
    data = lineNumberReader.read();
    // your word processing happens here
    int lineNumber = lineNumberReader.getLineNumber();
}
lineNumberReader.close();

http://tutorials.jenkov.com/java-io/linenumberreader.html http://tutorials.jenkov.com/java-io/linenumberreader.html

Have a counter and count for each line. 有一个计数器,为每一行计数。

    long count = 0;
    long lineNumberCounter = 0;
    List<Long> lineNumbers = new ArrayList<>();
    try (BufferedReader b = new BufferedReader(new java.io.FileReader(new File(fileName)))) {

        String readLine = "";

        System.out.println("Reading file using Buffered Reader");

        while ((readLine = b.readLine()) != null) {
            // Here is line number counter
            lineNumberCounter++;
            String[] words = readLine.split(" "); // Split the word using space
            System.out.println(Arrays.toString(words));
            for (String word : words) {
                // Search for the given word
                if (word.trim().equals(input)) {
                    count++; // If Present increase the count by one
                    System.out.println("Word " + input + " found in line " + lineNumberCounter);
                    lineNumbers.add(lineNumberCounter);
                }
            }
        }
    }

    // Check for count not equal to zero
    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");
    }

I think this will help. 我认为这会有所帮助。
All you have to do is track the line number and then save the line where the word is available 您所要做的就是跟踪行号,然后将行保存在有单词的位置

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

// for keeping track of the line numbers
int lineNumber= 0; 

//arraylist to save the numbers
List<int> lineNumberList = new ArrayList<>();

while((s=br.readLine())!=null)   //Reading Content from the file
{
  // increase the line number as we move on to the next line
  lineNumber++;

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

  // this is required so that same line number won't be repeated on the arraylist
  boolean flag = true;
  for (String word : words) 
  {

         if (word.equals(input))   //Search for the given word
         {
           count++;    //If Present increase the count by one
           if(flag){
               lineNumberList.add(lineNumber);
               flag=false;
            }
         }
  }
 }
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();
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM