(原创)分享自己写的几个工具类(十一)配置文件的操作工具类

上一篇分享了记录本地日志的工具类

https://blog.csdn.net/Android_xiong_st/article/details/102776170
这次来介绍配置文件的一些操作

有时候app会有一些随机操作

比如游戏里的道具出现概率等

而如果让它固定出现

则可以通过配置文件的方式

话不多说,直接上代码

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;
    }


}

使用起来也很简单

首先是初始化,来创建配置文件

可以在Application中机型

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");

猜你喜欢

转载自blog.csdn.net/Android_xiong_st/article/details/103366586