I am writing an Android application that fills up a Map on launch, and then when done, saves it to file. However, since this file can take up to 10 seconds to retrieve, I would like to know how to check if the file exists before getting it again, and if it does exist, just load it up into the Map object.
private Map<String, String> idMap;
If there is an even better way of saving this Map, let me know please.
(This is meant to be used as a form of dictionary)
Key: Corona
Value: 172
Key: Beer
Value: 199
Let me know if there is any more information you would need.
You can use the
File.exists()method to check if the file already exists:As for alternatives, if you’re just storing key-value pairs, you might want to consider using
SharedPreferencesto store the values instead of implementing your own solution. It will persist keys and values (of multiple types) to an XML file. It is also thread-safe and fast for reading.