Android 对.properties文件的读取
2024-08-30 07:01:53
-
-
/**
-
*
-
* @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;
-
}
-
}
-
-
}
最新文章
- 数对的个数(cogs610)
- Android -- 编辑框更改样式
- 大数相乘nyoj28
- block捕获自动变量和对象
- java矩阵相乘的计算
- linux环境内存分配原理 mallocinfo
- mvc设计模式和mvc框架的区别
- Mssql链接mysql数据库
- HDU 1402 A * B Problem Plus(FFT)
- XMPP登录应用
- UnitOfWork机制的实现和注意事项
- PAT_1026 程序运行时间
- Redis 安装(一)
- E - Coin Game
- RaspberryPi.1.开机与远程桌面
- 安装redis时候的坑
- GitBash: 右键添加 Git Bash Here 菜单
- sublime-text-how-to-jump-to-file-from-find-results-using-keyboard
- PHP之旅8 URL与表单
- oracle和mysql功能相同的函数
热门文章
- 电脑c盘清理
- jmeter--九种定时器介绍(包括思考时间、集合点)
- 驱动学习2-Menuconfig与Kconfig
- Android内存优化(使用SparseArray和ArrayMap取代HashMap)
- Redis学习笔记--Hash(五)
- python3中sum
- 9.4 Binder系统_驱动情景分析_服务使用过程
- RISC-V评估系列
- sqoop 1.4.4-cdh5.1.2快速入门 分类: C_OHTERS 2015-06-06 11:40 208人阅读 评论(0) 收藏
- windows ffmpeg 的安装