(Original) Share several tools written by myself (11) Operation tools for configuration files

The previous article shared the tools for recording local logs

https://blog.csdn.net/Android_xiong_st/article/details/102776170
This time I will introduce some operations of the configuration file

Sometimes the app will have some random operations

For example, the probability of the appearance of items in the game, etc.

And if you let it appear permanently

You can use the configuration file

Not much to say, just go to the code

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


}

It is also very simple to use

The first is initialization, to create a configuration file

Models available in Application

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
 + "/test", "test.cfg");
ConfigUtil.getInstance(file).createConfigFile();

Next is the specific use

Delete Files

ConfigUtil.getInstance(file).delConfigFile();

Modify and write parameters

ConfigUtil.getInstance(file).writeConfig(this, "key", "value");

Read parameters

ConfigUtil.getInstance(file).readConfig("key");

 

Guess you like

Origin blog.csdn.net/Android_xiong_st/article/details/103366586