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< ...
随机推荐
- ios 常用宏(copy)
分享一下我现在用的 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...
- BestCoder Round #14
Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- DB Query Analyzer 5.04 is released, 63 articles concerned have been published
DB Query Analyzer 5.04 is released, 63 articles concerned have been published DB QueryAnalyzer is pr ...
- iOS8 用AVAudioPlayer播放音乐(Swift)
AVAudioPlayer 类提供了播放音频文件的功能,在本次教程中,我们将对一个音乐文件进行播放暂停和停止操作,此外还会显示标题和播放时间.本次教程使用iOS8和Xcod6.3.1 打开Xcode创 ...
- DevExpress中ChartControl柱状图(Bar)用法
我的数据 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 chartControl1.Series.Clear(); ...
- Java面试题之jsp相关
一.jsp有哪些内置对象?作用分别是什么? 分别有什么方法? 本帖隐藏的内容 答:JSP共有以下9个内置的对象: request 用户端请求,此请求会包含来自GET/POST请求的参数 respons ...
- JQuery一句话实现全选/反选
$("#checkAll").click(function () { if (this.checked) { $("input[name='checkbox']& ...
- 使用prototype扩展的JavaScript常用函数库
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- WindowsService服务程序开发
Windows服务:Microsoft Windows 服务(即,以前的 NT服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可 ...
- CSS的“层叠”规则的总结
当你随机打开一个页面,查看源代码,你会发现,同一个元素,不止有一个CSS选择器及对应的样式.而一个元素只能应用一个样式,那么一堆样式中究竟是应用哪一个呢?这就涉及到CSS的层叠规则了.下面就来总结下C ...