-
- /**
- *
- * @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;
- }
- }
-
- }