今天尝试使用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. ASP.NET MVC Model元数据(二)

    ASP.NET MVC Model元数据(二) 前言 在上篇中,给大家留个对Model元数据的印象,并没有对Model元数据有过多的讲解,而在本篇中也不会对Model元数据的本身来解释,而是针对于它的 ...

  2. CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection)

    CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection) 2016-08-13 由于CSh ...

  3. SQL Server页类型汇总+疑问

    该文章整理自:http://www.sqlnotes.info/2011/10/31/page-type/ SQL Server中包含多种不同类型的页,来满足数据存储的需求.不管是什么类型的页,它们的 ...

  4. Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include

    问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...

  5. 读取xml数据装配到字典中

    public Dictionary<string, string> GetXml() { Dictionary<string, string> dic = new Dictio ...

  6. 常用js功能函数集合

    1.获取随机时间戳 function uniqueId(){         var a=Math.random,b=parseInt;         return Number(new Date( ...

  7. Java线上应用故障排查之一:高CPU占用

    一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. 以我们最近出现的一个实际故障为例,介绍怎么定位和解决这类问题. 根据top命令,发现PID为28555的Java进程占 ...

  8. Enterprise Solution 管理软件开发框架流程实战

    1 定义模块和功能 执行系统功能(SAUTSF),在系统主文件(System Master File SAMF)模块中增加功能SAMFEM,Employee Master. 给有权限的用户组分配功能S ...

  9. Eclipse中文语言包安装和设置中文Doc

    1.安装中文语言包 Eclipse所有的扩展功能都是以插件的形式添加上去的,安装插件时有多种形式,下面是比较常用的两种: 直接将插件中的文件复制到Eclipse对于的目录中.优点是安装时很方便,缺点是 ...

  10. 计算机程序的思维逻辑 (30) - 剖析StringBuilder

    上节介绍了String,提到如果字符串修改操作比较频繁,应该采用StringBuilder和StringBuffer类,这两个类的方法基本是完全一样的,它们的实现代码也几乎一样,唯一的不同就在于,St ...