寫這篇文章之前可以成功運行,文章后就各種找不到文件.所以並沒有采用此種方式,后期完善.詳見下篇解決方案.
配置文件讀取很容易,修改需要注意權限,比如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 = new Properties(); 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";</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span><span style="color: #000000"> Properties getProperties(Context context) { Log.e(</span>"configPath"<span style="color: #000000">, configPath); Properties urlProps; Properties props </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> Properties(); </span><span style="color: #0000ff">try</span><span style="color: #000000"> { </span><span style="color: #008000">//</span><span style="color: #008000">方法一:通過activity中的context攻取setting.properties的FileInputStream </span><span style="color: #008000">//</span><span style="color: #008000">注意這地方的參數appConfig在eclipse中應該是appConfig.properties才對,但在studio中不用寫后綴 </span><span style="color: #008000">//</span><span style="color: #008000">InputStream in = c.getAssets().open("appConfig.properties"); </span><span style="color: #008000">//</span><span style="color: #008000">props.load(context.getAssets().open(configName)); </span><span style="color: #008000">//</span><span style="color: #008000">方法二:通過class獲取setting.properties的FileInputStream </span><span style="color: #008000">//</span><span style="color: #008000">InputStream in = PropertiesUtill.class.getResourceAsStream("/assets/ setting.properties ")); </span><span style="color: #008000">//</span><span style="color: #008000"> 方法三</span>props.load(context.openFileInput(configPath));
// props.load(new FileInputStream(configPath));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}urlProps </span>=<span style="color: #000000"> props; </span><span style="color: #0000ff">return</span><span style="color: #000000"> urlProps; } </span><span style="color: #008000">//</span><span style="color: #008000">保存配置文件</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span><span style="color: #000000"> String setProperties(Context context, String keyName, String keyValue) { Properties props </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> Properties(); </span><span style="color: #0000ff">try</span><span style="color: #000000"> { props.load(context.openFileInput(configPath)); props.setProperty(keyName, keyValue); </span><span style="color: #008000">//</span><span style="color: #008000"> FileOutputStream out = context.getAssets().openFd(configPath).createOutputStream();</span> FileOutputStream out =<span style="color: #000000"> context.openFileOutput(configPath,Context.MODE_PRIVATE); </span><span style="color: #008000">//</span><span style="color: #008000"> FileOutputStream out = new FileOutputStream(configPath);</span> props.store(out, <span style="color: #0000ff">null</span><span style="color: #000000">); } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (Exception e) { e.printStackTrace(); Log.e(</span>"setPropertiesError"<span style="color: #000000">, e.toString()); </span><span style="color: #0000ff">return</span> "修改配置文件失敗!"<span style="color: #000000">; } </span><span style="color: #0000ff">return</span> "設置成功"<span style="color: #000000">; }}
UrlString類:
package com.lemon.demo.json;import android.content.Context;
import com.lemon.demo.utils.ProperTies;import java.util.Properties;
/**
- 讀寫配置屬性類
*/public class UrlString {
<span style="color: #0000ff">private</span> String contrastIPName = "contrastIP"<span style="color: #000000">; </span><span style="color: #008000">//</span><span style="color: #008000"> 上傳路徑</span> <span style="color: #0000ff">private</span><span style="color: #000000"> String ip; </span><span style="color: #0000ff">private</span><span style="color: #000000"> String ipAddress; </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setIPAddress(Context context) { Properties proper </span>=<span style="color: #000000"> ProperTies.getProperties(context); </span><span style="color: #0000ff">this</span>.ip = proper.getProperty(contrastIPName, ""<span style="color: #000000">); </span><span style="color: #0000ff">this</span>.ipAddress = "http://" + <span style="color: #0000ff">this</span>.ip + "/index.html"<span style="color: #000000">; } </span><span style="color: #0000ff">public</span><span style="color: #000000"> String setIPAddress(Context context, String keyValue) { String result </span>=<span style="color: #000000"> ProperTies.setProperties(context, contrastIPName, keyValue); </span><span style="color: #0000ff">this</span>.ip =<span style="color: #000000"> keyValue; </span><span style="color: #0000ff">this</span>.ipAddress = "http://" + <span style="color: #0000ff">this</span>.ip + "/index.html"<span style="color: #000000">; </span><span style="color: #0000ff">return</span><span style="color: #000000"> result; } </span><span style="color: #0000ff">public</span><span style="color: #000000"> String getIP() { </span><span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span><span style="color: #000000">.ip; } </span><span style="color: #0000ff">public</span><span style="color: #000000"> String getIPAddress() { </span><span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span><span style="color: #000000">.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);
