android操作ini工具类
package com.smarteye.common; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern; /**
* ini文件工具类
*
* @author xuwanshu
*
*/
public class IniFileTools { /**
* 点节
*
* @author liucf
*
*/
public class Section { private String name; private Map<String, Object> values = new LinkedHashMap<String, Object>(); public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void set(String key, Object value) {
values.put(key, value);
} public Object get(String key) {
return values.get(key);
} public Map<String, Object> getValues() {
return values;
} } /**
* 换行符
*/
private String line_separator = "\n"; /**
* 编码
*/
private String charSet = "UTF-8"; private Map<String, Section> sections = new LinkedHashMap<String, Section>(); /**
* 指定换行符
*
* @param line_separator
*/
public void setLineSeparator(String line_separator) {
this.line_separator = line_separator;
} /**
* 指定编码
*
* @param charSet
*/
public void setCharSet(String charSet) {
this.charSet = charSet;
} /**
* 设置值
*
* @param section
* 节点
* @param key
* 属性名
* @param value
* 属性值
*/
public void set(String section, String key, Object value) {
Section sectionObject = sections.get(section);
if (sectionObject == null)
sectionObject = new Section();
sectionObject.name = section;
sectionObject.set(key, value);
sections.put(section, sectionObject);
} /**
* 获取节点
*
* @param section
* 节点名称
* @return
*/
public Section get(String section) {
return sections.get(section);
} /**
* 获取值
*
* @param section
* 节点名称
* @param key
* 属性名称
* @return
*/
public Object get(String section, String key) {
return get(section, key, null);
} /**
* 获取值
*
* @param section
* 节点名称
* @param key
* 属性名称
* @param defaultValue
* 如果为空返回默认值
* @return
*/
public Object get(String section, String key, String defaultValue) {
Section sectionObject = sections.get(section);
if (sectionObject != null) {
Object value = sectionObject.get(key);
if (value == null || value.toString().trim().equals(""))
return defaultValue;
return value;
}
return null;
} /**
* 删除节点
*
* @param section
* 节点名称
*/
public void remove(String section) {
sections.remove(section);
} /**
* 删除属性
*
* @param section
* 节点名称
* @param key
* 属性名称
*/
public void remove(String section, String key) {
Section sectionObject = sections.get(section);
if (sectionObject != null)
sectionObject.getValues().remove(key);
} /**
* 当前操作的文件对像
*/
private File file = null; public IniFileTools() { } public IniFileTools(File file) {
this.file = file;
initFromFile(file);
} public IniFileTools(InputStream inputStream) {
initFromInputStream(inputStream);
} /**
* 加载一个ini文件
*
* @param file
*/
public void load(File file) {
this.file = file;
initFromFile(file);
} /**
* 加载一个输入流
*
* @param inputStream
*/
public void load(InputStream inputStream) {
initFromInputStream(inputStream);
} /**
* 写到输出流中
*
* @param outputStream
*/
public void save(OutputStream outputStream) {
BufferedWriter bufferedWriter;
try {
bufferedWriter = new BufferedWriter(new OutputStreamWriter(
outputStream, charSet));
saveConfig(bufferedWriter);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} /**
* 保存到文件
*
* @param file
*/
public void save(File file) {
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
file));
saveConfig(bufferedWriter);
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 保存到当前文件
*/
public void save() {
save(this.file);
} /**
* 从输入流初始化IniFile
*
* @param inputStream
*/
private void initFromInputStream(InputStream inputStream) {
BufferedReader bufferedReader;
try {
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream, charSet));
toIniFile(bufferedReader);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} /**
* 从文件初始化IniFile
*
* @param file
*/
private void initFromFile(File file) {
BufferedReader bufferedReader;
try {
bufferedReader = new BufferedReader(new FileReader(file));
toIniFile(bufferedReader);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} /**
* 从BufferedReader 初始化IniFile
*
* @param bufferedReader
*/
private void toIniFile(BufferedReader bufferedReader) {
String strLine;
Section section = null;
Pattern p = Pattern.compile("^\\[.*\\]$");
try {
while ((strLine = bufferedReader.readLine()) != null) {
if (p.matcher((strLine)).matches()) {
strLine = strLine.trim();
section = new Section();
section.name = strLine.substring(1, strLine.length() - 1);
sections.put(section.name, section);
} else {
String[] keyValue = strLine.split("=");
if (keyValue.length == 2) {
section.set(keyValue[0], keyValue[1]);
}
}
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 保存Ini文件
*
* @param bufferedWriter
*/
private void saveConfig(BufferedWriter bufferedWriter) {
try {
boolean line_spe = false;
if (line_separator == null || line_separator.trim().equals(""))
line_spe = true;
for (Section section : sections.values()) {
bufferedWriter.write("[" + section.getName() + "]");
if (line_spe)
bufferedWriter.write(line_separator);
else
bufferedWriter.newLine();
for (Map.Entry<String, Object> entry : section.getValues()
.entrySet()) {
bufferedWriter.write(entry.getKey());
bufferedWriter.write("=");
bufferedWriter.write(entry.getValue().toString());
if (line_spe)
bufferedWriter.write(line_separator);
else
bufferedWriter.newLine();
}
}
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
ini操作类
package com.smarteye.common; import java.io.File; import com.smarteye.adapter.BVPU_ServerParam; public class AddressManage {
public static final String ADDRESS_DIR = MPUPath.MPU_PATH_ROOT
+ "/address.ini"; /**
* 验证是否存在该文件
*
* @return
* @throws Exception
*/
public static boolean isExist() throws Exception {
try {
File f = new File(ADDRESS_DIR);
if (!f.exists()) {
return false;
}
} catch (Exception e) {
// TODO: handle exception
return false;
}
return true;
} public static void createIni(BVPU_ServerParam param) {
IniFileTools file2 = new IniFileTools();
file2.set("address", "ip", param.szServerAddr);
file2.set("address", "port", param.iServerPort);
file2.save(new File(ADDRESS_DIR));
} public static void readIni(BVPU_ServerParam param) {
IniFileTools file2 = new IniFileTools(new File(ADDRESS_DIR));
param.szServerAddr = file2.get("address", "ip").toString();
param.iServerPort = Integer.parseInt(String.valueOf(file2.get(
"address", "port")));
}
}
android操作ini工具类的更多相关文章
- (转载)实例详解Android快速开发工具类总结
实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- java中文件操作的工具类
代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...
- Android 软件管理工具类Utils
Android 软件管理工具类Utils /** * Created by uilubo on 2015/9/30. * 工具类 */ public class Utils { public stat ...
- Java操作Redis工具类
依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...
随机推荐
- Item with the same id "98" already exist
在magento项目中多次遇到这样一个错误: Item (Bluecom_Onefieldusername_Model_Customer) with the same id "98" ...
- python代码中pass的用法
我们有时会在方法中写一些注释代码,用来提示这个方法是干嘛的之类,看下面代码: class Game_object: def __init__(self, name): self.name = name ...
- HDOJ 2689
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- UART串口协议基础1
Louis kaly.liu@163.com 串口协议基础 1 串口概述 串口由收发器组成.发送器是通过TxD引脚发送串行数据,接收器是通过RxD引脚接收串行数据. 发送器和接收器都利用了一个移位寄存 ...
- 得到文件的MD5值
/// <summary> /// 得到文件的MD5值 /// </summary> /// <param name="Path">文件路径&l ...
- 第四课 Grid Control实验 GC OMS安装(第二台机器部署)
2.GC OMS安装(第二台机器部署) 1. 配置图形化 [oracle@ocm2 ~]$ xhost + access control disabled, clients can connect f ...
- Android 支付宝接入时常见的问题
1.概述 首先说明下,Android支付宝接入用的是快捷支付,下载地址是https://b.alipay.com/order/techService.htm 支付宝移动接入地址https://b ...
- VBA基础概念
一:VBA对象 'VBA对象 'VBA中的对象其实就是我们操作的具有方法.属性的excel中支持的对象 'Excel中的几个常用对象表示方法 '1.工作簿 ' Workbooks 代表工作簿集合,所有 ...
- EC读书笔记系列之11:条款20、21
条款20 宁以pass-by-reference-to-const替换pass-by-value 记住: ★尽量以pass-by-reference-to-const替换pass-by-value.前 ...
- group_concat 使用
Mysql中使用group_concat时,出现Row 1 was cut by GROUP_CONCAT()异常. group_concat默认的最大拼接长度,是1024. 把所有子节点的ID,用逗 ...