• Android 对.properties文件的读取


    1. /**
    2. *
    3. * @param filepath .properties文件的位置
    4. */
    5. public void checkFileExists(String filepath){
    6. File file = new File(filepath);
    7. if (file.exists()) {
    8. String s = PropertiesUtil.readValue(filepath, "allTime");
    9. if (s!=null) {
    10. ShowAllTime = Integer.parseInt(s)*60*1000;
    11. }
    12. String mqtt = PropertiesUtil.readValue(filepath, "showTime");
    13. if (mqtt!=null) {
    14. ShowTime = Integer.parseInt(mqtt)*1000;
    15. }
    16. }
    17. }
    1. /**
    2. * 对Properties文件的操作
    3. * <p/>
    4. * 写入
    5. * PropertiesUtil mProp = PropertiesUtil.getInstance(this).init();
    6. * mProp.writeString("name", "Mr Lee");
    7. * mProp.commit();
    8. * 读取EG
    9. * PropertiesUtil mProp = PropertiesUtil.getInstance(this).init();
    10. * mProp.open();
    11. * String name = mProp.readString("name", "");
    12. *
    13. * @author lei
    14. */
    15. public class PropertiesUtil {
    16. private Context mContext;
    17. private String mPath;
    18. private String mFile;
    19. private Properties mProp;
    20. private static PropertiesUtil mPropUtil = null;
    21. public static PropertiesUtil getInstance(Context context) {
    22. if (mPropUtil == null) {
    23. mPropUtil = new PropertiesUtil();
    24. mPropUtil.mContext = context;
    25. mPropUtil.mPath = Environment.getExternalStorageDirectory() + "/ExmKeyValue";
    26. mPropUtil.mFile = "properties.ini";
    27. }
    28. return mPropUtil;
    29. }
    30. public PropertiesUtil setPath(String path) {
    31. mPath = path;
    32. return this;
    33. }
    34. public PropertiesUtil setFile(String file) {
    35. mFile = file;
    36. return this;
    37. }
    38. public PropertiesUtil init() {
    39. try {
    40. File dir = new File(mPath);
    41. if (!dir.exists()) {
    42. dir.mkdirs();
    43. }
    44. File file = new File(dir, mFile);
    45. if (!file.exists()) {
    46. file.createNewFile();
    47. }
    48. InputStream is = new FileInputStream(file);
    49. mProp = new Properties();
    50. mProp.load(is);
    51. is.close();
    52. } catch (Exception e) {
    53. e.printStackTrace();
    54. }
    55. return this;
    56. }
    57. public void commit() {
    58. try {
    59. File file = new File(mPath + "/" + mFile);
    60. OutputStream os = new FileOutputStream(file);
    61. mProp.store(os, "");
    62. os.close();
    63. } catch (Exception e) {
    64. e.printStackTrace();
    65. }
    66. mProp.clear();
    67. }
    68. public void clear() {
    69. mProp.clear();
    70. }
    71. public void open() {
    72. mProp.clear();
    73. try {
    74. File file = new File(mPath + "/" + mFile);
    75. InputStream is = new FileInputStream(file);
    76. mProp = new Properties();
    77. mProp.load(is);
    78. is.close();
    79. } catch (Exception e) {
    80. e.printStackTrace();
    81. }
    82. }
    83. public void writeString(String name, String value) {
    84. mProp.setProperty(name, value);
    85. }
    86. public String readString(String name, String defaultValue) {
    87. return mProp.getProperty(name, defaultValue);
    88. }
    89. public void writeInt(String name, int value) {
    90. mProp.setProperty(name, "" + value);
    91. }
    92. public int readInt(String name, int defaultValue) {
    93. return Integer.parseInt(mProp.getProperty(name, "" + defaultValue));
    94. }
    95. public void writeBoolean(String name, boolean value) {
    96. mProp.setProperty(name, "" + value);
    97. }
    98. public boolean readBoolean(String name, boolean defaultValue) {
    99. return Boolean.parseBoolean(mProp.getProperty(name, "" + defaultValue));
    100. }
    101. public void writeDouble(String name, double value) {
    102. mProp.setProperty(name, "" + value);
    103. }
    104. public double readDouble(String name, double defaultValue) {
    105. return Double.parseDouble(mProp.getProperty(name, "" + defaultValue));
    106. }
    107. /**
    108. * 根据key读取value
    109. *
    110. * @param filePath
    111. * @param key
    112. * @return
    113. */
    114. public static String readValue(String filePath, String key) {
    115. Properties props = new Properties();
    116. try {
    117. InputStream in = new BufferedInputStream(new FileInputStream(
    118. filePath));
    119. props.load(in);
    120. String value = props.getProperty(key);
    121. if (value.equals("")) {
    122. return null;
    123. } else {
    124. return value;
    125. }
    126. } catch (Exception e) {
    127. e.printStackTrace();
    128. return null;
    129. }
    130. }
    131. }
  • 相关阅读:
    2333
    STL string
    后缀自动机的应用
    省选一轮
    等价类计数问题(Polya定理和burnside引理)
    Prufer序列与树的计数(坑)
    分治算法
    生成函数
    莫队算法
    Xamarin 技术解析
  • 原文地址:https://www.cnblogs.com/jpfss/p/9875633.html
Copyright © 2020-2023  润新知