Android 对.properties文件的读取


    /**
     * 
     * @param filepath .properties文件的位置
     */
    public  void checkFileExists(String filepath){
        File file = new File(filepath);
        if (file.exists()) {
            String s = PropertiesUtil.readValue(filepath, "allTime");
            if (s!=null) {
                ShowAllTime = Integer.parseInt(s)*60*1000;
            }
            String mqtt = PropertiesUtil.readValue(filepath, "showTime");
            if (mqtt!=null) {
                ShowTime = Integer.parseInt(mqtt)*1000;
            }
        }
    }

 

/**
 * 对Properties文件的操作
 * <p/>
 * 写入
 * PropertiesUtil mProp = PropertiesUtil.getInstance(this).init();
 * mProp.writeString("name", "Mr Lee");
 * mProp.commit();
 * 读取EG
 * PropertiesUtil mProp = PropertiesUtil.getInstance(this).init();
 * mProp.open();
 * String name = mProp.readString("name", "");
 *
 * @author lei
 */
public class PropertiesUtil {
    private Context mContext;
    private String mPath;
    private String mFile;
    private Properties mProp;
    private static PropertiesUtil mPropUtil = null;

    public static PropertiesUtil getInstance(Context context) {
        if (mPropUtil == null) {
            mPropUtil = new PropertiesUtil();
            mPropUtil.mContext = context;
            mPropUtil.mPath = Environment.getExternalStorageDirectory() + "/ExmKeyValue";
            mPropUtil.mFile = "properties.ini";
        }
        return mPropUtil;
    }

    public PropertiesUtil setPath(String path) {
        mPath = path;
        return this;
    }

    public PropertiesUtil setFile(String file) {
        mFile = file;
        return this;
    }

    public PropertiesUtil init() {
        try {
            File dir = new File(mPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File file = new File(dir, mFile);
            if (!file.exists()) {
                file.createNewFile();
            }
            InputStream is = new FileInputStream(file);
            mProp = new Properties();
            mProp.load(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public void commit() {
        try {
            File file = new File(mPath + "/" + mFile);
            OutputStream os = new FileOutputStream(file);
            mProp.store(os, "");
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        mProp.clear();
    }

    public void clear() {
        mProp.clear();
    }

    public void open() {
        mProp.clear();
        try {
            File file = new File(mPath + "/" + mFile);
            InputStream is = new FileInputStream(file);
            mProp = new Properties();
            mProp.load(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void writeString(String name, String value) {
        mProp.setProperty(name, value);
    }

    public String readString(String name, String defaultValue) {
        return mProp.getProperty(name, defaultValue);
    }

    public void writeInt(String name, int value) {
        mProp.setProperty(name, "" + value);
    }

    public int readInt(String name, int defaultValue) {
        return Integer.parseInt(mProp.getProperty(name, "" + defaultValue));
    }

    public void writeBoolean(String name, boolean value) {
        mProp.setProperty(name, "" + value);
    }

    public boolean readBoolean(String name, boolean defaultValue) {
        return Boolean.parseBoolean(mProp.getProperty(name, "" + defaultValue));
    }

    public void writeDouble(String name, double value) {
        mProp.setProperty(name, "" + value);
    }

    public double readDouble(String name, double defaultValue) {
        return Double.parseDouble(mProp.getProperty(name, "" + defaultValue));
    }
    /**
     * 根据key读取value
     *
     * @param filePath
     * @param key
     * @return
     */
    public static String readValue(String filePath, String key) {
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(
                    filePath));
            props.load(in);
            String value = props.getProperty(key);
            if (value.equals("")) {
                return null;
            } else {
                return value;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值