今天尝试使用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. XCodeGhost表明:为了安全,开发工具应该从官方网站下载

    今天的热门话题就是XCode编译器,这个神器在火热的移动互联网浪潮下也被人利用了,据文章分析 (XCode编译器里有鬼 - XCodeGhost样本分析)http://www.huochai.mobi ...

  2. 【腾讯Bugly干货分享】Android性能优化典范——第6季

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/580d91208d80e49771f0a07c 导语 这里是Android性能优 ...

  3. Lesson 21 Mad or not?

    Text Aeroplanes are slowly driving me mad. I live near an airport and passing planes can be heard ni ...

  4. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  5. mongodb全套配置

    1,下载&安装 MongoDB 提供了centos yum安装方式. 参考:http://docs.mongodb.org/manual/tutorial/install-mongodb-on ...

  6. QunInfo群数据库的还原与优化

    一. 背景 这个数据库的数据文件mdf大概有8.5G左右,当还原数据库之后感觉可以做很多性能方面上的调优,合并数据后mdf数据文件大概有6.2G左右,行压缩后mdf数据文件大概有4.8G左右,页压缩后 ...

  7. jquery判断当前浏览器的实现代码

    写了一个判断当前浏览器类型及版本的方法,只在IE 8/11 .谷歌 .360 浏览器(不完全)上测试过,需要用到jquery 核心代码: ;(function($, window, document, ...

  8. Eclipse 安装 SVN 的在线插件

    这是继上次svn 客户端与服务器安装后的如何在Eclipse 环境下在线安装 SVN插件,我的Eclipse版本是4.50 SVN的在线安装 下面为大家提供SVN 的在线安装教程.下面是安装的 详细过 ...

  9. WIN7下查看CPU核心数

    方法一 WIN+R输入cmd,输入wmic,输入cpu get *(注意空格),找到numberofcores和numberoflogicalprocessors,如下图为双核4线程,真核心数是2,使 ...

  10. Is-A,Has-A,Use-A(转载)

    原文地址:http://blog.csdn.net/loveyou128144/article/details/4749576 而Is-A,Has-A,Use-A则是用来描述类与类之间关系的.简单的说 ...