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. }

Android 对.properties文件的读取的更多相关文章

  1. Android 对 properties文件的读写操作

    -. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...

  2. 关于properties文件的读取(Java/spring/springmvc/springboot)

    一.Java读取properties文件 1.基于ClassLoder读取配置文件 注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便. Properties prope ...

  3. Android local.properties 文件读取

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6202369.html 本文出自[赵彦军的博客] 在Android Studio项目里面有个local.pro ...

  4. Java Bean 获取properties文件的读取

    实际的开发过程中,将一些配置属性从java代码中提取到properties文件中是个很好的选择,降低了代码的耦合度.下面介绍两种通过spring读取properties文件的方法,以ip地址配置为例. ...

  5. properties文件的读取

    Demo //声明资源器类 Properties pro=new Properties(); //获取路径 URL url= PropertiesTest.class.getClassLoader() ...

  6. android从资源文件中读取文件流显示

    在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样:代码区: private void doRaw(){ InputStream is = this ...

  7. Properties文件工具读取类

    import java.io.IOException;import java.io.InputStream;import java.util.Properties; public class Comm ...

  8. android从asset文件夹读取文件

    1)将一个txt文本(msg.txt)复制到开发目录的asset文件夹下. 2)用getAssets().open()可以得到一个输入流.注意getAssets方法必须用在Activity下边.如果不 ...

  9. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

随机推荐

  1. 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers

    [链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...

  2. FTP、WEB虚拟目录作用

    随风原文FTP.WEB虚拟目录作用 在 IIS中,双击您要为之添加虚拟目录的服务以显示其属性表.    单击“目录”选项卡.    单击“添加”.    单击“浏览”从“目录”框中选择一个目录.    ...

  3. OC的DES加密,使与java的Cipher类用DES/CBC/PKCS5Padding方式的加密结果同样

    问题说明: 近期用到DES加密,而且要与java的Cipher类加密的结果保持一致.没研究过java的Cliper,但工作中Cipher依据DES/CBC/PKCS5Padding加密方式生成了一个字 ...

  4. linux查找keyword在php出现的次数

    查找CleverCode在当前文件夹以及子文件夹,全部的php出现大于0的次数. # find -type f -name '*.php' | xargs grep CleverCode ./*.ph ...

  5. js课程 4-12 js中正则表达式如何使用

    js课程 4-12 js中正则表达式如何使用 一.总结 一句话总结: 1.js正则表达式手册取哪里找? w3cschool或者菜鸟教程->找到js正则表达式->完整的RegExp参考手册这 ...

  6. Centos配置java环境

    安装jdk JDK 是开发Java程序必须安装的软件,我们查看一下 yum 源里面的 JDK:yum list java* 选择适合本机的JDK,并安装:yum install java-1.7.0- ...

  7. spring mvc controller间跳转 重定向 传参(转)

    spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...

  8. Linux上制作Window启动盘

    Linux上制作Window启动盘 注意: U盘在Linux中的标签(依具体情况而定:执行df查看) U盘 ----- /dev/sdb4 格式化U盘 建立U盘的启动分区 安装关键工具 ms-sys ...

  9. (二)RabbitMQ消息队列-RabbitMQ消息队列架构与基本概念

    原文:(二)RabbitMQ消息队列-RabbitMQ消息队列架构与基本概念 没错我还是没有讲怎么安装和写一个HelloWord,不过快了,这一章我们先了解下RabbitMQ的基本概念. Rabbit ...

  10. Hadoop入门经典:WordCount 分类: A1_HADOOP 2014-08-20 14:43 2514人阅读 评论(0) 收藏

    以下程序在hadoop1.2.1上测试成功. 本例先将源代码呈现,然后详细说明执行步骤,最后对源代码及执行过程进行分析. 一.源代码 package org.jediael.hadoopdemo.wo ...