Android讀寫properties配置檔案
阿新 • • 發佈:2018-11-06
寫這篇文章之前可以成功執行,文章後就各種找不到檔案.所以並沒有採用此種方式,後期完善.詳見下篇解決方案.
配置檔案讀取很容易,修改需要注意許可權,比如assets目錄下就不允許修改.
配置檔案的建立:
New --- File
命名後選擇properties方式開啟
配置檔案設定
contrastIP = 192.166.1.65:8011
assets目錄建立
在main目錄下,與java res 目錄同級建立.
New --- Folder --- Assets Folder
assets目錄詳解: http://blog.csdn.net/chuntiandejiaobu10/article/details/52352128
許可權配置
在 AndroidManifest.xml 中新增:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
其實我去掉後測試也可以成功執行.還是加上.預防萬一.
先讀配置:
方法一 讀取assets目錄下的配置
Properties props = newProperties(); props.load(context.getAssets().open(configName));
將configName檔案從assets目錄下放出來,放在main目錄下:
方法二 會出現錯誤 open failed: ENOENT (No such file or directory) 然而並不知道目錄路徑該如何填
Properties props = new Properties(); props.load(new FileInputStream(configName));
方法三 這樣就能成功執行
Properties props = new Properties(); props.load(context.openFileInput(configName));
修改配置:
Properties props = new Properties();
props.load(context.openFileInput(configPath)); props.setProperty(keyName, keyValue);
// 讀取assets目錄下的,但是根本無法修改
// FileOutputStream out = context.getAssets().openFd(configPath).createOutputStream();
// 提示 open failed: EROFS (Read-only file system)
// FileOutputStream out = new FileOutputStream(configPath);
// 這樣就可以了
FileOutputStream out = context.openFileOutput(configPath,Context.MODE_PRIVATE);
參考Context.MODE_PRIVATE說明: http://www.cnblogs.com/yjpjy/p/5407251.html
完整程式碼
類
ProperTies 類
package com.lemon.demo.utils; import android.content.Context; import android.util.Log; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; public class ProperTies { //private static String configPath = getExternalStorageDirectory() + File.separator + "appConfig"; private static String configPath = "appConfig"; public static Properties getProperties(Context context) { Log.e("configPath", configPath); Properties urlProps; Properties props = new Properties(); try { //方法一:通過activity中的context攻取setting.properties的FileInputStream //注意這地方的引數appConfig在eclipse中應該是appConfig.properties才對,但在studio中不用寫字尾 //InputStream in = c.getAssets().open("appConfig.properties"); //props.load(context.getAssets().open(configName)); //方法二:通過class獲取setting.properties的FileInputStream //InputStream in = PropertiesUtill.class.getResourceAsStream("/assets/ setting.properties ")); // 方法三 props.load(context.openFileInput(configPath)); // props.load(new FileInputStream(configPath)); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } urlProps = props; return urlProps; } //儲存配置檔案 public static String setProperties(Context context, String keyName, String keyValue) { Properties props = new Properties(); try { props.load(context.openFileInput(configPath)); props.setProperty(keyName, keyValue); // FileOutputStream out = context.getAssets().openFd(configPath).createOutputStream(); FileOutputStream out = context.openFileOutput(configPath,Context.MODE_PRIVATE); // FileOutputStream out = new FileOutputStream(configPath); props.store(out, null); } catch (Exception e) { e.printStackTrace(); Log.e("setPropertiesError", e.toString()); return "修改配置檔案失敗!"; } return "設定成功"; } }
UrlString類:
package com.lemon.demo.json; import android.content.Context; import com.lemon.demo.utils.ProperTies; import java.util.Properties; /** * 讀寫配置屬性類 */ public class UrlString { private String contrastIPName = "contrastIP"; // 上傳路徑 private String ip; private String ipAddress; public void setIPAddress(Context context) { Properties proper = ProperTies.getProperties(context); this.ip = proper.getProperty(contrastIPName, ""); this.ipAddress = "http://" + this.ip + "/index.html"; } public String setIPAddress(Context context, String keyValue) { String result = ProperTies.setProperties(context, contrastIPName, keyValue); this.ip = keyValue; this.ipAddress = "http://" + this.ip + "/index.html"; return result; } public String getIP() { return this.ip; } public String getIPAddress() { return this.ipAddress; } }
在activity中使用:
載入配置檔案:
private UrlString urlString = new UrlString(); editText = (EditText) findViewById(R.id.et_ip); // 載入配置的資訊 --- IP地址 urlString.setIPAddress(this); editText.setText(urlString.getIP());
// 獲取完整地址
// urlString.getIPAddress()
修改配置檔案:
String value = editText.getText().toString(); String result = urlString.setIPAddress(this,value); tools.customToast(result, ConfigActivity.this);