示例:
assets/payconfig.txt
#檔案内容:
ShowAlipay=true
ShowWeChat=true
讀取配置:
private void Example()
{
AssetProperty config = new AssetProperty(context, "payconfig.txt");
String ShowAlipay = config.getConfig("ShowAlipay", ""); // 讀取配置資訊
// 備注:直接在檔案目錄下添加檔案,getAssets().open()可能會報錯: java.io.FileNotFoundException: payconfig.txt
// 在assets中添加配置檔案,需選中assets檔案夾(右鍵)-> New -> File -> 輸入檔案名(payconfig.txt)-> Finish
}
AssetProperty:
package com.joymeng.payment.util;
import java.io.InputStreamReader;
import java.util.Properties;
import android.content.Context;
import android.util.Log;
/** AssetProperty.java: 讀取assets目錄中的配置檔案資訊----- 2018-2-27 下午8:08:10 */
public class AssetProperty
{
// 示例:
private void Example()
{
// assets目錄下的檔案如: assets/payconfig.txt
// #檔案内容:
// ShowAlipay=true
// ShowWeChat=true
AssetProperty config = new AssetProperty(context, "payconfig.txt");
String ShowAlipay = config.getConfig("ShowAlipay", "true"); // 讀取配置資訊
// 備注:直接在檔案目錄下添加檔案,getAssets().open()可能會報錯: java.io.FileNotFoundException: payconfig.txt
// 在assets中添加配置檔案,需選中assets檔案夾(右鍵)-> New -> File -> 輸入檔案名(payconfig.txt)-> Finish
}
// -------------------------------
String filepath = ""; // assets目錄下的檔案如: assets/payconfig.txt
Context context;
Properties prop = null;
/** 建立AssetProperty */
public AssetProperty(Context context, String filepath)
{
this.context = context;
this.filepath = filepath;
if (prop == null) prop = getAssetsProperty(context, filepath);
}
/** 讀取AssetProperty中的配置資訊 */
public String getConfig(String name, String defval)
{
if (prop == null)
return defval;
else return prop.getProperty(name, defval);
}
/** 讀取Assest檔案夾下資源,傳回Properties */
public static Properties getAssetsProperty(Context context, String filepath)
{
try
{
Properties prop = new Properties();
InputStreamReader reader = new InputStreamReader(context.getAssets().open(filepath), "UTF-8");
prop.load(reader); // uses-sdk android:minSdkVersion="9"
reader.close();
return prop;
}
catch (Exception e)
{
Log.e("AssetProperty", e.toString());
}
return null;
}
}
assets配置檔案加密