惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
人人都是产品经理
人人都是产品经理
Recent Announcements
Recent Announcements
D
DataBreaches.Net
P
Proofpoint News Feed
V
Visual Studio Blog
J
Java Code Geeks
Recorded Future
Recorded Future
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Full Disclosure
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
罗磊的独立博客
Jina AI
Jina AI
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
G
GRAHAM CLULEY
Y
Y Combinator Blog
L
LangChain Blog
L
LINUX DO - 热门话题
宝玉的分享
宝玉的分享
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security
云风的 BLOG
云风的 BLOG
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园_首页
A
About on SuperTechFans
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Latest news
Latest news
T
Threatpost
T
Tenable Blog
有赞技术团队
有赞技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Stack Overflow Blog
Stack Overflow Blog
C
Cisco Blogs
C
Check Point Blog
T
Tor Project blog
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
美团技术团队
I
Intezer
S
Securelist
AWS News Blog
AWS News Blog

博客园 - 可可

广东电信最新DNS更新了 分布式缓存失效处理 序列化 suse中计划任务工具cron的使用 转载《.NET框架设计之五------------异常设计(异常与返回值)》 《高级Bash脚本编程指南》在线阅读 转载《Linux Shell Bash 各种小技巧》 Linux多线程例子 转载《Adding a Second IP Address to an Existing Network Adapter on Linux》 SuSE下chkconfig的启动脚本参考例子"/etc/init.d/skeleton” - 可可 - 博客园 转载《Starting and Stoping Services: insserv》 linux下c++程序编译错误--理解typename 一个公网IP情况下实现LVS VS/DR vmware中suse10不能上网解决方法 Using PHP with mod_fcgid PHP在Apache中两种工作方式的区别(CGI模式、Apache 模块DLL) SSO 转贴《负载均衡技术介绍》 CDN
Apache Configuration
可可 · 2009-02-02 · via 博客园 - 可可

Posted on 2009-02-02 08:51  可可  阅读(1134)  评论()    收藏  举报

浏览Apache的项目,突然发现Apache Configuration这个好东东,试用了一番,赞不绝口,于是就在这里推荐给各位朋友。http://jakarta.apache.org/commons/configuration/

      我们写程序的时候经常需要对一些参数进行

动态

配置,比如动态开辟内存的大小,要打开的文件名,可视化程序的背景颜色、窗体大小等等。通常我们会把这些变量写到一个配置文件中,程序每次启动的时候加载它并初始化各种参数,根据需要还可以把发生变化的参数写回配置文件。

      每到这个时候,读写配置文件是一个很繁琐的事情。需要先自己

定义

配置文件的格式,然后写代码打开文件,对关键字进行匹配,然后提取子串,再进行String到某类型的转换。总之这个过程是痛苦异常。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<gui-definition>
<colors>
<background>#808080</background>
<text>#000000</text>
<header>#008000</header>
<link normal="#000080" visited="#800080"/>
<default>${colors.header}</default>
</colors>
<rowsPerPage>15</rowsPerPage>
<buttons>
<name>OK,Cancel,Help</name>
</buttons>
<numberFormat pattern="###\,###.##"/>
</gui-definition>
Apache Configuration读取这个配置文件的代码非常非常的简单。首先生成一个Configuration的实例,同时用xml文件名进行初始化。
try {
XMLConfiguration config = new XMLConfiguration("tables.xml");
}
catch(ConfigurationException cex) {
// something went wrong, e.g. the file was not found
}
初始化完毕后,就可以进行参数的读取了
 
String backColor = config.getString("colors.background");
String textColor = config.getString("colors.text");
String linkNormal = config.getString("colors.link[@normal]");
String defColor = config.getString("colors.default");
int rowsPerPage = config.getInt("rowsPerPage");
List buttons = config.getList("buttons.name");
怎么样,是不是非常的简单:) 除了读取参数之外还可以把修改过的参数存储到配置文件中。
config.setProperty("background", "#999999");
config.setProperty("rowsPerPage", 82);
config.save();
 
简直是太方便了!以后用JAVA写程序再也不用怕繁琐的配置文件操作了!
转载地址:http://www.langtech.org.cn/index.php/uid-5080-action-viewspace-itemid-70