您的位置:首页 > 移动开发 > Android开发

Android中使用java.util.Properties犯的错

2016-03-24 00:48 483 查看
今天尝试使用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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: