本示例以properties格式文件为主
如果你自己想要其他格式的文件如txt等等,可以自己修改代码
如果你想要自己的解析格式也可以修改conf代码中的parseLine方法里的解析格式
1.配置文件:bsidinfo.properties
###gsmconf 存储短信发送配置信息,message:发送命令内容,comid=COM2端口号大写,baud=115200为短信设备的波特率,根据需求修改#### [gsmconf] message=SMS:SET+WAKEUP comid=COM3 baud=9600 ###connectioninfo连接流媒体服务器的配置信息,IP、端口号port、用户名username 密码password,根据需求修改## [connectioninfo] port=7660 username=root password=6973hiktb ip=202.91.248.243 ###以下是所有终端的配置信息,[]里代表的是终端的名称不能重复,bsid终端id号,simid手机号,status默认为0## ##这里你可以添加、修改、删除、任何终端,但是请慎重处理 [红满线85号塔_1013] bsid=000001013 simid=15349919673 status=0 [3G-000001143] bsid=000001143 simid=13396716823 status=0
2.代码:
Conf.java
package cn.thunderbird.lm.media.util.conf; import java.io.File; import java.io.FileNotFoundException; import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; /** * 该类主要解析example.propertes文件 * 格式: * #####是注释 * [content] * key=value; * @author luqw */ public class Conf { private static final Logger log = Logger.getLogger(Conf.class); private static final ArrayList String = null; //all the node of properties will be restored.Every object type is ConfEntity class. private ArrayList nodeList = null; private String filePath = null; private Map current_map = null; private ConfEntity current_Entity = null; public Conf() { } /** * 首先导入文件 * @param filePath * @throws Exception */ public void importConf(String filePath) { RandomAccessFile randFile = null; try { this.filePath = filePath; randFile = new RandomAccessFile(filePath, "r"); } catch (FileNotFoundException e) { closeRandomFile(randFile); System.out.println(Constant.E_OprConfigFileError + filePath); } try { loadNodeList(randFile); } catch (Exception e) { e.printStackTrace(); } } /** * */ public void moveBak() { try { File destFile = new File(this.filePath); File bakFile = new File(this.filePath + ".bak"); if (destFile.exists()) { destFile.delete(); } bakFile.renameTo(destFile); } catch (Exception e) { System.out.println("move bak file[" + this.filePath + ".bak] to oraginal file" + this.filePath + "] error:" + e.getMessage()); } } /** * @throws Exception */ public void deleteBak() { try { File bakFile = new File(this.filePath + ".bak"); if (bakFile.exists()) { bakFile.delete(); } } catch (Exception e) { System.out.println("delete bak file[" + this.filePath + ".bak] error:" + e.getMessage()); } } /** * @param rf */ public void closeRandomFile(RandomAccessFile rf) { try { if (rf != null) { rf.close(); } } catch (Exception e) { System.out .println("close randomAccessFile error:" + e.getMessage()); } } /** * export and restore **/ public void exportConf() { RandomAccessFile rf = null; if (nodeList != null) { try { File oraFile = new File(this.filePath); File bakFile = new File(this.filePath + ".bak"); if (bakFile.exists()) { bakFile.delete(); } oraFile.renameTo(bakFile); rf = new RandomAccessFile(this.filePath, "rw"); rf.seek(rf.length()); Iterator it = nodeList.iterator(); boolean isFirstLine = true; while (it.hasNext()) { ConfEntity entity = (ConfEntity) it.next(); Map map = entity.getMap(); String nodeName = "[" + entity.getNodeName() + "]"; if (isFirstLine) { isFirstLine = false; } else { nodeName = "\n" + nodeName; } rf.writeBytes(nodeName); if ((map != null) && (map.size() > 0)) { Iterator tt = map.keySet().iterator(); while (tt.hasNext()) { String key = (String) tt.next(); String value = (String) map.get(key); String line = "\n" + key + "=" + value; rf.writeBytes(line); } } } deleteBak(); // } catch (Exception e) { moveBak(); // System.out.println(Constant.E_OprConfigFileError +this.filePath + e.getMessage()); } finally { closeRandomFile(rf); } } } /** * 载入文件里面的信息 * @param randFile * @throws Exception */ public void loadNodeList(RandomAccessFile randFile) throws Exception { if (randFile != null) { try { String str; nodeList = new ArrayList(); while ((str = randFile.readLine()) != null) { String strLine = str; parseLine(strLine); } if (current_Entity != null) { current_Entity.setMap(current_map); nodeList.add(current_Entity); } } catch (Exception e) { e.printStackTrace(); System.out.println(Constant.E_OprConfigFileError + this.filePath); } finally { closeRandomFile(randFile); } } } /** * 按照prperties文档格式解析文件 * #代表注释 不解析 * []解析 * @param lineStr */ public void parseLine(String lineStr) { if ((lineStr != null) && (lineStr.length() > 0) && !lineStr.startsWith("#")) { if (lineStr.startsWith("[")) { // a new node if (current_Entity != null) { current_Entity.setMap(current_map); nodeList.add(current_Entity); } current_Entity = new ConfEntity(); String nodeName = lineStr.substring(1, lineStr.length() - 1); current_Entity.setNodeName(nodeName); current_map = new HashMap(); } else { if (lineStr.contains("=")) { String key = ""; String value = ""; int indexOfEq = lineStr.indexOf("="); key = lineStr.substring(0, indexOfEq); if ((lineStr.length() - 1) > indexOfEq) { value = lineStr.substring(indexOfEq + 1, lineStr .length()); } current_map.put(key, value); } } } } /** * getValue by node name and property key * @param node * @param key * @return */ public String getValue(String node, String key) { String value = null; if ((nodeList != null) && (nodeList.size() > 0)) { Iterator it = nodeList.iterator(); while (it.hasNext()) { ConfEntity entity = (ConfEntity) it.next(); if (entity.getNodeName().equals(node)) { Map map = entity.getMap(); if (map != null) { value = (String) map.get(key); break; } } } } return value; } /** * * @param nodeName * @throws Exception */ public void addNode(String nodeName) { if (nodeName != null) { if ((nodeList != null) && (nodeList.size() > 0)) { boolean hasNode = false; // Iterator it = nodeList.iterator(); while (it.hasNext()) { ConfEntity entity = (ConfEntity) it.next(); if (entity.getNodeName().equals(nodeName)) { hasNode = true; break; } } if (!hasNode) { ConfEntity entity = new ConfEntity(); entity.setNodeName(nodeName); entity.setMap(null); nodeList.add(entity); } } } } /** * @param nodeName * @param key * @param value * @throws Exception */ public void addPropertie(String nodeName, String key, String value) { if (nodeName != null) { if ((nodeList != null) && (nodeList.size() > 0)) { boolean hasNode = false; Iterator it = nodeList.iterator(); ConfEntity entity = null; while (it.hasNext()) { ConfEntity tempEntity = (ConfEntity) it.next(); if (tempEntity.getNodeName().equals(nodeName)) { hasNode = true; entity = tempEntity; break; } } if (!hasNode) { entity = new ConfEntity(); entity.setNodeName(nodeName); Map map = new HashMap(); map.put(key, value); entity.setMap(map); nodeList.add(entity); } else { Map map = entity.getMap(); if (map == null) { map = new HashMap(); } map.put(key, value); entity.setMap(map); } } } } /** * * @param nodeName * @param map * @throws Exception */ public void addPropertie(String nodeName, Map map) { if ((nodeName != null)) { if ((nodeList != null) && (nodeList.size() > 0)) { boolean hasNode = false; Iterator it = nodeList.iterator(); ConfEntity entity = null; while (it.hasNext()) { ConfEntity tempEntity = (ConfEntity) it.next(); if (tempEntity.getNodeName().equals(nodeName)) { hasNode = true; entity = tempEntity; break; } } if (!hasNode) { System.out.println("!hasNode"); entity = new ConfEntity(); entity.setNodeName(nodeName); entity.setMap(map); nodeList.add(entity); } else { Map oraMap = entity.getMap(); if ((oraMap == null) || (oraMap.size() == 0)) { System.out.println("oraMap == null"); oraMap = map; } else { Iterator mapIt = map.keySet().iterator(); while (mapIt.hasNext()) { String key = (String) mapIt.next(); String value = (String) map.get(key); oraMap.put(key, value); } } } } } } /** * * @param nodeName * @throws Exception */ public void removeNode(String nodeName) { if (nodeName != null) { if ((nodeList != null) && (nodeList.size() > 0)) { boolean hasNode = false; Iterator it = nodeList.iterator(); while (it.hasNext()) { ConfEntity entity = (ConfEntity) it.next(); if (entity.getNodeName().equals(nodeName)) { nodeList.remove(entity); break; } } } } } /** * * @param nodeName * @param key */ public void removeKey(String nodeName, String key) { if ((nodeName != null) && (key != null)) { if ((nodeList != null) && (nodeList.size() > 0)) { Iterator it = nodeList.iterator(); while (it.hasNext()) { ConfEntity entity = (ConfEntity) it.next(); if (entity.getNodeName().equals(nodeName)) { Map map = entity.getMap(); map.remove(key); break; } } } } } /** * @param nodeName * @param key * @param value */ public void setProperties(String nodeName, String key, String value) { if ((nodeName != null) && (key != null) && (key.length() > 0) && (value != null)) { if ((nodeList != null) && (nodeList.size() > 0)) { Iterator it = nodeList.iterator(); while (it.hasNext()) { ConfEntity entity = (ConfEntity) it.next(); if (entity.getNodeName().equals(nodeName)) { ConfEntity confEntity = new ConfEntity(); Map map = entity.getMap(); map.remove(key); map.put(key, value); break; } } } } } /** * * @param nodeName * @param key * @param value */ public List<String> listKey(String nodeName) { ArrayList<String> nodeLists = new ArrayList<String>(); if ((nodeList != null) && (nodeList.size() > 0)) { Iterator it = nodeList.iterator(); while (it.hasNext()) { ConfEntity entity = (ConfEntity) it.next(); if (entity.getNodeName().equals(nodeName)) { Map map = entity.getMap(); if ((map != null) && (map.size() > 0)) { Iterator tt = map.keySet().iterator(); while (tt.hasNext()) { String key = (String) tt.next(); nodeLists.add(key); } } } } } return nodeLists; } public ArrayList getNodeList() { return nodeList; } /** * 测试代码 */ public void printNodeList() { if ((nodeList != null) && (nodeList.size() > 0)) { Iterator it = nodeList.iterator(); while (it.hasNext()) { ConfEntity entity = (ConfEntity) it.next(); System.out.println("---===========node===========---" + entity.getNodeName()); Map map = entity.getMap(); if ((map != null) && (map.size() > 0)) { Iterator tt = map.keySet().iterator(); while (tt.hasNext()) { String key = (String) tt.next(); String value = (String) map.get(key); System.out.println(key + "-------->" + value); } } } } } }
ConfEntity.java
package cn.thunderbird.lm.media.util.conf; import java.util.Map; public class ConfEntity { private String nodeName; private Map map; public String getNodeName() { return nodeName; } public void setNodeName(String nodeName) { this.nodeName = nodeName; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; }
Constant.java
package cn.thunderbird.lm.media.util.conf; public class Constant { public static final String E_OprConfigFileError = "读写配置文件失败"; }
Test.java
package cn.thunderbird.lm.media.dao; import java.util.Iterator; import java.util.Map; import cn.thunderbird.lm.media.util.CharacterUtil; import cn.thunderbird.lm.media.util.conf.Conf; public class Test { private static Conf conf =null;//全局的每次用完清空 private static String filepath=""; public void addNode(String nodeName, Map map){ nodeName=CharacterUtil.utfToIsso(nodeName); conf=new Conf(); conf.importConf(filepath); if ((map != null) && (map.size() > 0)) { Iterator tt = map.keySet().iterator(); while (tt.hasNext()) { String key = (String) tt.next(); String value = (String) map.get(key); conf.addPropertie(nodeName, key, value); } conf.exportConf(); conf=null; } } public void updateNode(String nodeName, Map map){ nodeName=CharacterUtil.utfToIsso(nodeName); conf=new Conf(); conf.importConf(filepath); if ((map != null) && (map.size() > 0)) { Iterator tt = map.keySet().iterator(); while (tt.hasNext()) { String key = (String) tt.next(); String value = (String) map.get(key); conf.setProperties(nodeName, key, value); } } conf.exportConf(); conf=null; } //修改某一个属性 public void updateProperty(String nodeName, String key,String value){ //nodeName=CharacterUtil.utfToIsso(nodeName); conf=new Conf(); conf.importConf(filepath); conf.setProperties(nodeName, key, value); conf.exportConf(); conf=null; } public static void main(String[] args) { Test test=new Test(); filepath="D:\\conf\\bsidinfo.properties"; test.updateProperty("test4","bsid","4"); //其他的方法类似 map就是[]下面的名值对key=value } }
发表评论
-
Java动态代理
2015-03-11 12:56 506本篇博客介绍是Java代理模式的基本提纲,帮助初步了解的人 ... -
如何选择SSH框架和Spring+SpringMVC+Mybatis
2015-02-02 15:41 0SSH的架构已经使用了四五年了,最近在思考为什么要使用框架 ... -
log4jweb中加载
2012-02-03 09:45 0Log4j在JAVA WEB应用中的推荐配置方法[转](200 ... -
log日志的存放位置
2012-02-03 09:42 0以DailyRollingFileAppender 为例:假设 ... -
JMS一些实例十一到实例十二
2011-12-29 17:13 694JMS一些实例十一到实例十二 深入掌握JMS(十一): ... -
JMS一些实例六到实例十
2011-12-29 17:08 741继续上篇文章jms实例六到十 深入掌握JMS(六):消 ... -
JMS一些实例一到实例五
2011-12-29 17:00 796本文并非原创 纯属转载 深入掌握JMS(一):JSM基 ... -
java email 实例
2011-12-23 16:18 622网上找的一些发邮件的代码供大家学习(eclipse java项 ... -
java modem 短信发送
2011-12-21 14:20 1255前一段时间公司用到短信modem发送信息,我通过参考网络资料, ... -
log4j 驱动加载错误
2011-12-16 15:59 1567错误: java.lang.ClassNotFoundExc ... -
log4j详解 转载
2011-12-16 15:52 760stone 的 log4j配置详解 Log4J的配置文件(C ...
相关推荐
java调用oracle存储过程入门实例 增删改查 使用dom4j读取数据库的配置文件(xml) jdbc中connection的管理 使用threadlocal
Apache Commons Configuration对properties、xml配置文件的【增删改查】及动态加载小案例。小编也是找了网上很多资源,综合研究了一下。被怪我黑!!!
Java之JDBC连接数据库实现增删改查(2018 使用Dao层实现 完美封装解决硬编码问题 使用预编译对象PreparedStatement) 配置文件 db.properties(保存数据库账号和密码等) 工具类 JDBCUtil.java(抽取公共部分,解决硬...
Java之JDBC连接数据库实现增删改查(2018 使用Dao层实现 完美封装解决硬编码问题) 配置文件 db.properties(保存数据库账号和密码等) 工具类 JDBCUtil.java(抽取公共部分,解决硬编码问题) 用户账号实体类 User....
- Properties配置文件:2个,存储项目配置信息 - 字体文件:1个EOT和1个SVG,为前端提供丰富的字体样式 功能简述: 本项目基于SSM框架实现了基础的增删改查功能,为用户提供了一个简洁、高效的交互平台,可以快速...
实现类 UserDaoImpl.java(实现增删改查功能 使用预编译对象PreparedStatement 安全、便捷不需要我们去拼接字符串,特别是字段很多的时候 同时效率比Statement更高 ) 测试类 UserDaoTest.java(做测试增删改查功能...
使用ibatis基本配置及连接数据库 进行增删改查 存储过程及分页模拟 根据配置文件输出sql日志
该demo针对自己需要使用cjon来存储配置文件,学习cjon的记录
执行增删改查操作。 关于SQLite SQLite是一个轻量级的关系型数据库管理系统,遵守ACID原则,其特点是高度便携、使用方便、结构紧凑、高效和可靠。它不是客户端-服务器结构的数据库,而是嵌入式的数据库引擎,可以将...
Java之JDBC连接数据库实现增删改查(2018 使用Dao层实现 完美封装解决硬编码问题) 配置文件 db.properties(保存数据库账号和密码等) 工具类 JDBCUtil.java(抽取公共部分,解决硬编码问题) 用户账号实体类 User....
.ini配置文件常被用作存储程序中的一些参数,通过...下面这篇文章主要给大家介绍了关于python对配置文件.ini进行增删改查操作的方法示例,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
源码是经过本地编译可运行的,下载完成之后配置相应环境即可使用。源码功能都是经过老师肯定的,都能满足要求,有需要放心下载即可 源码是经过本地编译可运行的,下载完成之后配置相应环境即可使用。源码功能都是...
mongoDB是一个基于分布式文件存储的数据库,由 C++ 语言编写,旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。它介于关系数据库和非关系数据库之间,被认为是非关系数据库当中功能最丰富,最像关系数据库的...
选择 需要管理 的管理项后, 会跳转到 管理界面, 可进行 增, 删, 改, 查 四项基本管理功能. 增 点击 新建 按钮, 即可弹出 对应管理项 的 新建对话框, 填入数据 后点击 确定 按钮, 即可新建一条数据. 删 选中 要...
本文实例讲述了Android编程连接MongoDB及增删改查等基本操作。分享给大家供大家参考,具体如下: MongoDB简介 Mongodb,分布式文档存储数据库,由C++语言编写,旨在为WEB应用提供可扩展的高性能数据存储解决方案。...
2. oracle文件操作命令(增,删,改,查,vi编辑文件) 3. oracle用户、组 (linux用户、组的操作命令、切换用户) 4. oracle安装需要的包(软件)支持 5. linux软件安装、解压缩的命令,yum工具 6. oracle需要的...
院系信息的增、删、改 班级信息的增、删、改 使用部门信息的增、删、改 密码的修改 书费管理 交纳书费 退回多余的书费 出入库 学生领书 院系领书 教材入库 综合查询 书费查询 领书查询 ...
此资源为后台代码:对查询食品信息的存储读取,爆料以及用户信息的增删改查。 注意:上传资源包括后台的使用文档,需要下载者配置.yml文件,才能启动此后台程序。前端代码和数据库表结构和表关系作者会相继上传。...
HDFS Shell命令允许使用命令行的方式在HDFS存储中进行文件夹和文件操作,包括像:文件夹的增删改查、文件的增删改查等等。 HDFS Shell命令 调用文件系统(FS)Shell命令应使用 bin/hadoop fs 的形式。 所有的的FS ...
主要介绍了C#针对xml基本操作及保存配置文件应用实例,包括了针对XML文件的定义、初始化、创建、以及增删改查等基础操作,并配有详细的实例加以说明,需要的朋友可以参考下