简体   繁体   中英

What is a good "design pattern" if you want to constantly access a file?

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 possible, you should pre-load the entire contents of the file in-memory and index it using some data structure defined as an attribute, possibly a Map keyed with the strings to match. Then the method searchInsideFile should look inside the data structure and avoid loading the file altogether.

The fastest solution is to read the file once and keep it in memory. But this is only good if it's not a big file. If the file is too big or can get too big in the future you have to read it each time from disk, because you have to search the whole file. Random access does not really help you in this case.

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

I would use a ConstantDataManager pattern. The basic idea is that when you start the program there will be an overhead as the object extracts all the information from the file you are using into it as a Vector or however (Map etc..) you want to store it.

Then you can do a binary search (assuming your dictionary stores words in order) on the store of data, you may also want to have a save method in the object to update any contents to file if you so wanted.

a good book to have a look at is "Software Architecture Design Patterns in Java - Partha Kuchana" Here is a link to the relevant chapter in the book, although you have to pay to see it for 72 hours or buy it from them. You can probably get it from any library or some other source... http://www.cr.netbase.com/doi/pdf/10.1201/9780203496213.ch7

Also have you thought about using an MYSQL database, which might make this a bit quicker if your dictionary has a lot of entries?

Hope this helps, -Ben

Perhaps have the file in the Searcher class as an instance variable, then make a separate function that opens the file. Then change the searchInsideFile() function to access the file that was already opened before. Remember to close the file afterwards!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM