You'll need to be a bit more specific. A simple way to search a text file of arbitrary size is to simply step through it looking for a keyword. If you're getting an exception due to being out of memory, you have a memory leak somewhere, or are trying to read through the whole file at once.
For any operation, you can typically be efficient with time, memory, or I/O - usually you get to pick two of those three. If you don't have enough memory to do it quickly, you will take longer to do it a different way.