aspose file tools*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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
Author

How to find a word in a text file?

Ekrem Altintas
Greenhorn

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: 2367
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.

_M_


Mike Noel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find a word in a text file?