Android 使用Properties文件保存软件配置信息
来源:互联网 发布:linux 应用 编辑:程序博客网 时间:2024/06/06 19:17
Properties文件保存软件配置信息,类似hashmap保存信息,key和value对应。在网上找了一个封装类,使用起来特别方便
- 封装类ProperUtil
package com.softconfig.Utils;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Properties;/** * Created by ${王sir} on 2017/6/23. * application */public class ProperUtil { private static String FilePath = "/sdcard/.Properties/"; private static String FileName = ".investigation_terminal.properties"; /** * 得到properties配置文件中的所有配置属性 * * @return Properties对象 */ public static String getConfigProperties(String key) { File mfile = new File(FilePath); if (!mfile.exists()) { mfile.mkdirs(); } File mfileName = new File(FilePath+FileName); if (!mfileName.exists()) { return ""; } Properties props = new Properties(); InputStream in = null; try { in = new FileInputStream(FilePath+FileName); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { props.load(in); } catch (IOException e) { e.printStackTrace(); } String value = props.getProperty(key); if (value==null) { value=""; } return value; } /** * 给属性文件添加属性 * * @param value * @author qiulinhe * @createTime 2016年6月7日 下午1:46:53 */ public static void writeDateToLocalFile( String key, String value) { File mfile = new File(FilePath); if (!mfile.exists()) { mfile.mkdirs(); } Properties p = new Properties(); try { InputStream in = new FileInputStream(FilePath+FileName); p.load(in); } catch (IOException e) { e.printStackTrace(); } p.put(key, value); OutputStream fos; try { fos = new FileOutputStream(FilePath+FileName); p.store(fos, null); } catch (Exception e) { throw new RuntimeException(e); } }}
FilePath是文件保存路径。FileName是保存文件的名称
- 运用
将信息保存到文件中
Sting testContent = "测试内容"; ProperUtil.writeDateToLocalFile("TEST", testContent);
获取保存的信息
String testContent = ProperUtil.getConfigProperties("TEST");
用起来特别方便。记得权限别忘记添加
<!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
阅读全文
0 0
- Android 使用Properties文件保存软件配置信息
- Android使用Properties文件保存信息的永久链接
- android:使用Properties保存程序配置
- Android使用Properties保存本地配置
- java 使用properties文件配置动态信息
- android中使用properties文件配置
- Android下使用Properties文件保存程序设置
- Android下使用Properties文件保存程序设置
- Android下使用Properties文件保存程序设置
- Android下使用Properties文件保存程序设置
- ANDROID下使用PROPERTIES文件保存程序设置
- Android Junit中读取properties文件中的配置信息
- 使用INI文件来保存软件设置信息示例
- Android下用Properties保存程序配置
- Android下用Properties保存程序配置
- Android下用Properties保存程序配置
- Android下用Properties保存程序配置
- Android下用Properties保存程序配置
- Android之免清单注册启动Activity
- Python2.7 中文乱码、输入字符串格式 问题
- Javascript中 带名 匿名 箭头函数的重要区别(推荐)
- [境内法规]中国人民银行关于分支行反洗钱工作的指导意见—银发[2005]56号
- sqlserver日期和字符串函数的处理
- Android 使用Properties文件保存软件配置信息
- matlab 中一些对数组或矩阵的处理
- BZOJ 2982 combination Lucas定理
- js正则
- 判断字符串是否相等
- 你所不知道的Python奇技淫巧
- SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 解决方法
- 编译器的工作过程
- hdu 2049 不容易系列之(4)——考新郎(错排+组合数)