Error in using java. util. Properties in Android, utilproperties

Source: Internet
Author: User

Error in using java. util. Properties in Android, utilproperties

Try to use java today. util. properties to save the application configuration. However, there are several problems that may be the same errors for beginners who are familiar with this content, but it is inevitable that new users like me will encounter it again, I hope this article will be helpful.

Error 1

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

Error code:

1 File file = new File("config.properties");2 if(!file.exists()){3     file.createNewFile();4 }

This code is used to create a file for saving configuration information when the application is enabled for the first time.

Cause of error: the config. propeties file created in this way is placed in the application package directory. The best way to do this is to use an absolute path to create a file. Reference http://stackoverflow.com/questions/16847151/error-while-creating-a-new-file-in-android-filenotfoundexception-test-png-op,

Modify:

String appDir = getApplicationContext().getFilesDir().toString();File file = new File(appDir + "/" + "config.properties");if(!file.exists()){    file.createNewFile();}

Use getApplicationContext (). getFilesDir (). toString () to obtain the absolute path of the application package directory, and then create a file. The absolute path is "/data/com. company. App/files/", and com. company. App indicates your application package name.

Error 2

Java. lang. IllegalArgumentException: File/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:

Properties properties = new Properties();properties.load(getApplicationContext().openFileInput(appDir + "/" + “config.properties”));

This is really a pig error, because of the previous error, so I also changed this to the absolute path, but in the API documentation (http://www.android-doc.com/reference/android/content/Context.html#openFileInput (java. lang. string) The input parameter is "the name of The file to open; can not contain path separators. ", as long as" config. properties, that is, the file name is enough! The reason for this error is that I started to write some articles on the Internet, but did not carefully read the API documentation. In this case, I first read the official documentation for new things.

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 nullProperties properties = new Properties (); properties. setProperty (keyString, valueString );

This is also The same as The above error, which is clearly written in The API documentation (http://www.android-doc.com/reference/java/util/Properties.html#setProperty (java. lang. String, java. lang. String): The key and value cannot be null. Because the Code does not logically consider that valueString is still null, this error is still caused!

You have also considered permissions. In fact, you do not need to apply for permissions to read and write private files in your own package, including android. permission. WRITE_EXTERNAL_STORAGE.

 

The final correct code is actually relatively simple. The purpose of writing this article is to tell yourself: Read More API documentation!

1 private final String CONFIG_KEY = "CONFIG_KEY"; 2 private final String CONFIG_FILE = "config. properties "; 3 private String mConfigValue; 4 5 // read the configuration parameter value mConfigValue, call 6 private void configInit () when starting the application () {7 try {8 File file = new File (getFilesDir () + "/" + CONFIG_FILE); 9 if (! File. exists () {10 file. createNewFile (); 11} 12 Properties properties = new Properties (); 13 // openFileInput does not create a file that does not exist on its own. It throws a FileNotFoundException exception 14 properties. load (getApplicationContext (). openFileInput (CONFIG_FILE); 15 mConfigValue = (String) properties. get (CONFIG_KEY); 16} catch (Exception e) {17 e. printStackTrace (); 18 // TODO: handle exception19} 20} 21 22 // The Configuration Parameter Value mConfigValue is modified and saved to the configuration file 23 private v Oid saveConfig () {24 try {25 Properties properties = new Properties (); 26 if (mConfigValue! = Null) {27 properties. setProperty (CONFIG_KEY, mConfigValue); 28} 29 // when the CONFIG_FILE file does not exist, openFileOutput creates a new file 30 // you need to know the second parameter mode of openFileOutput: 31. // http://www.android-doc.com/reference/android/content/Context.html#MODE_PRIVATE32 properties. store (getApplicationContext (). openFileOutput (CONFIG_FILE, MODE_PRIVATE), null); 33} catch (Exception e) {34 // TODO: handle exception35 e. printStackTrace (); 36} 37}

Finally, for Properties to recommend a better application tutorial Java Properties file examples: http://www.mkyong.com/java/java-properties-file-examples/

Related Article

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.