zoukankan      html  css  js  c++  java
  • Android中使用java.util.Properties犯的错

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

    错误1

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

    出错代码:

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

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

    出错原因:对于这样创建的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文档!

     1 private final String CONFIG_KEY = "CONFIG_KEY";
     2 private final String CONFIG_FILE = "config.properties";
     3 private String mConfigValue;
     4 
     5 //读取配置参数值mConfigValue,启动应用的时候调用
     6 private void configInit(){
     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不会自己创建不存在的文件,会抛出FileNotFoundException异常
    14         properties.load(getApplicationContext().openFileInput(CONFIG_FILE));
    15         mConfigValue = (String)properties.get(CONFIG_KEY);
    16     } catch (Exception e) {
    17         e.printStackTrace();
    18         // TODO: handle exception
    19     }
    20 }
    21 
    22 //配置参数值mConfigValue被修改,保存到配置文件
    23 private void saveConfig(){
    24     try {
    25         Properties properties = new Properties();
    26         if(mConfigValue != null){
    27             properties.setProperty(CONFIG_KEY, mConfigValue);
    28         }
    29         //当CONFIG_FILE文件不存在的时候,openFileOutput则会新建此文件
    30         //这里需要了解下openFileOutput的第二个参数mode:
    31         //http://www.android-doc.com/reference/android/content/Context.html#MODE_PRIVATE
    32         properties.store(getApplicationContext().openFileOutput(CONFIG_FILE, MODE_PRIVATE),null);
    33     } catch (Exception e) {
    34         // TODO: handle exception
    35         e.printStackTrace();
    36     }
    37 }

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

  • 相关阅读:
    类加载器
    java类的主动使用方式
    Java类装载器和命名空间
    cas改造
    所有jar文件列出工具
    源码阅读工具_解析所有类的使用情况
    web(s,哈哈,w,没有,d,用处,f)web框架引用类的次数
    struts2类型转换器StrutsTypeConverter的使用
    struts2的验证框架的使用和扩展
    Python3连接MySQL
  • 原文地址:https://www.cnblogs.com/willhua/p/5313864.html
Copyright © 2011-2022 走看看