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 ...
随机推荐
- Qt Creator 源码学习 03:qtcreator.pro
当我们准备好 Qt Creator 的源代码之后,首先进入到它的目录,来看一下它的源代码目录有什么奥秘. 这里一共有 9 个文件夹和 9 个文件.我们来一一看看它们都是干什么用的. .git: 版本控 ...
- C#基础数据类型与字节数组(内存中的数据格式)相互转换(BitConverter 类)
在某种通讯协议中(如 Modbus),可能需要把一些基本的数据类型内存中的表示形式转换成以字节数组的形式,方便传送.C/C++中可以利用指针等操作完成,但C#中没有指针,咋办呢?可以用BitCon ...
- php课程 8-32 如何使用gd库进行图片裁剪和缩放
php课程 8-32 如何使用gd库进行图片裁剪和缩放 一.总结 一句话总结:图片缩放到图片裁剪就是改变原图截取的位置以及截取的宽高. 1.电商网站那么多的图片,如果全部加载卡得慢的很,所以他们是怎么 ...
- Android Studio查看android源码
Android Studio的默认版本在/Applications/Android Studio.app/Contents/info.plist中设置,默认没有1.8.如下: <key>J ...
- 【Codeforces Round #437 (Div. 2) C】 Ordering Pizza
[链接]h在这里写链接 [题意] 给你参赛者的数量以及一个整数S表示每块披萨的片数. 每个参数者有3个参数,si,ai,bi; 表示第i个参赛者它要吃的披萨的片数,以及吃一片第 ...
- word中公式的排版及标题列表
1.首先建好你的标题,如标题1,标题2等等,你能够依次改变它们的字体,段落等格式,新建格式例如以下图所看到的 红圈处即建立新的格式,你能够建立不论什么你想要的格式,非常方便: 2.当你建立好了多个标题 ...
- POJ 1775 Sum of Factorials (ZOJ 2358)
http://poj.org/problem?id=1775 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1334 题目大意: ...
- cat /proc/cpuinfo 引发的思考--CPU 物理封装-物理核心-逻辑核心-超线程之间关系
CPU的物理封装,一个物理封装使用独立的一个CPU物理插槽,共享电源和风扇: CPU物理核心:在一个物理封装中封装了多个独立CPU核心,每一个CPU核心都有自己独立的完整硬件单元. CPU逻辑核心:一 ...
- php实现求二进制中1的个数(右移、&、int32位)(n = n & (n - 1);)
php实现求二进制中1的个数(右移.&.int32位)(n = n & (n - 1);) 一.总结 1.PHP中的位运算符和java和c++一样 2.位移运算符看箭头方向,箭头向左就 ...
- Linux下使用Python的Tkinter库出现的No module named _tkinter问题
这是由于python的版本没有包含tkinter的模块,只需要把tk的package安装就可以了. 一般在linux才出现,windows版本一般已经包含了tkinter模块.