Question

How to print lines from a file that contain a specific word using java ?

Want to create a simple utility that allows to find a word in a file and prints the complete line in which given word is present.

I have done this much to count the occurence but don't knoe hoe to print the line containing it...

      import java.io.*;

       public class SearchThe {
          public static void main(String args[]) 
           {
             try 
              {
                String stringSearch = "System";

                 BufferedReader bf = new BufferedReader(new FileReader("d:/sh/test.txt"));

         int linecount = 0;
            String line;

        System.out.println("Searching for " + stringSearch + " in file...");

        while (( line = bf.readLine()) != null)
        {
            linecount++;
            int indexfound = line.indexOf(stringSearch);

            if (indexfound > -1) 
            {
                System.out.println("Word is at position " + indexfound + " on line " + linecount);
            }
        }
        bf.close();
    }
    catch (IOException e) 
    {
        System.out.println("IO Error Occurred: " + e.toString());
    }
}

}

Was it helpful?

Solution

Suppose you are reading from a file named file1.txt Then you can use the following code to print all the lines which contains a specific word. And lets say you are searching for the word "foo".

import java.util.*;
import java.io.*;
    public class Classname
    {
        public static void main(String args[])
        {
        File file =new File("file1.txt");
        Scanner in = null;
        try {
            in = new Scanner(file);
            while(in.hasNext())
            {
                String line=in.nextLine();
                if(line.contains("foo"))
                    System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }}

Hope this code helps.

OTHER TIPS

public static void grep(Reader inReader, String searchFor) throws IOException {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(inReader);
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains(searchFor)) {
                System.out.println(line);
            }
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

Usage:

   grep(new FileReader("file.txt"), "GrepMe");

Have a look at BufferedReader or Scanner for reading the file. To check if a String contains a word use contains from the String-class.

If you show some effort I'm willing to help you out more.

you'll need to do something like this

   public void readfile(){
     try {

        BufferedReader br;
        String line;

        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("file path"), "UTF-8");
        br = new BufferedReader(inputStreamReader);
        while ((line = br.readLine()) != null) {
          if (line.contains("the thing I'm looking for")) {
            //do something 
          }
          //or do this
          if(line.matches("some regular expression")){
            //do something
          }
        }

        // Done with the file
        br.close();
        br = null;

      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top