android使用properties文件存储数据

有时引入第三平台时使用SharePreference存储数据,数据会失效。 这时可以考虑使用properties文件存储数据,简单方便。

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by Administrator on 2016/7/5.
 */
public class ConfigTool {
    public static final  String configPath="/mnt/sdcard/my_config.properties";
    public static Properties loadConfig(String file) {
        Log.i("lingling", "loadConfig");
        Properties properties = new Properties();
        try {
            File config=new File(file);
            if(config==null||!config.exists()){
                Log.i("lingling", "loadConfig FileInputStream is null");
                saveMyConfig(file,properties);
                loadConfig(file);
            }
            FileInputStream s = new FileInputStream(file);
            properties.load(s);
            Log.i("lingling", "properties.load(s)");
            Log.i("lingling",properties.toString());
        }catch (IOException e) {
            Log.i("lingling", e.getMessage());
            e.printStackTrace();
        }

        return properties;
    }

    public static void deleteConfig(String file) {

        Properties properties = new Properties();

            File config=new File(file);
            if(config!=null){
                config.delete();
            }
    }

    public static void saveMyConfig( String file, Properties properties) {
        Log.i("lingling", "saveMyConfig");
        try {
            FileOutputStream s = new FileOutputStream(file, false);
            properties.store(s, "save");
            Log.i("lingling", "properties.store");
        } catch (IOException  e){
            Log.i("lingling", e.getMessage());
            e.printStackTrace();
        }
    }

    public  static void saveConfig(String key, Object value) {
        Properties properties=ConfigTool.loadConfig(ConfigTool.configPath);
        properties.put(key,value);
        ConfigTool.saveMyConfig(ConfigTool.configPath,properties);
    }
}

android使用properties文件存储数据_第1张图片
源码已上传至github
https://github.com/thatluck/PreferenceApplication
参考资料:
Android中读取properties文件
http://www.2cto.com/kf/201210/164308.html

你可能感兴趣的:(android,数据存储)