Using java.util.Properties to make mistakes in Android

Source: Internet
Author: User

Today try to use Java.util.Properties to save the application configuration, but encountered several problems, for familiarity with this content may be a pig-like error, but there are inevitably like me as a novice again, I hope this article can help.

Error 1

Java.io.IOException:open failed:erofs (read-only file system)
At Java.io.File.createNewFile (file.java:940)

Error code:

1 New File ("Config.properties"); 2 if (! file.exists ()) {3    file.createnewfile (); 4 }

This code is used to create a file to hold configuration information when the app is first enabled.

Cause of error: For such a config.propeties file is placed in the application's package directory, for such a file, the best way is to use absolute path to create file. Reference http://stackoverflow.com/questions/16847151/ Error-while-creating-a-new-file-in-android-filenotfoundexception-test-png-op,

Modify:

String appdir =new File (Appdir + "/" + "config.properties"); if (! file.exists ()) {    file.createnewfile ();}

Use Getapplicationcontext (). Getfilesdir (). ToString () to obtain an absolute path to the application package directory before creating the file. The absolute path is "/data/data/com.company.app/files/" and COM.COMPANY.APP represents your app package name.

Error 2

Java.lang.illegalargumentexception:file/data/data/com.example.basictest2/files/aa.properties contains a path Separator
At Android.app.ContextImpl.makeFilename (contextimpl.java:1805)
At Android.app.ContextImpl.openFileInput (contextimpl.java:767)
At Android.content.ContextWrapper.openFileInput (contextwrapper.java:166)

Error code:

New+ "/" + "config.properties"));

This is really a pig mistake, because there is a previous error, so I also changed this to an absolute path, but in the API document (http://www.android-doc.com/reference/android/content/ Context.html#openfileinput (java.lang.String) writes clearly, the passed parameter is "the name of the file to open; Can not contain path separators. ", as long as" config.properties "is the file name is enough! The reason for this mistake is also because I read some articles on the internet began to write, and did not seriously look at the API document, as a ring, encounter new things, first look at the official documents.

Error 3

Java.lang.NullPointerException
At Java.util.Hashtable.put (hashtable.java:365)
At Java.util.Properties.setProperty (properties.java:511)

Error code:

// The code runs here, valuestring is null New Properties ();p roperties.setproperty (keystring, valuestring);

This is also the same as the above error, in the API documentation (HTTP://WWW.ANDROID-DOC.COM/REFERENCE/JAVA/UTIL/PROPERTIES.HTML#SETPROPERTY ( Java.lang.String, java.lang.String) clearly wrote: The key and value cannot be null. Because the code does not logically consider the valuestring here is still null, so this error is caused, or that sentence: Read more API documentation!

There are also in the middle also considered the issue of permissions, in fact, read and write their own package private files are not required to apply for permission, including Android.permission.WRITE_EXTERNAL_STORAGE do not need to apply.

Finally, the final correct code, in fact, is relatively simple. The purpose of writing this article is to tell yourself: Read more API documentation!

1 Private FinalString Config_key = "Config_key";2 Private FinalString config_file = "Config.properties";3 PrivateString Mconfigvalue;4 5 //Read configuration parameter value Mconfigvalue, call when launching the application6 Private voidConfiginit () {7     Try {8File File =NewFile (Getfilesdir () + "/" +config_file);9         if(!file.exists ()) {Ten file.createnewfile (); One         } AProperties Properties =NewProperties (); -         //Openfileinput does not create a nonexistent file by itself, it throws a FileNotFoundException exception - Properties.load (Getapplicationcontext (). Openfileinput (Config_file)); theMconfigvalue =(String) properties.get (config_key); -}Catch(Exception e) { - e.printstacktrace (); -         //Todo:handle Exception +     } - } +  A //configuration parameter value Mconfigvalue is modified to save to configuration file at Private voidSaveconfig () { -     Try { -Properties Properties =NewProperties (); -         if(Mconfigvalue! =NULL){ - Properties.setproperty (Config_key, mconfigvalue); -         } in         //when the Config_file file does not exist, Openfileoutput will create this file -         //Here you need to know the second parameter of the next Openfileoutput mode: to         //http://www.android-doc.com/reference/android/content/Context.html#MODE_PRIVATE +Properties.store (Getapplicationcontext (). Openfileoutput (Config_file, Mode_private),NULL); -}Catch(Exception e) { the         //Todo:handle Exception * e.printstacktrace (); $     }Panax Notoginseng}

Finally, a good application tutorial on the properties recommended Java Properties file examples: http://www.mkyong.com/java/ java-properties-file-examples/

Using Java.util.Properties in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.