• 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. }
  • 相关阅读:
    list转map,set,使用stream进行转化
    使用thymeleaf模板实现博客评论的异步刷新
    python 字典元组集合字符串
    python列表解析补充:
    python列表解析
    springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据
    配置github——每次提交后使contributions有记录(有小绿格子)
    github浏览器无法访问,并且idea无法push项目
    互联网协议入门
    http协议入门---转载
  • 原文地址:https://www.cnblogs.com/jpfss/p/9875633.html
Copyright © 2020-2023  润新知