It's not a secret anymore!
The moose likes Beginning Java and the fly likes How to find a word in a text file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find a word in a text file?" Watch "How to find a word in a text file?" New topic

How to find a word in a text file?

Ekrem Altintas

Joined: Dec 20, 2005
Posts: 14
I'm want to find a word in a text file which clas and method is suitable for this purpose?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Look at FileReader, BufferedReader, and String.
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
There isn't a single class that will do this for you. Your basic algorithm will involve reading the file contents and then searching the data for the target text.

There are several ways you could read the file but I would suggest using a BufferedReader (which requires creating another Reader object, maybe FileReader since you can just give that the file name). BufferedReader provides a readLine() method which gives you a String to work with.

Once you have the string you can use the indexOf() method from the String class to find your target text.

Of course you'll need a loop for reading a line at a time from the file.

Note that if you are using the default character encoding. If you aren't then you'll need to create your own InputStreamReader and specify the character encoding.


Mike Noel
I agree. Here's the link:
subject: How to find a word in a text file?
It's not a secret anymore!