My first thought was to use the SharedPreferences class. My brain picked up this idea while using the SharedPreferences in another project. The SharedPreferences is used to read/write preferences for an Android application. The preferences are stored in an xml file that is put in the private directory structure of an application. You could edit the file by pulling it from the device or emulator using
adb pull
. After editing it you push it back using the adb push
command. If you are using the Android Eclipse plugin you could use the file browser to do the same trick. But this is cumbersome to pull/edit/push the file and I do not really like to use XML for storing properties.But how do we do it in microlog? The properties are stored in a properties file that is put in the JAR or by setting properties in the JAD file. Both are easily edited in Eclipse without no need to pull/push the file. The same goes for NetBeans. The properties file is bundled with the jar. Why not do the same on Android? Time to Google again! I found out that there was primarily two ways of doing this:
- Using the AssetManager
- Reading a raw resource
/assets
directory while the other was put in the /res/raw
directory. The first thing that happened was that Eclipse complained about the naming of micrologV2.properties
that was put in the /res/raw
directory. It was kind enough to inform me that only [0..9][a..z] was allowed. Changing the capital V to a lower case v was simple. Now time to do some coding. The code for reading using the AssetManager looked like this:Resources resources = this.getResources(); AssetManager assetManager = resources.getAssets(); // Read from the /assets directory try { InputStream inputStream = assetManager.open("microlog.properties"); Properties properties = new Properties(); properties.load(inputStream); System.out.println("The properties are now loaded"); System.out.println("properties: " + properties); } catch (IOException e) { System.err.println("Failed to open microlog property file"); e.printStackTrace(); }
The code is dead simple and thank God for the Properties class, although microproperties would do the trick. The second way to do it is as simple as the first approach. The code looks like this:
// Read from the /res/raw directory try { InputStream rawResource = resources.openRawResource(R.raw.micrologv2); Properties properties = new Properties(); properties.load(rawResource); System.out.println("The properties are now loaded"); System.out.println("properties: " + properties); } catch (NotFoundException e) { System.err.println("Did not find raw resource: "+e); } catch (IOException e) { System.err.println("Failed to open microlog property file"); }
Notice that I omitted the code for getting the resources, since this was part of the first example. Both ways seems to be good. But what to choose? Using approach 1) has the following advantages from a microlog4android perspective:
- You are not limited to name your file with lower case letters only.
- It is possible to use a default file name if the user does not specify one.
10 comments:
I found this while Googling for java Properties and Android. Very useful, thank you.
Glad that you liked it and found it useful.
Helped me a lot, thanks. But is there a possibility to change values in the property file. I can not get an output stream ...
I have not tested to write to a property file, but I guess that it is not possible to do. If you need to write data to a file, you could store a file in the application private directory. Or you could use the preference classes to store preferences.
I am still not sure where to drop the properties file. Tried keeping it under /src and /assets. But looks like neither of the files are picked up.(to test, i configured FileAppender but found the following in logCat
11-11 20:46:32.804: WARN/Microlog.Logger(269): Warning! No appender is set, using LogCatAppender with PatternFormatter
Hi, many thanks for this, simple, effective and very useful. Saved me a lot of time.
Thanks! Glad that you liked my article.
Kiran; have you tried the /raw directory? Have you checked that the properties file is really included in the build? What is the name of the file?
Useful post. Thanks!
Thanks! Nice that you appreciate the article.
Post a Comment