﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-Simplify the source work!</title><link>http://www.blogjava.net/sinpo/</link><description>关注开源，参与开源！</description><language>zh-cn</language><lastBuildDate>Sun, 12 Apr 2026 06:01:02 GMT</lastBuildDate><pubDate>Sun, 12 Apr 2026 06:01:02 GMT</pubDate><ttl>60</ttl><item><title>软件开发，我们还需要什么</title><link>http://www.blogjava.net/sinpo/archive/2008/11/17/241049.html</link><dc:creator>徐辛波</dc:creator><author>徐辛波</author><pubDate>Mon, 17 Nov 2008 15:09:00 GMT</pubDate><guid>http://www.blogjava.net/sinpo/archive/2008/11/17/241049.html</guid><wfw:comment>http://www.blogjava.net/sinpo/comments/241049.html</wfw:comment><comments>http://www.blogjava.net/sinpo/archive/2008/11/17/241049.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sinpo/comments/commentRss/241049.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sinpo/services/trackbacks/241049.html</trackback:ping><description><![CDATA[<p style="text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; text-align:justify; "><span style=" font-size:10.5000pt; font-family:'宋体'; "><font face="宋体">不止一次我们的项目在靠开发人员硬扛着，bug来不及修改，文档还在猛补，项目经理又在催着出版本，每修改一个bug都要在代码的泥沼中摸爬滚打半天，却又在制造着另外的bug，为了文档而制造着根本没有参考价值的文档。每发布一个版本都要瞻前顾后并且总要无休止的加班，似乎开发人员永远有干不完的活。</font></span></p>
<p style="text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; text-align:justify; "><span style=" font-size:10.5000pt; font-family:'宋体'; "><font face="宋体">我们不禁问自己，为什么会有那么多失败的软件，软件开发我们到底还需要什么？&#160;</font></span></p>
<p style="text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; text-align:justify; "><span style=" font-size:10.5000pt; font-family:'宋体'; "><font face="宋体">工具？我们不乏伟大的工具，IDE我们有eclipse、intelliJ、VC++等等，建模我们有Rose、together、Visio等等，配置管理我们Subversion、CVS、ClearCase等等，bug管理我们有ClearQuest、bugzilla等等，文档我们word、wps等，还有集成、测试甚至生成代码等等我们都有伟大的工具，工具我们不缺。流程？瀑布模型、迭代模型、UP、XP我们不缺流程管理的理论。知识？软件开发算是一项知识型的工作，我们的开发人员一般都是本科硕士毕业，况且搜索引擎如此发达的今天，知识根本不是问题，况且我们的软件开发需要多么高深的知识吗?</font></span></p>
<p style="text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; text-align:justify; "><span style=" font-size:10.5000pt; font-family:'宋体'; ">&#160;<font face="宋体">优秀的理论支持、有能力的人员、先进的工具，这些我们都具备，我们所缺的只是一种软件开发的理念，缺少软件开发的情商。我们一开始学习编程知识接触到的就是C语言和数据结构，慢慢的一些结构化的思想就扎根于大脑，其实在商业软件中数据结构、算法很少涉及，我们所需要的仅仅是一种设计、开发的理念。比如用面向对象本来是一种简单的思想，目的是为了降低软件的复杂性而出现的，可是让熟悉了结构化编程的人去搞反而觉得很难。一些好的实践经验我们也经常提到，比如模块化、松散耦合、面向接口编程、类应只关注本职工作等等开发设计理念以及规范命名、详尽使用的注释、清晰的结构等代码规范以及每日构建、有效沟通、配置管理、bug管理等一些管理理念，这些做起来都非常容易，关键是懒惰是人的本性，不知不觉中我们就会犯着大家都在重复的错误。如果在项目开工之初就充分贯彻这些优秀的理念，在项目进行中无论时间多紧都持之以恒，并且项目进行中不断的反思代码中的坏味道，一经发现立即重构，相信我们的开发过程会进入一个良性的循环中去，我们的开发人员将会体会到什么是快乐开发。</font></span></p>
<img src ="http://www.blogjava.net/sinpo/aggbug/241049.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sinpo/" target="_blank">徐辛波</a> 2008-11-17 23:09 <a href="http://www.blogjava.net/sinpo/archive/2008/11/17/241049.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>诚心结识有志于研究、开发开源软件的志同道合之士组建开源团队</title><link>http://www.blogjava.net/sinpo/archive/2008/10/31/237895.html</link><dc:creator>徐辛波</dc:creator><author>徐辛波</author><pubDate>Fri, 31 Oct 2008 13:00:00 GMT</pubDate><guid>http://www.blogjava.net/sinpo/archive/2008/10/31/237895.html</guid><wfw:comment>http://www.blogjava.net/sinpo/comments/237895.html</wfw:comment><comments>http://www.blogjava.net/sinpo/archive/2008/10/31/237895.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/sinpo/comments/commentRss/237895.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sinpo/services/trackbacks/237895.html</trackback:ping><description><![CDATA[徐辛波，西安，从事软件开发设计工作，熟悉Java语言，爱好开发工作，特别是java相关的编程，业余关注开源项目，诚心结识志同道合之士组建开源团队，共同学习、进步、协作、为中国开源事业贡献微薄之力。
<div>徐辛波 sinpo.xu@gmail.com</div>
<img src ="http://www.blogjava.net/sinpo/aggbug/237895.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sinpo/" target="_blank">徐辛波</a> 2008-10-31 21:00 <a href="http://www.blogjava.net/sinpo/archive/2008/10/31/237895.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDOM常用类介绍及示例代码</title><link>http://www.blogjava.net/sinpo/archive/2008/10/25/236589.html</link><dc:creator>徐辛波</dc:creator><author>徐辛波</author><pubDate>Sat, 25 Oct 2008 13:02:00 GMT</pubDate><guid>http://www.blogjava.net/sinpo/archive/2008/10/25/236589.html</guid><wfw:comment>http://www.blogjava.net/sinpo/comments/236589.html</wfw:comment><comments>http://www.blogjava.net/sinpo/archive/2008/10/25/236589.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sinpo/comments/commentRss/236589.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sinpo/services/trackbacks/236589.html</trackback:ping><description><![CDATA[<div style="layout-grid:15.6000pt;">
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" font-size:10.5000pt; font-family:'宋体'; ">JDOM<font face="宋体">因其简洁易用易懂的API而被广泛的使用。JDOM常用的核心类及它们间的关系如下图所示：</font></span></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><img width="450" height="352" src="http://www.blogjava.net/images/blogjava_net/sinpo/35487/r_JDOM%20Core.jpg" alt="" /></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" font-size:10.5000pt; font-family:'宋体'; ">Document<font face="宋体">代表了文档对象，抽象类Content表示文档中的内容元素，各种内容组成了文档对象。常用的内容元素有xml元素Element、xml注释Comment、文本Text。下面以如下片段来说明各类的含义。</font></span></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'Courier New'; ">&lt;?xml version="1.0" encoding="UTF-8"?></span></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;customers></span></p>
<p style="text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;customer></span></p>
<p style="margin-left:21.0000pt; text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;name><font face="宋体">徐辛波&lt;/name></font></span></p>
<p style="margin-left:21.0000pt; text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;occupation>developer&lt;/occupation></span></p>
<p style="margin-left:21.0000pt; text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;!-- comment:following is contact info --></span></p>
<p style="margin-left:21.0000pt; text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;contact></span></p>
<p style="margin-left:42.0000pt; text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; "><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#38;&#97;&#109;&#112;&#59;&#97;&#109;&#112;&#59;&#97;&#109;&#112;&#59;&#97;&#109;&#112;&#59;&#108;&#116;&#59;&#101;&#109;&#97;&#105;&#108;&#62;&#115;&#105;&#110;&#112;&#111;&#46;&#120;&#117;&#64;&#104;&#111;&#116;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;&#38;&#97;&#109;&#112;&#59;&#97;&#109;&#112;&#59;&#97;&#109;&#112;&#59;&#97;&#109;&#112;&#59;&#108;&#116;&#59;&#47;&#101;&#109;&#97;&#105;&#108;&#62;"><span style=" font-size:10.0000pt; font-family:'宋体'; ">&lt;email>sinpo.xu@hotmail.com&lt;/email></span></a></p>
<p style="margin-left:42.0000pt; text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;mobile>15029357227&lt;/mobile></span></p>
<p style="margin-left:42.0000pt; text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;fix-phone>02985457683&lt;/fix-phone></span></p>
<p style="margin-left:21.0000pt; text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;/contact></span></p>
<p style="text-indent:21.0000pt; margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;/customer></span></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;/customers></span></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" font-size:10.5000pt; font-family:'宋体'; "><font face="宋体">上述文档用Document来抽象；customers为文档的根元素（root element ），Element即一个封闭起来的元素，element元素可以有子元素，如</font></span><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">&lt;mobile>15029357227&lt;/mobile><font face="宋体">是一个元素，而&lt;contact>...&lt;/contact>也是一个元素，甚至&lt;customers>...&lt;/customers>也是一个大元素；&lt;!-- ... -->代表了xml中注释，注释在JDOM中用Comment类来抽象；Text代表了xml中的文本值，如元素属性的值、元素的值、注释的内容等，父元素的Text为子元素和值组成的串，使用Text类可以方便的表示一些特殊字符,如：</font></span></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">Element element = new Element("name");</span></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">Text text = new Text("AAA.&lt;<font face="宋体">、BBB/>.&lt;CCC>");</font></span></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; ">element.addContent(text);</span></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; "><font face="宋体">值得一提的是Element的方法addContent(Content content),因参数是抽象父类Content，所以可以添加Text、Element和Comment等，如果添加的是Text则自动作为element的文本值，如果是Element则作为element的子元素，如果是Comment则作为element的注释，使用十分方便。元素的值如&lt;name>徐辛波&lt;/name>中的&#8220;徐辛波&#8221;也是一个和元素平行的Content对象（Text对象），当使用Element的getDescendants()方法时将返回一个该元素所有后代的迭代器，这些后代包括Element、Comment、Text等，如元素&lt;contact>的后代包括email、mobile、fix-phone三个元素以及这三个元素的Text共6个后代，如果计算后代时有父子嵌套则应注意，父元素作为一个后代，其嵌套的子元素作为另一个后代。</font></span></p>
<p style="margin-bottom:0pt; margin-top:0pt; "><span style=" color:rgb(0,0,0); font-size:10.5000pt; font-family:'宋体'; "><font face="宋体">刚才提到核心类都包含在org.jdom包下，jdom还包含了org.jdom.input和org.jdom.output两个包分别来处理xml内容的输入输出。当要读取xml资源时我们通常使用input包下的SAXBuilder类从输入流构建dom对象，当资源加载后常用的做法是在内存中缓存，这样后续的查找修改等操作就非常快。文档加载后内存的中各个元素是记录有各自的位置和关系的，即保持有上下文环境的。如果想要删除一段内容（Element Comment Text），只用调用该内容的detach方法即可，这样元素即和文档脱离关系了，再对文档进行遍历或者持久化到磁盘上时游离的元素就不可见了。Jdom的输出类包括XMLOutputter、DOMOutputter、SAXOutputter。最常用的是XMLOutputter，通过它可以将dom对象输出到指定的输出流，并且可以指定所输出xml文件的格式，比如缩进的样式等。DOMOutputter输出</font></span><span style=" font-size:10.5000pt; font-family:'Courier New'; ">org.w3c.dom.Document</span><span style=" font-size:10.5000pt; font-family:'宋体'; "><font face="宋体">对象，用于JDOM对象同w3c dom对象转换，SAXOutputter可以注册回调函数来处理相应的sax事件。</font></span></p>
</div><br />一下示例代码实现一个常用的读取配置文件并且允许更改后同步到磁盘的操作：<br /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title></title>  <style type="text/css">    <!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->  </style>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body>

<!-- ======================================================== --><!-- = Java Sourcecode to HTML automatically converted code = --><!-- =   Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard  markus@jave.de   = --><!-- =     Further information: http://www.java2html.de     = --><div align="left" class="java"><table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff">   <tr>  <!-- start source code -->   <td nowrap="nowrap" valign="top" align="left">    <code><font color="#7f0055"><strong>package&nbsp;</strong></font><font color="#000000">sinpo.usagedemo;</font><br /><font color="#ffffff"></font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">java.io.File;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">java.io.FileOutputStream;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">java.io.IOException;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">java.io.InputStream;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">java.io.OutputStream;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">java.net.URL;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">java.util.List;</font><br /><font color="#ffffff"></font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">org.jdom.Document;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">org.jdom.Element;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">org.jdom.input.SAXBuilder;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">org.jdom.output.Format;</font><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">org.jdom.output.XMLOutputter;</font><br /><font color="#ffffff"></font><br /><font color="#3f5fbf">/**</font><br /><font color="#ffffff">&nbsp;</font><font color="#3f5fbf">*&nbsp;读取配置文件，并且修改后及时同步到磁盘</font><br /><font color="#ffffff">&nbsp;</font><font color="#3f5fbf">*&nbsp;</font><font color="#7f9fbf">@author&nbsp;</font><font color="#3f5fbf">徐辛波(sinpo.xu@hotmail.com)&nbsp;</font><br /><font color="#ffffff">&nbsp;</font><font color="#3f5fbf">*&nbsp;Oct&nbsp;23,&nbsp;2008</font><br /><font color="#ffffff">&nbsp;</font><font color="#3f5fbf">*/</font><br /><font color="#7f0055"><strong>public&nbsp;class&nbsp;</strong></font><font color="#000000">Configuration&nbsp;</font><font color="#000000">{</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#000000">Element&nbsp;root&nbsp;=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#000000">Document&nbsp;dom&nbsp;=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;static&nbsp;final&nbsp;</strong></font><font color="#000000">String&nbsp;resourceName&nbsp;=&nbsp;</font><font color="#2a00ff">&#34;/config.xml&#34;</font><font color="#000000">;</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;static&nbsp;</strong></font><font color="#000000">Configuration&nbsp;_INSTANCE&nbsp;=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>public&nbsp;static&nbsp;synchronized&nbsp;</strong></font><font color="#000000">Configuration&nbsp;getInstance</font><font color="#000000">()&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">_INSTANCE&nbsp;==&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">_INSTANCE&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">Configuration</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>return&nbsp;</strong></font><font color="#000000">_INSTANCE;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#000000">Configuration</font><font color="#000000">()&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">load</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#000000">String&nbsp;getConfig</font><font color="#000000">(</font><font color="#000000">String&nbsp;configName</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">String&nbsp;configValue&nbsp;=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Element&nbsp;found&nbsp;=&nbsp;findRecursively</font><font color="#000000">(</font><font color="#000000">configName,&nbsp;root</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">found&nbsp;!=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">configValue&nbsp;=&nbsp;found.getText</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>return&nbsp;</strong></font><font color="#000000">configValue;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#7f0055"><strong>void&nbsp;</strong></font><font color="#000000">updateConfig</font><font color="#000000">(</font><font color="#000000">String&nbsp;configName,&nbsp;String&nbsp;newValue</font><font color="#000000">)</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>throws&nbsp;</strong></font><font color="#000000">IOException&nbsp;</font><font color="#000000">{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Element&nbsp;found&nbsp;=&nbsp;findRecursively</font><font color="#000000">(</font><font color="#000000">configName,&nbsp;root</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">found&nbsp;!=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">found.setText</font><font color="#000000">(</font><font color="#000000">newValue</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}&nbsp;</font><font color="#7f0055"><strong>else&nbsp;</strong></font><font color="#000000">{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Element&nbsp;configNode&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">Element</font><font color="#000000">(</font><font color="#000000">configName</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">configNode.addContent</font><font color="#000000">(</font><font color="#000000">newValue</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#3f7f5f">//&nbsp;also:&nbsp;configNode.setText(newValue);</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">root.addContent</font><font color="#000000">(</font><font color="#000000">configNode</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">sync</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#7f0055"><strong>void&nbsp;</strong></font><font color="#000000">deleteConfig</font><font color="#000000">(</font><font color="#000000">String&nbsp;configName</font><font color="#000000">)&nbsp;</font><font color="#7f0055"><strong>throws&nbsp;</strong></font><font color="#000000">IOException&nbsp;</font><font color="#000000">{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Element&nbsp;found&nbsp;=&nbsp;findRecursively</font><font color="#000000">(</font><font color="#000000">configName,&nbsp;root</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">found&nbsp;!=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">found.detach</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">sync</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#7f0055"><strong>void&nbsp;</strong></font><font color="#000000">load</font><font color="#000000">()&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">SAXBuilder&nbsp;builder&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">SAXBuilder</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">InputStream&nbsp;source&nbsp;=&nbsp;getClass</font><font color="#000000">()</font><font color="#000000">.getResourceAsStream</font><font color="#000000">(</font><font color="#000000">resourceName</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>try&nbsp;</strong></font><font color="#000000">{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">dom&nbsp;=&nbsp;builder.build</font><font color="#000000">(</font><font color="#000000">source</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">root&nbsp;=&nbsp;dom.getRootElement</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}&nbsp;</font><font color="#7f0055"><strong>catch&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">Exception&nbsp;e</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">e.printStackTrace</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#3f7f5f">//&nbsp;递归查找.&nbsp;在指定的父节点下查找叶子元素</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#000000">Element&nbsp;findRecursively</font><font color="#000000">(</font><font color="#000000">String&nbsp;name,&nbsp;Element&nbsp;parent</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Element&nbsp;found&nbsp;=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">List&lt;Element&gt;&nbsp;children&nbsp;=&nbsp;parent.getChildren</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">children&nbsp;!=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>for&nbsp;</strong></font><font color="#000000">(</font><font color="#7f0055"><strong>int&nbsp;</strong></font><font color="#000000">i&nbsp;=&nbsp;</font><font color="#990000">0</font><font color="#000000">;&nbsp;i&nbsp;&lt;&nbsp;children.size</font><font color="#000000">()</font><font color="#000000">;&nbsp;i++</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Element&nbsp;element&nbsp;=&nbsp;children.get</font><font color="#000000">(</font><font color="#000000">i</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">String&nbsp;tmpName&nbsp;=&nbsp;element.getName</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">((</font><font color="#000000">name.equals</font><font color="#000000">(</font><font color="#000000">tmpName</font><font color="#000000">))&nbsp;</font><font color="#000000">&amp;&amp;&nbsp;</font><font color="#000000">(</font><font color="#000000">!hasChild</font><font color="#000000">(</font><font color="#000000">element</font><font color="#000000">)))&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>return&nbsp;</strong></font><font color="#000000">element;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>for&nbsp;</strong></font><font color="#000000">(</font><font color="#7f0055"><strong>int&nbsp;</strong></font><font color="#000000">i&nbsp;=&nbsp;</font><font color="#990000">0</font><font color="#000000">;&nbsp;i&nbsp;&lt;&nbsp;children.size</font><font color="#000000">()</font><font color="#000000">;&nbsp;i++</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Element&nbsp;element&nbsp;=&nbsp;children.get</font><font color="#000000">(</font><font color="#000000">i</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">hasChild</font><font color="#000000">(</font><font color="#000000">element</font><font color="#000000">))&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">found&nbsp;=&nbsp;findRecursively</font><font color="#000000">(</font><font color="#000000">name,&nbsp;element</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">found&nbsp;!=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>return&nbsp;</strong></font><font color="#000000">found;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>return&nbsp;</strong></font><font color="#000000">found;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#7f0055"><strong>boolean&nbsp;</strong></font><font color="#000000">hasChild</font><font color="#000000">(</font><font color="#000000">Element&nbsp;element</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>boolean&nbsp;</strong></font><font color="#000000">hasChild&nbsp;=&nbsp;</font><font color="#7f0055"><strong>false</strong></font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">List&nbsp;children&nbsp;=&nbsp;element.getChildren</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">((</font><font color="#000000">children&nbsp;!=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">)&nbsp;</font><font color="#000000">&amp;&amp;&nbsp;</font><font color="#000000">(</font><font color="#000000">children.size</font><font color="#000000">()&nbsp;</font><font color="#000000">&gt;&nbsp;</font><font color="#990000">0</font><font color="#000000">))&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">hasChild&nbsp;=&nbsp;</font><font color="#7f0055"><strong>true</strong></font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>return&nbsp;</strong></font><font color="#000000">hasChild;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#7f0055"><strong>void&nbsp;</strong></font><font color="#000000">sync</font><font color="#000000">()&nbsp;</font><font color="#7f0055"><strong>throws&nbsp;</strong></font><font color="#000000">IOException&nbsp;</font><font color="#000000">{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Format&nbsp;format&nbsp;=&nbsp;Format.getPrettyFormat</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">XMLOutputter&nbsp;outputter&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">XMLOutputter</font><font color="#000000">(</font><font color="#000000">format</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">File&nbsp;file&nbsp;=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">URL&nbsp;url&nbsp;=&nbsp;getClass</font><font color="#000000">()</font><font color="#000000">.getResource</font><font color="#000000">(</font><font color="#000000">resourceName</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">url&nbsp;==&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">file&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">File</font><font color="#000000">(</font><font color="#000000">resourceName</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}&nbsp;</font><font color="#7f0055"><strong>else&nbsp;</strong></font><font color="#000000">{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">file&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">File</font><font color="#000000">(</font><font color="#000000">url.getPath</font><font color="#000000">())</font><font color="#000000">;</font><br /><font color="#ffffff"></font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">OutputStream&nbsp;out&nbsp;=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>try&nbsp;</strong></font><font color="#000000">{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">out&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">FileOutputStream</font><font color="#000000">(</font><font color="#000000">file</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">outputter.output</font><font color="#000000">(</font><font color="#000000">dom,&nbsp;out</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">out.close</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">out&nbsp;=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}&nbsp;</font><font color="#7f0055"><strong>catch&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">Exception&nbsp;e</font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">e.printStackTrace</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>if&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">out&nbsp;!=&nbsp;</font><font color="#7f0055"><strong>null</strong></font><font color="#000000">)&nbsp;{</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">out.close</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#000000">}</font></code>       </td>  <!-- end source code -->   </tr></table></div><!-- =       END of automatically generated HTML code       = --><!-- ======================================================== -->

</body></html>
 <img src ="http://www.blogjava.net/sinpo/aggbug/236589.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sinpo/" target="_blank">徐辛波</a> 2008-10-25 21:02 <a href="http://www.blogjava.net/sinpo/archive/2008/10/25/236589.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>也谈线程的休眠与等待</title><link>http://www.blogjava.net/sinpo/archive/2008/10/22/236061.html</link><dc:creator>徐辛波</dc:creator><author>徐辛波</author><pubDate>Wed, 22 Oct 2008 15:26:00 GMT</pubDate><guid>http://www.blogjava.net/sinpo/archive/2008/10/22/236061.html</guid><wfw:comment>http://www.blogjava.net/sinpo/comments/236061.html</wfw:comment><comments>http://www.blogjava.net/sinpo/archive/2008/10/22/236061.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sinpo/comments/commentRss/236061.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sinpo/services/trackbacks/236061.html</trackback:ping><description><![CDATA[<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-align: justify">
<span style="font-size: 10.5pt; font-family: '宋体'; mso-spacerun: 'yes'">
<font face="宋体">关于线程间的交互和共享数据通常有轮询和通知机制。一下举例说明：Thread1和Thread2共享一块数据ShareData，Thread1使用数据，Thread2更新数据。当Thread1使用数据时发现数据没有更新就可以先休眠（sleep（））一段时间然后再去判断是否更新，如此反复直到数据可用，这就是所述的轮询机制。可以看出轮询机制需要不断的轮询数据状态，很耗费资源；当采用通知机制时过程是这样的，Thread1发现数据不可用就在ShareData上等待（ShareData.wait()），当Thread2更新数据后就通知所有在ShareData上等待的线程（ShareData.notifyAll())，这样Thread1受到通知继续运行。</font>
</span>
<span style="font-size: 10.5pt; font-family: '宋体'; mso-spacerun: 'yes'">
<o:p>
</o:p>
</span>
</p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-align: justify">
<span style="font-size: 10.5pt; font-family: '宋体'; mso-spacerun: 'yes'">
<font face="宋体">关于等待和休眠还有另一个区别就是当线程等待时，该线程锁定的资源是释放掉的，这时其它线程是可以锁定这些资源的，当线程被唤醒或者等待时限到时线程重新获取资源才能继续运行；而当线程休眠时线程锁定的资源是不被释放的。</font>
</span>
<span style="font-size: 10.5pt; font-family: '宋体'; mso-spacerun: 'yes'">
<o:p>
</o:p>
</span>
</p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-align: justify">
<span style="font-size: 10.5pt; font-family: '宋体'; mso-spacerun: 'yes'">
<font face="宋体">还有一点就是要在对象lock上等待时是必须先要获取lock的对象锁才能进行的，即必须要类似下面的逻辑</font>
</span>
<span style="font-weight: bold; font-size: 10.5pt; font-family: '宋体'; mso-spacerun: 'yes'">synchronized<font face="宋体">（lock){&#160;lock.wait()}</font></span>
<span style="font-weight: bold; font-size: 10.5pt; font-family: '宋体'; mso-spacerun: 'yes'">
<o:p>
</o:p>
</span>
</p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt">
<span style="font-size: 10.5pt; font-family: '宋体'; mso-spacerun: 'yes'">
<font face="宋体">以下为一个简单的示例：</font>
</span>
</p>
<!--EndFragment-->
<!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = -->
<!-- =   Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard  markus@jave.de   = -->
<!-- =     Further information: http://www.java2html.de     = -->
<div align="left" class="java">
<table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff">
    <tbody>
        <tr>
            <!-- start source code -->
            <td nowrap="nowrap" valign="top" align="left">
            <code>
            <font color="#7f0055">
            <strong>package&#160;</strong>
            </font>
            <font color="#000000">sinpo.usagedemo;</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#3f5fbf">/**</font>
            <br />
            <font color="#ffffff">&#160;</font>
            <font color="#3f5fbf">*&#160;该例子说明线程休眠与等待以及注意事项。</font>
            <br />
            <font color="#ffffff">&#160;</font>
            <font color="#3f5fbf">*&#160;</font>
            <br />
            <font color="#ffffff">&#160;</font>
            <font color="#3f5fbf">*&#160;</font>
            <font color="#7f9fbf">@author&#160;</font>
            <font color="#3f5fbf">徐辛波(sinpo.xu@hotmail.com)&#160;</font>
            <br />
            <font color="#ffffff">&#160;</font>
            <font color="#3f5fbf">*&#160;Oct&#160;22,&#160;2008</font>
            <br />
            <font color="#ffffff">&#160;</font>
            <font color="#3f5fbf">*/</font>
            <br />
            <font color="#7f0055">
            <strong>public&#160;class&#160;</strong>
            </font>
            <font color="#000000">PendingThreadDemo&#160;</font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>public&#160;</strong>
            </font>
            <font color="#000000">Console&#160;console&#160;=&#160;</font>
            <font color="#7f0055">
            <strong>new&#160;</strong>
            </font>
            <font color="#000000">Console</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>private&#160;</strong>
            </font>
            <font color="#7f0055">
            <strong>void&#160;</strong>
            </font>
            <font color="#000000">writeToConsole1</font>
            <font color="#000000">()&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>synchronized</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">console</font>
            <font color="#000000">){</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>try&#160;</strong>
            </font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">Thread.sleep</font>
            <font color="#000000">(</font>
            <font color="#990000">1&#160;</font>
            <font color="#000000">*&#160;</font>
            <font color="#990000">1000</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <font color="#3f7f5f">//NOTE:sleep时并未释放console别的线程是不能锁定console的</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#3f7f5f">//TODO&#160;do&#160;things</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}&#160;</font>
            <font color="#7f0055">
            <strong>catch&#160;</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">InterruptedException&#160;e</font>
            <font color="#000000">)&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">e.printStackTrace</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>private&#160;</strong>
            </font>
            <font color="#7f0055">
            <strong>void&#160;</strong>
            </font>
            <font color="#000000">writeToConsole2</font>
            <font color="#000000">()&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>synchronized</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">console</font>
            <font color="#000000">){</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>try&#160;</strong>
            </font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">console.wait</font>
            <font color="#000000">(</font>
            <font color="#990000">1</font>
            <font color="#000000">*</font>
            <font color="#990000">1000</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <font color="#3f7f5f">//NOTE:wait时别的线程是可以锁定console的</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#3f7f5f">//TODO&#160;do&#160;things</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}&#160;</font>
            <font color="#7f0055">
            <strong>catch&#160;</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">InterruptedException&#160;e</font>
            <font color="#000000">)&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">e.printStackTrace</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#000000">}</font>
            <br />
            <font color="#3f7f5f">//控制台类</font>
            <br />
            <font color="#7f0055">
            <strong>class&#160;</strong>
            </font>
            <font color="#000000">Console&#160;</font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font>
            <font color="#3f7f5f">//TODO&#160;implements&#160;me</font>
            <br />
            <font color="#000000">}</font>
            </code>
            </td>
            <!-- end source code -->
        </tr>
    </tbody>
</table>
</div>
<!-- =       END of automatically generated HTML code       = -->
<!-- ======================================================== -->
 <img src ="http://www.blogjava.net/sinpo/aggbug/236061.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sinpo/" target="_blank">徐辛波</a> 2008-10-22 23:26 <a href="http://www.blogjava.net/sinpo/archive/2008/10/22/236061.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>求助：怎么贴代码才能保持代码的格式呀</title><link>http://www.blogjava.net/sinpo/archive/2008/10/20/235554.html</link><dc:creator>徐辛波</dc:creator><author>徐辛波</author><pubDate>Mon, 20 Oct 2008 14:42:00 GMT</pubDate><guid>http://www.blogjava.net/sinpo/archive/2008/10/20/235554.html</guid><wfw:comment>http://www.blogjava.net/sinpo/comments/235554.html</wfw:comment><comments>http://www.blogjava.net/sinpo/archive/2008/10/20/235554.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/sinpo/comments/commentRss/235554.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sinpo/services/trackbacks/235554.html</trackback:ping><description><![CDATA[<font face="'lucida grande'" size="3">
<span style="font-size: 11px; white-space: pre;">麻烦有谁知道的告知一下。。。</span>
</font>
<br />
今天终于找到一个不错的工具，能将用eclipse等编辑的java代码做成一样风格的html格式，试用了挺好用的，谢谢<a href="http://www.java2html.de/docs/styles.html">Java2Html</a>团队。
<img src ="http://www.blogjava.net/sinpo/aggbug/235554.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sinpo/" target="_blank">徐辛波</a> 2008-10-20 22:42 <a href="http://www.blogjava.net/sinpo/archive/2008/10/20/235554.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java NIO学习-UDP的例子</title><link>http://www.blogjava.net/sinpo/archive/2008/10/20/235553.html</link><dc:creator>徐辛波</dc:creator><author>徐辛波</author><pubDate>Mon, 20 Oct 2008 14:38:00 GMT</pubDate><guid>http://www.blogjava.net/sinpo/archive/2008/10/20/235553.html</guid><wfw:comment>http://www.blogjava.net/sinpo/comments/235553.html</wfw:comment><comments>http://www.blogjava.net/sinpo/archive/2008/10/20/235553.html#Feedback</comments><slash:comments>11</slash:comments><wfw:commentRss>http://www.blogjava.net/sinpo/comments/commentRss/235553.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sinpo/services/trackbacks/235553.html</trackback:ping><description><![CDATA[<div class="Section0" style="layout-grid:15.6000pt;">
<p class="p0" style="margin-bottom:0pt; margin-top:0pt; ">
<span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">
<font face="宋体">这几天需要实现一个底层基于UDP的协议，该协议底层使用UDP传输但是具有拥塞控制、超时重发、数据确认等功能又比TCP简单 （RUDP，Reliable UDP）。在实现协议底层的UDP服务时准备使用Java的NIO，在网上查资料都是以TCP为例讲的，于是自己研究了一下基于UDP的NIO。</font>
</span>
<span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">
<o:p>
</o:p>
</span>
</p>
<p class="p0" style="margin-bottom:0pt; margin-top:0pt; ">
<span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">NIO<font face="宋体">的思路是基于多路选择的，即由原来的每个连接都由一个线程来等待消息，改为每个连接都在选择器上注册，由选择器来等待。当然NIO引入了很多新的概念，如Channel，Buffer、Charset、Selector等，使得编程更简洁、更面向对象化。</font></span>
<span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">
<o:p>
</o:p>
</span>
</p>
<p class="p0" style="margin-bottom:0pt; margin-top:0pt; ">
<span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">
<font face="宋体">下面贴出用NIO API改造成UDP示例代码，注意其中使用Charset来编码解码的过程（当然Charset还支持很多其他编码不仅局限于默认编码）以及Buffer的使用。</font>
</span>
<span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">
<o:p>
</o:p>
</span>
</p>
</div>
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = -->
<!-- =   Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard  markus@jave.de   = -->
<!-- =     Further information: http://www.java2html.de     = -->
<div align="left" class="java">
<table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff">
    <tbody>
        <tr>
            <!-- start source code -->
            <td nowrap="nowrap" valign="top" align="left">
            <code>
            <font color="#7f0055">
            <strong>package </strong>
            </font>
            <font color="#000000">sinpo.usagedemo;</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.net.DatagramSocket;</font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.net.InetSocketAddress;</font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.net.SocketAddress;</font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.nio.ByteBuffer;</font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.nio.CharBuffer;</font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.nio.channels.DatagramChannel;</font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.nio.channels.SelectionKey;</font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.nio.channels.Selector;</font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.nio.charset.Charset;</font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.util.Iterator;</font>
            <br />
            <font color="#7f0055">
            <strong>import </strong>
            </font>
            <font color="#000000">java.util.Set;</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#3f5fbf">/**</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#3f5fbf">* </font>
            <font color="#7f9fbf">@author </font>
            <font color="#3f5fbf">徐辛波(sinpo.xu@hotmail.com) Oct 19, 2008</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#3f5fbf">*/</font>
            <br />
            <font color="#7f0055">
            <strong>public class </strong>
            </font>
            <font color="#000000">UDPServer </font>
            <font color="#7f0055">
            <strong>extends </strong>
            </font>
            <font color="#000000">Thread </font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#7f0055">
            <strong>public </strong>
            </font>
            <font color="#7f0055">
            <strong>void </strong>
            </font>
            <font color="#000000">run</font>
            <font color="#000000">() {</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">Selector selector = </font>
            <font color="#7f0055">
            <strong>null</strong>
            </font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#7f0055">
            <strong>try </strong>
            </font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">DatagramChannel channel = DatagramChannel.open</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">DatagramSocket socket = channel.socket</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">channel.configureBlocking</font>
            <font color="#000000">(</font>
            <font color="#7f0055">
            <strong>false</strong>
            </font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">socket.bind</font>
            <font color="#000000">(</font>
            <font color="#7f0055">
            <strong>new </strong>
            </font>
            <font color="#000000">InetSocketAddress</font>
            <font color="#000000">(</font>
            <font color="#990000">5057</font>
            <font color="#000000">))</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">selector = Selector.open</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">channel.register</font>
            <font color="#000000">(</font>
            <font color="#000000">selector, SelectionKey.OP_READ</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">} </font>
            <font color="#7f0055">
            <strong>catch </strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">Exception e</font>
            <font color="#000000">) {</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">e.printStackTrace</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">ByteBuffer byteBuffer = ByteBuffer.allocate</font>
            <font color="#000000">(</font>
            <font color="#990000">65536</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#7f0055">
            <strong>while </strong>
            </font>
            <font color="#000000">(</font>
            <font color="#7f0055">
            <strong>true</strong>
            </font>
            <font color="#000000">) {</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#7f0055">
            <strong>try </strong>
            </font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#7f0055">
            <strong>int </strong>
            </font>
            <font color="#000000">eventsCount = selector.select</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#7f0055">
            <strong>if </strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">eventsCount > </font>
            <font color="#990000">0</font>
            <font color="#000000">) {</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">Set selectedKeys = selector.selectedKeys</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">Iterator iterator = selectedKeys.iterator</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#7f0055">
            <strong>while </strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">iterator.hasNext</font>
            <font color="#000000">()) {</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">SelectionKey sk = </font>
            <font color="#000000">(</font>
            <font color="#000000">SelectionKey</font>
            <font color="#000000">) </font>
            <font color="#000000">iterator.next</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">iterator.remove</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#7f0055">
            <strong>if </strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">sk.isReadable</font>
            <font color="#000000">()) {</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">DatagramChannel datagramChannel = </font>
            <font color="#000000">(</font>
            <font color="#000000">DatagramChannel</font>
            <font color="#000000">) </font>
            <font color="#000000">sk</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">.channel</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">SocketAddress sa = datagramChannel</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">.receive</font>
            <font color="#000000">(</font>
            <font color="#000000">byteBuffer</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">byteBuffer.flip</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#3f7f5f">// 测试：通过将收到的ByteBuffer首先通过缺省的编码解码成CharBuffer 再输出</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">CharBuffer charBuffer = Charset.defaultCharset</font>
            <font color="#000000">()</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">.decode</font>
            <font color="#000000">(</font>
            <font color="#000000">byteBuffer</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">System.out.println</font>
            <font color="#000000">(</font>
            <font color="#2a00ff">"receive message:"</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">+ charBuffer.toString</font>
            <font color="#000000">())</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">byteBuffer.clear</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">String echo = </font>
            <font color="#2a00ff">"This is the reply message from 服务器。"</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">ByteBuffer buffer = Charset.defaultCharset</font>
            <font color="#000000">()</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">.encode</font>
            <font color="#000000">(</font>
            <font color="#000000">echo</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">datagramChannel.write</font>
            <font color="#000000">(</font>
            <font color="#000000">buffer</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">} </font>
            <font color="#7f0055">
            <strong>catch </strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">Exception e</font>
            <font color="#000000">) {</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">e.printStackTrace</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#7f0055">
            <strong>public static </strong>
            </font>
            <font color="#7f0055">
            <strong>void </strong>
            </font>
            <font color="#000000">main</font>
            <font color="#000000">(</font>
            <font color="#000000">String</font>
            <font color="#000000">[] </font>
            <font color="#000000">args</font>
            <font color="#000000">) {</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#7f0055">
            <strong>new </strong>
            </font>
            <font color="#000000">UDPServer</font>
            <font color="#000000">()</font>
            <font color="#000000">.start</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">
            </font>
            <font color="#000000">}</font>
            <br />
            <font color="#000000">}</font>
            </code>
            </td>
            <!-- end source code -->
        </tr>
    </tbody>
</table>
</div>
<!-- =       END of automatically generated HTML code       = -->
<!-- ======================================================== -->
Client
<br />
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = -->
<!-- =   Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard  markus@jave.de   = -->
<!-- =     Further information: http://www.java2html.de     = -->
<div align="left" class="java">
<table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff">
    <tbody>
        <tr>
            <!-- start source code -->
            <td nowrap="nowrap" valign="top" align="left">
            <code>
            <font color="#7f0055">
            <strong>package&#160;</strong>
            </font>
            <font color="#000000">sinpo.usagedemo;</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#7f0055">
            <strong>import&#160;</strong>
            </font>
            <font color="#000000">java.net.InetSocketAddress;</font>
            <br />
            <font color="#7f0055">
            <strong>import&#160;</strong>
            </font>
            <font color="#000000">java.net.SocketAddress;</font>
            <br />
            <font color="#7f0055">
            <strong>import&#160;</strong>
            </font>
            <font color="#000000">java.nio.ByteBuffer;</font>
            <br />
            <font color="#7f0055">
            <strong>import&#160;</strong>
            </font>
            <font color="#000000">java.nio.channels.DatagramChannel;</font>
            <br />
            <font color="#7f0055">
            <strong>import&#160;</strong>
            </font>
            <font color="#000000">java.nio.channels.SelectionKey;</font>
            <br />
            <font color="#7f0055">
            <strong>import&#160;</strong>
            </font>
            <font color="#000000">java.nio.channels.Selector;</font>
            <br />
            <font color="#7f0055">
            <strong>import&#160;</strong>
            </font>
            <font color="#000000">java.nio.charset.Charset;</font>
            <br />
            <font color="#7f0055">
            <strong>import&#160;</strong>
            </font>
            <font color="#000000">java.util.Iterator;</font>
            <br />
            <font color="#7f0055">
            <strong>import&#160;</strong>
            </font>
            <font color="#000000">java.util.Set;</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#3f5fbf">/**</font>
            <br />
            <font color="#ffffff">&#160;</font>
            <font color="#3f5fbf">*&#160;</font>
            <font color="#7f9fbf">@author&#160;</font>
            <font color="#3f5fbf">徐辛波(sinpo.xu@hotmail.com)</font>
            <br />
            <font color="#ffffff">&#160;</font>
            <font color="#3f5fbf">*&#160;Oct&#160;19,&#160;2008</font>
            <br />
            <font color="#ffffff">&#160;</font>
            <font color="#3f5fbf">*/</font>
            <br />
            <font color="#7f0055">
            <strong>public&#160;class&#160;</strong>
            </font>
            <font color="#000000">UDPClient&#160;</font>
            <font color="#7f0055">
            <strong>extends&#160;</strong>
            </font>
            <font color="#000000">Thread&#160;</font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>public&#160;</strong>
            </font>
            <font color="#7f0055">
            <strong>void&#160;</strong>
            </font>
            <font color="#000000">run</font>
            <font color="#000000">()&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">DatagramChannel&#160;channel&#160;=&#160;</font>
            <font color="#7f0055">
            <strong>null</strong>
            </font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">Selector&#160;selector&#160;=&#160;</font>
            <font color="#7f0055">
            <strong>null</strong>
            </font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>try&#160;</strong>
            </font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">channel&#160;=&#160;DatagramChannel.open</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">channel.configureBlocking</font>
            <font color="#000000">(</font>
            <font color="#7f0055">
            <strong>false</strong>
            </font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">SocketAddress&#160;sa&#160;=&#160;</font>
            <font color="#7f0055">
            <strong>new&#160;</strong>
            </font>
            <font color="#000000">InetSocketAddress</font>
            <font color="#000000">(</font>
            <font color="#2a00ff">"localhost"</font>
            <font color="#000000">,&#160;</font>
            <font color="#990000">5057</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">channel.connect</font>
            <font color="#000000">(</font>
            <font color="#000000">sa</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}&#160;</font>
            <font color="#7f0055">
            <strong>catch&#160;</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">Exception&#160;e</font>
            <font color="#000000">)&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">e.printStackTrace</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>try&#160;</strong>
            </font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">selector&#160;=&#160;Selector.open</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">channel.register</font>
            <font color="#000000">(</font>
            <font color="#000000">selector,&#160;SelectionKey.OP_READ</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">channel.write</font>
            <font color="#000000">(</font>
            <font color="#000000">Charset.defaultCharset</font>
            <font color="#000000">()</font>
            <font color="#000000">.encode</font>
            <font color="#000000">(</font>
            <font color="#2a00ff">"Tell&#160;me&#160;your&#160;time"</font>
            <font color="#000000">))</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}&#160;</font>
            <font color="#7f0055">
            <strong>catch&#160;</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">Exception&#160;e</font>
            <font color="#000000">)&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">e.printStackTrace</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">ByteBuffer&#160;byteBuffer&#160;=&#160;ByteBuffer.allocate</font>
            <font color="#000000">(</font>
            <font color="#990000">100</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>while&#160;</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#7f0055">
            <strong>true</strong>
            </font>
            <font color="#000000">)&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>try&#160;</strong>
            </font>
            <font color="#000000">{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>int&#160;</strong>
            </font>
            <font color="#000000">eventsCount&#160;=&#160;selector.select</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>if&#160;</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">eventsCount&#160;>&#160;</font>
            <font color="#990000">0</font>
            <font color="#000000">)&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">Set&#160;selectedKeys&#160;=&#160;selector.selectedKeys</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">Iterator&#160;iterator&#160;=&#160;selectedKeys.iterator</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>while&#160;</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">iterator.hasNext</font>
            <font color="#000000">())&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">SelectionKey&#160;sk&#160;=&#160;</font>
            <font color="#000000">(</font>
            <font color="#000000">SelectionKey</font>
            <font color="#000000">)&#160;</font>
            <font color="#000000">iterator.next</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">iterator.remove</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#7f0055">
            <strong>if&#160;</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">sk.isReadable</font>
            <font color="#000000">())&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">DatagramChannel&#160;datagramChannel&#160;=&#160;</font>
            <font color="#000000">(</font>
            <font color="#000000">DatagramChannel</font>
            <font color="#000000">)&#160;</font>
            <font color="#000000">sk</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">.channel</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">datagramChannel.read</font>
            <font color="#000000">(</font>
            <font color="#000000">byteBuffer</font>
            <font color="#000000">)</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">byteBuffer.flip</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#3f7f5f">//TODO&#160;将报文转化为RUDP消息并调用RUDP协议处理器来处理</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">System.out.println</font>
            <font color="#000000">(</font>
            <font color="#000000">Charset.defaultCharset</font>
            <font color="#000000">()</font>
            <font color="#000000">.decode</font>
            <font color="#000000">(</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">byteBuffer</font>
            <font color="#000000">)</font>
            <font color="#000000">.toString</font>
            <font color="#000000">())</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">byteBuffer.clear</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">datagramChannel.write</font>
            <font color="#000000">(</font>
            <font color="#000000">Charset.defaultCharset</font>
            <font color="#000000">()</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">.encode</font>
            <font color="#000000">(</font>
            <font color="#2a00ff">"Tell&#160;me&#160;your&#160;time"</font>
            <font color="#000000">))</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}&#160;</font>
            <font color="#7f0055">
            <strong>catch&#160;</strong>
            </font>
            <font color="#000000">(</font>
            <font color="#000000">Exception&#160;e</font>
            <font color="#000000">)&#160;{</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">e.printStackTrace</font>
            <font color="#000000">()</font>
            <font color="#000000">;</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#ffffff">
            </font>
            <br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font>
            <font color="#000000">}</font>
            <br />
            <font color="#000000">}</font>
            </code>
            </td>
            <!-- end source code -->
        </tr>
    </tbody>
</table>
</div>
<!-- =       END of automatically generated HTML code       = -->
<!-- ======================================================== -->
<img src ="http://www.blogjava.net/sinpo/aggbug/235553.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sinpo/" target="_blank">徐辛波</a> 2008-10-20 22:38 <a href="http://www.blogjava.net/sinpo/archive/2008/10/20/235553.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>模式学习-基于接口的模式</title><link>http://www.blogjava.net/sinpo/archive/2008/10/20/235541.html</link><dc:creator>徐辛波</dc:creator><author>徐辛波</author><pubDate>Mon, 20 Oct 2008 13:48:00 GMT</pubDate><guid>http://www.blogjava.net/sinpo/archive/2008/10/20/235541.html</guid><wfw:comment>http://www.blogjava.net/sinpo/comments/235541.html</wfw:comment><comments>http://www.blogjava.net/sinpo/archive/2008/10/20/235541.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sinpo/comments/commentRss/235541.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sinpo/services/trackbacks/235541.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.blogjava.net/sinpo/archive/2008/10/20/235541.html'>阅读全文</a><img src ="http://www.blogjava.net/sinpo/aggbug/235541.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sinpo/" target="_blank">徐辛波</a> 2008-10-20 21:48 <a href="http://www.blogjava.net/sinpo/archive/2008/10/20/235541.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用ClassLoader读取资源文件</title><link>http://www.blogjava.net/sinpo/archive/2008/10/19/235328.html</link><dc:creator>徐辛波</dc:creator><author>徐辛波</author><pubDate>Sun, 19 Oct 2008 12:36:00 GMT</pubDate><guid>http://www.blogjava.net/sinpo/archive/2008/10/19/235328.html</guid><wfw:comment>http://www.blogjava.net/sinpo/comments/235328.html</wfw:comment><comments>http://www.blogjava.net/sinpo/archive/2008/10/19/235328.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sinpo/comments/commentRss/235328.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sinpo/services/trackbacks/235328.html</trackback:ping><description><![CDATA[关于读取资源文件（如文本文件、图像、二进制文件等），一般不推荐直接给出操作系统的路径，而是给出相对于当前类的相对路径，这样就可以使用类的装载器来装载资源文件。常用的方法有：
<div><strong>Class类的getResourceAsStream(String resourcePath);</strong></div>
<div><strong>ClassLoader类的getResourceAsStream(String resourcePath)</strong></div>
<div>Class类的该方法最终还是委派给ClassLoader的getResourceAsStream方法，但是使用中发现Class#getResourceAsStream()使用的是绝对路径（以/开头），而ClassLoader#getResourceAsStream()使用的相对路径。
</div>
<div>propterty文件经常放在类路径的根路径下（最顶层包的上层目录，如classes），这样加载property文件时就可以先用Class#getResourceAsStream方法获取输入源，再从该输入源load各entry。</div>
code piece:<br />
<div align="left" class="java">
<table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff">
    <tbody>
        <tr>
            <!-- start source code -->
            <td nowrap="nowrap" valign="top" align="left"><code><font color="#7f0055"><strong>package&#160;</strong></font><font color="#000000">sinpo.usagedemo;</font><br />
            <font color="#ffffff"></font><br />
            <font color="#7f0055"><strong>import&#160;</strong></font><font color="#000000">java.io.BufferedReader;</font><br />
            <font color="#7f0055"><strong>import&#160;</strong></font><font color="#000000">java.io.InputStream;</font><br />
            <font color="#7f0055"><strong>import&#160;</strong></font><font color="#000000">java.io.InputStreamReader;</font><br />
            <font color="#7f0055"><strong>import&#160;</strong></font><font color="#000000">java.util.Properties;</font><br />
            <font color="#ffffff"></font><br />
            <font color="#7f0055"><strong>import&#160;</strong></font><font color="#000000">junit.framework.TestCase;</font><br />
            <font color="#ffffff"></font><br />
            <font color="#3f5fbf">/**</font><br />
            <font color="#ffffff">&#160;</font><font color="#3f5fbf">*&#160;</font><font color="#7f9fbf">@author&#160;</font><font color="#3f5fbf">徐辛波(sinpo.xu@hotmail.com)</font><br />
            <font color="#ffffff">&#160;</font><font color="#3f5fbf">*&#160;Oct&#160;19,&#160;2008</font><br />
            <font color="#ffffff">&#160;</font><font color="#3f5fbf">*/</font><br />
            <font color="#7f0055"><strong>public&#160;class&#160;</strong></font><font color="#000000">LoadResource&#160;</font><font color="#7f0055"><strong>extends&#160;</strong></font><font color="#000000">TestCase&#160;</font><font color="#000000">{</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font><font color="#7f0055"><strong>public&#160;</strong></font><font color="#7f0055"><strong>void&#160;</strong></font><font color="#000000">test</font><font color="#000000">()&#160;</font><font color="#7f0055"><strong>throws&#160;</strong></font><font color="#000000">Exception&#160;</font><font color="#000000">{</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#3f7f5f">//usage&#160;1：&#160;use&#160;absolute&#160;path&#160;(mostly&#160;used)</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">InputStream&#160;in1&#160;=&#160;</font><font color="#7f0055"><strong>this</strong></font><font color="#000000">.getClass</font><font color="#000000">()</font><font color="#000000">.getResourceAsStream</font><font color="#000000">(</font><font color="#2a00ff">"/sinpo/test2.properties"</font><font color="#000000">)</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#3f7f5f">//usage&#160;2:&#160;use&#160;relative&#160;path</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">InputStream&#160;in2&#160;=&#160;</font><font color="#7f0055"><strong>this</strong></font><font color="#000000">.getClass</font><font color="#000000">()</font><font color="#000000">.getClassLoader</font><font color="#000000">()</font><font color="#000000">.getResourceAsStream</font><font color="#000000">(</font><font color="#2a00ff">"sinpo/test2.properties"</font><font color="#000000">)</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#3f7f5f">//usage&#160;3:&#160;use&#160;system&#160;class&#160;path</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">InputStream&#160;in3&#160;=&#160;ClassLoader.getSystemResourceAsStream</font><font color="#000000">(</font><font color="#2a00ff">"system.properties"</font><font color="#000000">)</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#3f7f5f">//将读取的资源作为Properties的输入源</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">Properties&#160;props&#160;=&#160;</font><font color="#7f0055"><strong>new&#160;</strong></font><font color="#000000">Properties</font><font color="#000000">()</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">props.load</font><font color="#000000">(</font><font color="#000000">in1</font><font color="#000000">)</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">String&#160;propValue&#160;=&#160;props.getProperty</font><font color="#000000">(</font><font color="#2a00ff">"propKey"</font><font color="#000000">)</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#000000">propValue</font><font color="#000000">)</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#3f7f5f">//将读取的资源作为文本输出</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">InputStreamReader&#160;reader&#160;=&#160;</font><font color="#7f0055"><strong>new&#160;</strong></font><font color="#000000">InputStreamReader</font><font color="#000000">(</font><font color="#000000">in1</font><font color="#000000">)</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">BufferedReader&#160;bReader&#160;=&#160;</font><font color="#7f0055"><strong>new&#160;</strong></font><font color="#000000">BufferedReader</font><font color="#000000">(</font><font color="#000000">reader</font><font color="#000000">)</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">String&#160;content&#160;=&#160;bReader.readLine</font><font color="#000000">()</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#3f7f5f">//输出第一行内容</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#000000">System.out.println</font><font color="#000000">(</font><font color="#000000">content</font><font color="#000000">)</font><font color="#000000">;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</font><font color="#3f7f5f">//TODO&#160;close&#160;them</font><br />
            <font color="#ffffff">&#160;&#160;&#160;&#160;</font><font color="#000000">}</font><br />
            <font color="#000000">}</font></code></td>
            <!-- end source code -->
        </tr>
    </tbody>
</table>
</div>
<img src ="http://www.blogjava.net/sinpo/aggbug/235328.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sinpo/" target="_blank">徐辛波</a> 2008-10-19 20:36 <a href="http://www.blogjava.net/sinpo/archive/2008/10/19/235328.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>