I have a file (dictionary.txt) with data field entries as follows –
ABC This represents ...
PQR This represents ...
XYZ This represents ...
...
...
... (hundreds of such entries)
I have a Java program called Searcher.java with the following function
private String[] searchInsideFile(String stringToMatch, String fileName)
This looks for the occurrence of any data field(s) in the file that are contained in the stringToMatch. However, the function, as it stands, opens and closes the file each time and reads all of its hundreds of fields to look for a match.
I am going to have to call this function many times (possibly hundreds), therefore, I don’t think what I am doing is pretty efficient. Is there a good “design pattern” for such a situation? Thanks.
If the file is not too big and will not change, you could read its contents to a string on startup and then just make your searches on top of it