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 ...
随机推荐
- Windows下安装Resin及配置具体解释与公布应用
关于Resin的优点,网上介绍了一大堆.小编经不住诱惑,决定试用一下. 眼下Resin的最新版本号为:4.0.40.能够从官网直接下载. 1. 将下载下来的Resin包解压开,会看到一 ...
- QWaitCondition 的正确使用方法(通过 mutex 把有严格时序要求的代码保护起来,同时把 wakeAll() 也用同一个 mutex 保护起来)
简单用法 QWaitCondition 用于多线程的同步,一个线程调用QWaitCondition::wait() 阻塞等待,直到另一个线程调用QWaitCondition::wake() 唤醒才继续 ...
- IOS蓝牙项目总结
常见的蓝牙标准有2.0和4.0. 特点 2.0 1.适用于数据量比较大得传输,比如音乐.语音2.IOS开发中,要求设备是经过MFI认证 4.0 1.适用于实时性比较高的数据传输,比如遥控类的鼠标. ...
- 利用Eclipse+openJTAG调试led.axf文件
转自calvinlee1984 Subject:利用Eclipse+openJTAG调试led.axf文件 Date: 3-Mar-2011 By: Calvinlee1984 ...
- Spring5源码深度解析(一)之理解Configuration注解
代码地址:https://github.com/showkawa/spring-annotation/tree/master/src/main/java/com/brian 1.Spring体系结构 ...
- 【UIL框架】Universal-Image-Loader全然解析(一)之介绍与使用具体解释
转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/50439814 本文出自:[江清清的博客] (一).前言: [好消息] ...
- sql海量数据优化
1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null 可以在num上设 ...
- spring mvc controller间跳转 重定向 传参(转)
spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...
- [CSS] Control Image Aspect Ratio Using CSS
Resize images and videos to fill their parent and maintain their aspect ratio with pure CSS. The new ...
- rabbitmq.config详细配置参数
原文:rabbitmq.config详细配置参数 rabbitmq.config详细配置参数 详细使用方法请点击:http://blog.csdn.net/Super_RD/article/detai ...