Android 对.properties文件的读取
-
-
/**
-
*
-
* @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;
-
}
-
}
-
-
}
Android 对.properties文件的读取的更多相关文章
- Android 对 properties文件的读写操作
-. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...
- 关于properties文件的读取(Java/spring/springmvc/springboot)
一.Java读取properties文件 1.基于ClassLoder读取配置文件 注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便. Properties prope ...
- Android local.properties 文件读取
转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6202369.html 本文出自[赵彦军的博客] 在Android Studio项目里面有个local.pro ...
- Java Bean 获取properties文件的读取
实际的开发过程中,将一些配置属性从java代码中提取到properties文件中是个很好的选择,降低了代码的耦合度.下面介绍两种通过spring读取properties文件的方法,以ip地址配置为例. ...
- properties文件的读取
Demo //声明资源器类 Properties pro=new Properties(); //获取路径 URL url= PropertiesTest.class.getClassLoader() ...
- android从资源文件中读取文件流显示
在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样:代码区: private void doRaw(){ InputStream is = this ...
- Properties文件工具读取类
import java.io.IOException;import java.io.InputStream;import java.util.Properties; public class Comm ...
- android从asset文件夹读取文件
1)将一个txt文本(msg.txt)复制到开发目录的asset文件夹下. 2)用getAssets().open()可以得到一个输入流.注意getAssets方法必须用在Activity下边.如果不 ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
随机推荐
- regexp模式匹配+location页面跳转+cookie/localstorage本地存储
学习js的过程中,根据知识点编写一些code进行测试,以便检验. 这段程序使用了以下知识点: 1.regexp,对数据进行模式匹配 2.使用location对象进行页面跳转. 3.cookie/loc ...
- WSDL文档框架
- 最大公约数最小公倍数 (例:HDU2028 Lowest Common Multiple Plus)
也称欧几里得算法 原理: gcd(a,b)=gcd(b,a mod b) 边界条件为 gcd(a,0)=a; 其中mod 为求余 故辗转相除法可简单的表示为: int gcd(int a, int b ...
- (转)30 IMP-00019: row rejected due to ORACLE error 12899
IMP: row rejected due IMP: ORACLE error encountered ORA: value too large , maximum: )导入日志报 IMP: 由于 O ...
- (转)iptables常用规则:屏蔽IP地址、禁用ping、协议设置、NAT与转发、负载平衡、自定义链
转自:http://lesca.me/archives/iptables-examples.html 本文介绍25个常用的iptables用法.如果你对iptables还不甚了解,可以参考上一篇ipt ...
- 【2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B】
[链接]h在这里写链接 [题意] 一个硬币正面朝上的概率为q/p; 抛k次,问你偶数次朝上的概率为多少. [题解] [错的次数] 0 [反思] 在这了写反思 [代码] #include <bit ...
- Kinect开发笔记之三Kinect开发环境配置具体解释
0.前言: 首先说一下我的开发环境,Visual Studio是2013的,系统是win8的64位版本号,SDK是Kinect for windows SDK 1.8版本 ...
- apper
查漏补缺系列之dapper初体验 什么是dapper 在维护一些较老的项目的时候,往往我们会用很多sql那么这个时候我们要考虑优化这些项目的时候,我们就可以使用dapper dapper 是一款轻 ...
- 关于Altium Designer的BOM,元件清单
在生成BOM列表的时候,要记得调整BOM的表格的宽度,以免显示不全, 还有就是BOM列表一共有 comment栏 ,description栏,designator栏,footprint栏,libref ...
- [RxJS] BehaviorSubject: representing a value over time
When an Observer subscribe to a BehaviorSubject. It receivces the last emitted value and then all th ...