Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 中使用 java.util.Properties 常見錯誤與解法

編輯:關於 Android 開發

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)

出錯程式碼:

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

這段程式碼用於在應用首次啟動時建立用來保存配置資訊的檔案。出錯原因:這樣建立的 config.properties 會放在應用的套件目錄下,對於這種檔案,最好的做法是用絕對路徑來建立 File。

修改方式:

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)

出錯程式碼:

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

這真是「豬一樣的錯誤」:因為前面那個錯誤,我把這裡也改成了絕對路徑,但 API 文件中寫得清清楚楚——傳入的參數是「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 文件中清楚寫著「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();
    }
}

// 配置參數值 mConfigValue 被修改後,保存到配置檔
private void saveConfig() {
    try {
        Properties properties = new Properties();
        if (mConfigValue != null) {
            properties.setProperty(CONFIG_KEY, mConfigValue);
        }
        // 當 CONFIG_FILE 不存在時,openFileOutput 會新建此檔案
        // 這裡需了解 openFileOutput 的第二個參數 mode(如 MODE_PRIVATE)
        properties.store(getApplicationContext().openFileOutput(CONFIG_FILE, MODE_PRIVATE), null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

最後,關於 Properties 推薦一個不錯的應用教學:Java Properties file examples(https://www.mkyong.com/java/java-properties-file-examples/)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved