編輯:關於 Android 開發
今天嘗試使用 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/)。
用 NDK 替 Android 彩圖轉灰階加速:Java 與原生層的實戰對比
在 Android 上用純 Java 把彩圖轉成灰階圖,思路和早年 J2ME 的做法幾乎如出一轍:取出每個像素的紅、綠、藍三個通道,算出平均值後再寫回同一個位置。問題在於
Android 源碼解析(五)——Log 日誌類的底層實現全解讀
從 framework 源碼走進 Log 的內部世界 本系列走到第五篇,已經正式進入 Android framework 層的源碼地帶。在動手翻閱之前,先推薦一個非常好用
Apache Cordova 開發 Android 應用與 ADB 驅動排錯
Apache Cordova 開發 Android 應用與 ADB 驅動排錯 使用 Apache Cordova 開發跨平台應用時,Visual Studio 需要具備
Android res 目錄結構詳解:各文件夾用途與資源引用方式
Android res 目錄結構與資源使用完全指南 1. 相關文件夾介紹 在 Android 項目中,主要資源文件放在 res 文件夾裡;assets 文件夾則存放不經編