/**     *      * @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文件的操作 * 

* 写入 * 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; } }}

 

更多相关文章

  1. Android中对XML文件的解析
  2. cocos2d-x 移植安卓错误解决
  3. Android(安卓)sdk更新代理配置
  4. android 调用系统相机程序,存放文件夹创建不了(miui 2.3.9系统)
  5. android 用Pull 创建XML
  6. android 下Excel操作
  7. Android下载完文件打开
  8. android 存储操作 大小显示换算 kb mb KB MB 读取
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android获取通话状态
  2. android TextView的跑马灯效果的实现
  3. view measure详解
  4. Android通过App启动另一个APP
  5. android shape 常用到属性的设置
  6. Android 通过继承TextView类自定义字体默
  7. android 自定义对话框 背景透明
  8. android中利用itext读取pdf文档
  9. android intent.setType指定浏览本地多种
  10. Android(安卓)EditText得到和失去焦点时,