how to find a string in file

i need to find a string in a file and print
each line of the file that the string in it.

how i should do it?

detailed code will be a great help

thanks
talspAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

blairdyeCommented:
Sorry about the formatting, just cut and paste.

import java.io.*;
import java.util.*;
public class OutputStringClass {
 String fileIn;
 private BufferedReader inputFile;
 public OutputStringClass(String fileIn) {
      super();
      this.fileIn = fileIn;
      try {
            inputFile = new BufferedReader(new FileReader(fileIn));
      } catch (Exception e) {
            System.out.println("Exception in OutputStringClass constructor" + e);
      }
 }
      
 public static void main(String[] args) {
      OutputStringClass outputStringClass = new OutputStringClass("c:/tmp/test.txt");
      outputStringClass.writeString("textstring");
 }
      
 public void writeString(String toWrite) {
      String line;
      try {
            while ((line = inputFile.readLine()) != null) {
                  StringTokenizer myTokenizer = new StringTokenizer(line);
                  while (myTokenizer.hasMoreTokens()) {
                        String nextToken = myTokenizer.nextToken();
                        if (nextToken.equals(toWrite)) {
                              System.out.println("line =" + line);
                        }
                  }
            }
      } catch (Exception e) {
        System.out.println("Error in writeString() " + e);
      }
 }
}

Blair
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.