今天尝试使用java.util.Properties来保存应用配置,然而遇到了好几个问题,对于熟悉此内容的来说可能都是猪一样的错误,但难免有像我一样的新手再次遇到,希望此文能有所帮助。

错误1

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

出错代码:

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

本代码是用于在应用被初次启用,创建用来保存配置信息的文件。

出错原因:对于这样创建的config.propeties文件是放在应用的包目录下的,对于这样的文件,最好的方法是使用绝对路径来创建file。参考http://stackoverflow.com/questions/16847151/error-while-creating-a-new-file-in-android-filenotfoundexception-test-png-op

修改:

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

先通过getApplicationContext().getFilesDir().toString()获取本应用包目录的绝对路径,然后再创建文件。绝对路径为“/data/data/com.company.App/files/”,com.company.App表示你的应用包名。

错误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)

出错代码:

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

这个真是猪一样的错误,因为有了前面一个错误,所以我也就把这里也改成了绝对路径,但是在API文档中(http://www.android-doc.com/reference/android/content/Context.html#openFileInput(java.lang.String)写的清清楚楚,传入的参数是“The name of the file to open; can not contain path separators.”,只要“config.properties”即文件名就够了!犯这个错误的原因也是因为我看了一些网上的文章就开始写,而没有认真看下API文档,以此为戒,遇到新东西,首先看官方文档。

错误3

java.lang.NullPointerException
at java.util.Hashtable.put(Hashtable.java:365)
at java.util.Properties.setProperty(Properties.java:511)

出错代码:

//代码运行到这里,valueString为null
Properties properties = new Properties();
properties.setProperty(keyString, valueString);

这个也是同上面的错误一样,在API文档(http://www.android-doc.com/reference/java/util/Properties.html#setProperty(java.lang.String, java.lang.String)中清楚的写了:The key and value cannot be null。由于代码在逻辑上没有考虑到此处valueString的依然为null,所以导致此错误,还是那句话:多看API文档!

还有中间还考虑过权限的问题,其实读写自己包私有的文件是不需要申请权限的,包括android.permission.WRITE_EXTERNAL_STORAGE也不需要申请。

最后贴上最终正确的代码,其实还是比较简单的。写此文的目的就是告诉自己:多看API文档!

 private final String CONFIG_KEY = "CONFIG_KEY";
private final String CONFIG_FILE = "config.properties";
private String mConfigValue; //读取配置参数值mConfigValue,启动应用的时候调用
private void configInit(){
try {
File file = new File(getFilesDir() + "/" + CONFIG_FILE);
if(!file.exists()){
file.createNewFile();
}
Properties properties = new Properties();
//openFileInput不会自己创建不存在的文件,会抛出FileNotFoundException异常
properties.load(getApplicationContext().openFileInput(CONFIG_FILE));
mConfigValue = (String)properties.get(CONFIG_KEY);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
} //配置参数值mConfigValue被修改,保存到配置文件
private void saveConfig(){
try {
Properties properties = new Properties();
if(mConfigValue != null){
properties.setProperty(CONFIG_KEY, mConfigValue);
}
//当CONFIG_FILE文件不存在的时候,openFileOutput则会新建此文件
//这里需要了解下openFileOutput的第二个参数mode:
//http://www.android-doc.com/reference/android/content/Context.html#MODE_PRIVATE
properties.store(getApplicationContext().openFileOutput(CONFIG_FILE, MODE_PRIVATE),null);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

最后,关于Properties推荐一个比较好的应用教程Java Properties file examples:http://www.mkyong.com/java/java-properties-file-examples/

Android中使用java.util.Properties犯的错的更多相关文章

  1. java.util.Properties 读取配置文件中的参数

    用法 getProperty方法的返回值是String类型. java.util.Properties 读取配置文件中的参数 //读取配置文件 FileInputStream inStream = n ...

  2. 关于java.util.Properties读取中文乱码的正确解决方案(不要再用native2ascii.exe了)

    从Spring框架流行后,几乎根本不用自己写解析配置文件的代码了,但近日一个基础项目(实在是太基础,不能用硕大繁琐的Spring), 碰到了用java.util.Properties读取中文内容(UT ...

  3. 使用java.util.Properties类读写配置文件

    J2SE 1.5 以前的版本要求直接使用 XML 解析器来装载配置文件并存储设置,虽说也并非难事,相比 java.util.Properties却要做额外的解析工作.而java.util.Proper ...

  4. java.util.Properties类 学习笔记

    学习目标:   1.认识properties文件,理解其含义,会正确创建properties文件. 2.会使用java.util.Properties类来操作properties文件. 3.掌握相对路 ...

  5. 属性集合java.util.Properties

    属性集合java.util.Properties java.util.Properties集合 extends Hashtable<k, v> implements Map<k, v ...

  6. Java.util.properties读取配置文件分析

    Java.util.properties API链接: https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html Clas ...

  7. 【Java笔记】配置文件java.util.Properties类的使用

    配置文件的路径:项目名/src/main/resources/mmall.properties mmall.properties的内容是键值对.例如假设写了ftp服务器的一些信息. ftp.serve ...

  8. 方便好使的java.util.Properties类

    今天偶然碰到这个类,发现jdk中这些平时不大用到的类还挺好玩儿的,用起来也特别实在方便,随便写点记录下. java.util.Properties是对properties这类配置文件的映射.支持key ...

  9. java使用java.util.Properties读取properties文件的九种方法

    直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...

随机推荐

  1. NodeJs 开发微信公众号(四)微信网页授权

    微信的网页授权指的是在微信公众号中访问第三方网页时获取用户地理.个人等信息的权限.对于开发了自己的网页app应用时,获取个人的信息非常重要.上篇博客讲到了注册时可以获取用户的信息,很多人会问为什么还需 ...

  2. Vue.js——60分钟webpack项目模板快速入门

    概述 browserify是一个 CommonJS风格的模块管理和打包工具,上一篇我们简单地介绍了Vue.js官方基于browserify构筑的一套开发模板.webpack提供了和browserify ...

  3. Fedora 22中的日期和时间配置

    Introduction Modern operating systems distinguish between the following two types of clocks: A real- ...

  4. jQuery选择器中,通配符[id^='code']input[id$='code'][id*='code']

     1.选择器 (1)通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='code']&qu ...

  5. vue-router2.0 组件之间传参及获取动态参数

    <li v-for=" el in hotLins" > <router-link :to="{path:'details',query: {id:el ...

  6. 安装wamp2.5报权限错误的解决办法

    安装完wampServer 2.5,新建了一个虚拟目录,xhp/ 访问xhp/index.php报You don't have permission to access 按照以前的经验,只要打开/wa ...

  7. 1、.NET平台概述

        本学习主要参考Andrew Troelsen的C#与.NET4高级程序设计,这小节主要述说以下几个东西:     宏观上讨论一下.net相关的主题:程序集.CIL(Common Interme ...

  8. SVN服务器和客户端安装教程

    SVN是什么?有何用? SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁 ...

  9. 6.C#WinForm基础城市选择器

    源码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  10. 使用C#,轻松发邮件之QQ邮箱

    参考来源 http://www.cnblogs.com/youring2/archive/2008/11/29/1343911.html