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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
G
Google Developers Blog
Project Zero
Project Zero
小众软件
小众软件
NISL@THU
NISL@THU
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
B
Blog RSS Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
宝玉的分享
宝玉的分享
博客园 - 司徒正美
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
G
GRAHAM CLULEY
GbyAI
GbyAI
Recent Announcements
Recent Announcements
Cisco Talos Blog
Cisco Talos Blog
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
T
Tailwind CSS Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
PCI Perspectives
PCI Perspectives
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threatpost
B
Blog
The Hacker News
The Hacker News
Attack and Defense Labs
Attack and Defense Labs
腾讯CDC
T
Tenable Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 可可

广东电信最新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