前回の記事では、ローカルログを記録するためのツールを共有しました
https://blog.csdn.net/Android_xiong_st/article/details/102776170
今回は、設定ファイルの操作をいくつか紹介します。
時々アプリはいくつかのランダムな操作をします
たとえば、ゲーム内のアイテムの出現の確率など。
そして、あなたがそれを永久に表示させれば
設定ファイルを使用できます
言うことはあまりありませんが、コードに移動するだけです
public class ConfigUtil {
private static class ConfigUtilHolder {
private static ConfigUtil mConfigUtil = new ConfigUtil();
}
private ConfigUtil() {
}
public static ConfigUtil getInstance(File cfgFile) {
ConfigUtilHolder.mConfigUtil.cfgFile = cfgFile;
return ConfigUtilHolder.mConfigUtil;
}
private static File cfgFile;
public static File getCfgFile() {
return cfgFile;
}
/**
* 创建配置文件
*
* @return
*/
public static boolean createConfigFile() {
if (cfgFile == null) {
Log.d("print", "文件为空");
return false;
}
if (!cfgFile.exists()) {
Log.d("print", "配置文件不存在");
//创建父目录文件夹
cfgFile.getParentFile().mkdirs();
try {
//创建文件
cfgFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//返回文件创建结果
if (!cfgFile.exists()) {
return false;
} else {
return true;
}
}
/**
* 删除配置文件
*/
public static void delConfigFile() {
if (!cfgFile.exists()) {
Log.d("print", "配置文件不存在");
} else {
cfgFile.delete();
}
}
/**
* 读取配置文件参数
*
* @param key
* @return
*/
public static String readConfig(String key) {
Properties props = new Properties();
String portstr = "";
if (cfgFile == null || isNullOrNil(key) || !cfgFile.exists()) {
return portstr;
}
try {
InputStream in = new BufferedInputStream(new FileInputStream(cfgFile));
props.load(in);
portstr = props.getProperty(key);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return portstr;
}
/**
* 修改配置文件参数
*
* @param context
* @param key
* @param value
* @return
*/
public static boolean writeConfig(Context context, String key, String value) {
if (cfgFile == null || isNullOrNil(key) || isNullOrNil(value) || !cfgFile.exists()) {
return false;
}
Properties prop = new Properties();
InputStream fis = null;
OutputStream fos = null;
try {
fis = new FileInputStream(cfgFile);
prop.load(fis);
fos = new FileOutputStream(cfgFile);
prop.setProperty(key, value);
//store方法是用来写头部注释的,第二个参数就是注释内容
prop.store(fos, "write an key/value pair");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
notifySystemToScan(context, cfgFile);
return true;
}
/**
* 扫描sd卡,进行更新
*
* @param context
* @param file
*/
public static void notifySystemToScan(Context context, File file) {
if (context == null || file == null || !file.exists()) {
return;
}
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);
}
/**
* 判断字符串是否为空
*
* @param str
* @return
*/
public static boolean isNullOrNil(String str) {
if (str == null || str.length() == 0) {
return true;
}
return false;
}
}
使い方もとても簡単です
1つ目は、構成ファイルを作成するための初期化です。
アプリケーションで利用可能なモデル
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/test", "test.cfg");
ConfigUtil.getInstance(file).createConfigFile();
次は特定の用途です
ファイルを削除する
ConfigUtil.getInstance(file).delConfigFile();
パラメータの変更と書き込み
ConfigUtil.getInstance(file).writeConfig(this, "key", "value");
パラメータの読み取り
ConfigUtil.getInstance(file).readConfig("key");