简体   繁体   中英

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?

  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 . It supports BufferedReader and LineNumber.

Javadocs for more information - 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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