The FileCacher is a simple library for Android to cache your object (any object type, e.g. int
, String
, ArrayList<YourClass>
, HashMap<int, ArrayList<YourClass>>
, etc) to store in a file in the internal storage. It is a context based cache and the file creation mode is MODE_PRIVATE. That is, you can read cache within an app that you write. It won't exponse your cache to other apps to used.
You can however read and write the same file in different Activities. Because of this, you have to be careful about the file name. The same file name that have different object type can cause your app not responsed (ANR). To void ANR, make sure you catch ClassCastException
during the reading cache, e.g readCache()
method. See the full code of readCache()
below.
You can watch a video tutorial of FileCacher at https://www.youtube.com/watch?v=WX4yYQmJKBY
- Download FileCacher.jar
- Copy it and paste into your Android project at App > libs > right click on the jar file and choose Add as Library
You can simply cache an object and read it back in 3 simple steps.
Because you can cache any object type, it is ideally important to specify a type parameter when creating an instance. In the example below, it can take a String
object. However, it is not limit to String, you can store any other object type, e.g. ArrayList<YouClass>
or HashMap<int, ArrayList<YourClass>>
.
FileCacher<String> stringCacher = new FileCacher<>(MainActivity.this, "sometext.txt");
That's it. You are ready to writeCache()
in the second line. It should ask you catch an IOException
.
try {
stringCacher.writeCache("Just a sample text while you can cache more up to your internal storage.");
} catch (IOException e) {
e.printStackTrace();
}
After you write, now it's time to read it. However, it is neccesary to check wether you have already written the cache or not. To check it, you can use hasCache()
method. The readCache()
ask you catch an IOException
. Because you may accidently use the same file name in different Activities and write different object type, you should additionally catch ClassCastException
.
if(stringCacher.hasCache()){
try {
stringCacher.readCache();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassCastException e){
e.printStackTrace();
}
}
Method appendOrWrite()
to append an object if file exists, or create a new file and write an object to it.
try {
stringCacher.appendOrWrite("add more text.");
} catch (IOException e) {
e.printStackTrace();
}
try {
List<String> list = stringCacher.getAllCaches();
for(String text : list){
//do something with text
}
} catch (IOException e) {
e.printStackTrace();
}
try {
stringCacher.clearCache();
} catch (IOException e) {
e.printStackTrace();
}
if(stringCacher.hasCache()){
//read cache
}
Log.d("MainActivity", stringCacher.getFilePath());
Method getSize()
returns long integer of a file size. If returns -1, it means the file does not exist.
Log.d("MainActivity", "" + stringCacher.getSize());
This is an additional file to manage all caches. It can delete all caches, retrieve the cache file names, and check if there is a cache.
Below is a snippet code to retrieve cache file names.
FileCacheManager manager = new FileCacheManager(MainActivity.this);
manager.getPath();
if(manager.hasCacheFiles()){
String[] files = manager.getCacheFileNames();
for(String f : files){
Log.d("MainActivity", f);
}
}
Method deleteAllCaches()
You can use this method to delete all caches within your app. It won't affect other app caches. However, once you delete it, it's gone.
manager.deleteAllCaches();
- Get more free source code at https://github.com/kosalgeek
- Watch my video tutorials at my YouTube channel https://youtube.com/user/oumsaokosal
- Like my Facebook Page at https://facebook.com/kosalgeek
- Follow me on Twitter https://twitter.com/okosal
- Get more tutorials at http://www.kosalgeek.com and http://www.top12review.com
If you think this library have saved your life, please support me by donating a few bucks, just for my coffee :)
(The MIT License) Copyright (c) 2016 KosalGeek. (kosalgeek at gmail dot com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.