<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>布谷屋</title>
	<atom:link href="http://www.buguw.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.buguw.com</link>
	<description>软件开发&#124;IT技术探讨&#124;互联网前沿</description>
	<lastBuildDate>Tue, 17 Apr 2012 03:47:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>JAVA使用XFire开发Web Service客户端几种调用方式</title>
		<link>http://www.buguw.com/java%e4%bd%bf%e7%94%a8xfire%e5%bc%80%e5%8f%91web-service%e5%ae%a2%e6%88%b7%e7%ab%af%e5%87%a0%e7%a7%8d%e8%b0%83%e7%94%a8%e6%96%b9%e5%bc%8f.html</link>
		<comments>http://www.buguw.com/java%e4%bd%bf%e7%94%a8xfire%e5%bc%80%e5%8f%91web-service%e5%ae%a2%e6%88%b7%e7%ab%af%e5%87%a0%e7%a7%8d%e8%b0%83%e7%94%a8%e6%96%b9%e5%bc%8f.html#comments</comments>
		<pubDate>Tue, 17 Apr 2012 03:47:34 +0000</pubDate>
		<dc:creator>tiger7456</dc:creator>
				<category><![CDATA[Java天地]]></category>
		<category><![CDATA[XFire]]></category>

		<guid isPermaLink="false">http://www.buguw.com/?p=314</guid>
		<description><![CDATA[一、服务提供者告诉你interface，你可以使用如下三种方式来开发： YourService即是服务提供者告诉给你的一个interface(当然，也可以根据WSDL的定义，自己定义一个同样的interface)。 1、简单的方式 Java语言: Codee#26116 1 Service serviceModel = new ObjectServiceFactory().create(YourService.class); 2 YourService service = (YourService)new XFireProxyFactory().create(serviceModel, http://your/remote/url); 2、JSR 181注释的方式 Java语言: Codee#26117 1 Service serviceModel = new AnnotationServiceFactory().create(YourService.class); 2 YourService client = (YourService)new XFireProxyFactory().create(serviceModel, &#34;http://your/remote/url&#34;); 3、混合方式 Java语言: Codee#26118 1 Service serviceModel = new AnnotationServiceFactory(new Jsr181WebAnnotations(), XFireFactory.newInstance().getXFire().getTransportManager(), new AegisBindingProvider(new JaxbTypeRegistry())).create(YourService.class); &#160; 二、通过WSDL创建一个动态的客户端，如下： Java语言: Codee#26119 01 import [...]]]></description>
			<content:encoded><![CDATA[<p>一、服务提供者告诉你interface，你可以使用如下三种方式来开发：    <br />YourService即是服务提供者告诉给你的一个interface(当然，也可以根据WSDL的定义，自己定义一个同样的interface)。 </p>
<p>1、简单的方式</p>
<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26116/">Codee#26116</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">1</span> <span style="color: rgb(0,0,0)">Service</span> <span style="color: rgb(0,0,0)">serviceModel</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">ObjectServiceFactory</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">create</span>(<span style="color: rgb(0,0,0)">YourService</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">class</span>);     <br /><span style="color: rgb(0,128,0)">2</span> <span style="color: rgb(0,0,0)">YourService</span> <span style="color: rgb(0,0,0)">service</span> <span style="color: rgb(0,0,0)">=</span> (<span style="color: rgb(0,0,0)">YourService</span>)<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">XFireProxyFactory</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">create</span>(<span style="color: rgb(0,0,0)">serviceModel</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)"><a href="http://your/remote/url">http://your/remote/url</a></span>);     </div>
<p>2、JSR 181注释的方式</p>
<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26117/">Codee#26117</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">1</span> <span style="color: rgb(0,0,0)">Service</span> <span style="color: rgb(0,0,0)">serviceModel</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">AnnotationServiceFactory</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">create</span>(<span style="color: rgb(0,0,0)">YourService</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">class</span>);     <br /><span style="color: rgb(0,128,0)">2</span> <span style="color: rgb(0,0,0)">YourService</span> <span style="color: rgb(0,0,0)">client</span> <span style="color: rgb(0,0,0)">=</span> (<span style="color: rgb(0,0,0)">YourService</span>)<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">XFireProxyFactory</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">create</span>(<span style="color: rgb(0,0,0)">serviceModel</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;http://your/remote/url&quot;</span>);     </div>
<p>3、混合方式</p>
<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26118/">Codee#26118</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">1</span> <span style="color: rgb(0,0,0)">Service</span> <span style="color: rgb(0,0,0)">serviceModel</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">AnnotationServiceFactory</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Jsr181WebAnnotations</span><span style="color: rgb(0,0,0)">(),</span> <span style="color: rgb(0,0,0)">XFireFactory</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">newInstance</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">getXFire</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">getTransportManager</span><span style="color: rgb(0,0,0)">(),</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">AegisBindingProvider</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">JaxbTypeRegistry</span><span style="color: rgb(0,0,0)">())).</span><span style="color: rgb(0,0,0)">create</span>(<span style="color: rgb(0,0,0)">YourService</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">class</span>);     </div>
<p>&#160;</p>
<p>二、通过WSDL创建一个动态的客户端，如下：</p>
<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26119/">Codee#26119</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">01</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.net.MalformedURLException</span>;     <br /><span style="color: rgb(0,128,0)">02</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.net.URL</span>;     <br /><span style="color: rgb(0,128,0)">03</span>     <br /><span style="color: rgb(0,128,0)">04</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">org.codehaus.xfire.client.Client</span>;     <br /><span style="color: rgb(248,16,176)">05</span>     <br /><span style="color: rgb(0,128,0)">06</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">DynamicClientTest</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">07</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">main</span>(<span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">MalformedURLException</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">Exception</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">08</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">Client</span> <span style="color: rgb(0,0,0)">client</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Client</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">URL</span>(<span style="color: rgb(163,21,21)">&quot;http://localhost:8080/xfiretest/services/TestService?wsdl&quot;</span>));     <br /><span style="color: rgb(0,128,0)">09</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">Object</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">results</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">client</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">invoke</span>(<span style="color: rgb(163,21,21)">&quot;sayHello&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Object</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">{</span> <span style="color: rgb(163,21,21)">&quot;Firends&quot;</span> <span style="color: rgb(0,0,0)">});</span>     <br /><span style="color: rgb(248,16,176)">10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(0,0,0)">results</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">]);</span>     <br /><span style="color: rgb(0,128,0)">11</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">12</span> <span style="color: rgb(0,0,0)">}</span>     </div>
<p>三，使用ANT工具或命令行通过WSDL生成一个客户端：    <br />1、使用ANT生成客户端，ANT脚本如下：</p>
<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26120/">Codee#26120</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">01</span> <span style="color: rgb(0,0,0)">&lt;?</span><span style="color: rgb(0,0,0)">xml</span> <span style="color: rgb(0,0,0)">version</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;1.0&quot;</span><span style="color: rgb(0,0,0)">?&gt;</span>     <br /><span style="color: rgb(0,128,0)">02</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">project</span> <span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;wsgen&quot;</span> <span style="color: rgb(0,0,255)">default</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;wsgen&quot;</span> <span style="color: rgb(0,0,0)">basedir</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;.&quot;</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">03</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">path</span> <span style="color: rgb(0,0,0)">id</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;classpathId&quot;</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">04</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">fileset</span> <span style="color: rgb(0,0,0)">dir</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;./WebRoot/WEB-INF/lib&quot;</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(248,16,176)">05</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">include</span> <span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;*.jar&quot;</span> <span style="color: rgb(0,0,0)">/&gt;</span>     <br /><span style="color: rgb(0,128,0)">06</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">fileset</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">07</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">path</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">08</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">taskdef</span> <span style="color: rgb(0,0,0)">classpathref</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;classpathId&quot;</span> <span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;wsgen&quot;</span> <span style="color: rgb(0,0,0)">classname</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;org.codehaus.xfire.gen.WsGenTask&quot;</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">09</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">taskdef</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(248,16,176)">10</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">target</span> <span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;wsgen&quot;</span> <span style="color: rgb(0,0,0)">description</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;generate client&quot;</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">wsgen</span> <span style="color: rgb(0,0,0)">outputDirectory</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;./src/&quot;</span> <span style="color: rgb(0,0,0)">wsdl</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;abc.wsdl&quot;</span> <span style="color: rgb(0,0,0)">binding</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;xmlbeans&quot;</span> <span style="color: rgb(0,0,0)">package</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;com.abc.p&quot;</span> <span style="color: rgb(0,0,0)">overwrite</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;true&quot;</span> <span style="color: rgb(0,0,0)">/&gt;</span>     <br /><span style="color: rgb(0,128,0)">12</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">target</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">13</span> <span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">project</span><span style="color: rgb(0,0,0)">&gt;</span>     </div>
<p>请注意，脚本中有一个参数binding，可以指定如下两种不同的方式：    <br />(1)jaxb(Java Architecture for XML Binding，<a href="https://jaxb.dev.java.net/">https://jaxb.dev.java.net/</a>)：使用此种方式时，会自动生成更多的Request和Resopnse类。     <br />(2)xmlbeans</p>
<p>调用方式如下：</p>
<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26121/">Codee#26121</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">1</span> <span style="color: rgb(0,0,0)">AbcServiceClient</span> <span style="color: rgb(0,0,0)">client</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">AbcServiceClient</span>();     <br /><span style="color: rgb(0,128,0)">2</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">url</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(163,21,21)">&quot;http://localhost:8080/xfireTest/services/TestService&quot;</span>;     <br /><span style="color: rgb(0,128,0)">3</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">result</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">client</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getAbcPort</span>(<span style="color: rgb(0,0,0)">url</span><span style="color: rgb(0,0,0)">).</span><span style="color: rgb(0,0,0)">sayHello</span>(<span style="color: rgb(163,21,21)">&quot;Robin&quot;</span>);     </div>
<p>2、使用命令生成客户端的命令如下：</p>
<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26122/">Codee#26122</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">1</span> <span style="color: rgb(0,0,0)">gpath</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">xfire</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">all</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1.2</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">SNAPSHOT</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">ant</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1.6</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">5</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">jaxb</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">api</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">2.0</span><span style="color: rgb(0,0,0)">EA3</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">stax</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">api</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1.0</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">1</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">jdom</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1.0</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">jaxb</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">impl</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">2.0</span><span style="color: rgb(0,0,0)">EA3</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="border-bottom: rgb(255,0,0) 1px solid; border-left: rgb(255,0,0) 1px solid; border-top: rgb(255,0,0) 1px solid; border-right: rgb(255,0,0) 1px solid">\</span>    <br /><span style="color: rgb(0,128,0)">2</span> <span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">jaxb</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">xjc</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">2.0</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">ea3</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">wstx</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">asl</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">2.9</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">3</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">commons</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">logging</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1.0</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">4</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">activation</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1.1</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">wsdl4j</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1.5</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">2</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">XmlSchema</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1.0</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">3</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,0,0)">xfire</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">jsr181</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">api</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1.0</span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">M1</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">jar</span>;    <br /><span style="color: rgb(0,128,0)">3</span>     <br /><span style="color: rgb(0,128,0)">4</span> <span style="color: rgb(0,0,0)">java</span> <span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">cp</span> <span style="color: rgb(0,0,0)">$gpath</span> <span style="color: rgb(0,0,0)">org</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">codehaus</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">xfire</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">gen</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">WsGen</span> <span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">wsdl</span> <span style="color: rgb(0,0,0)">http:</span><span style="color: rgb(0,128,0)">//localhost:8080/xfire/services/Bookservice?wsdl -o . -p pl.tomeks.client -overwrite true</span></div>
<p><span style="color: rgb(0,128,0)"></span></p>
<p>其结果与ANT生成的一样。   <br />四、参考资源：    <br />1、XFire 1.2.6手册(<a href="http://xfire.codehaus.org/User%27s+Guide">http://xfire.codehaus.org/User%27s+Guide</a>)    <br />2、<a href="http://xfire.codehaus.org/Client+API">http://xfire.codehaus.org/Client+API</a>    <br />3、<a href="http://xfire.codehaus.org/Dynamic+Client">http://xfire.codehaus.org/Dynamic+Client</a></p>
<p>&#160;</p>
<p>转至：<a title="http://my.oschina.net/lovedreamland/blog/52977" href="http://my.oschina.net/lovedreamland/blog/52977">http://my.oschina.net/lovedreamland/blog/52977</a></p>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"></div>
]]></content:encoded>
			<wfw:commentRss>http://www.buguw.com/java%e4%bd%bf%e7%94%a8xfire%e5%bc%80%e5%8f%91web-service%e5%ae%a2%e6%88%b7%e7%ab%af%e5%87%a0%e7%a7%8d%e8%b0%83%e7%94%a8%e6%96%b9%e5%bc%8f.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>20个常用的Java程序块</title>
		<link>http://www.buguw.com/20%e4%b8%aa%e5%b8%b8%e7%94%a8%e7%9a%84java%e7%a8%8b%e5%ba%8f%e5%9d%97.html</link>
		<comments>http://www.buguw.com/20%e4%b8%aa%e5%b8%b8%e7%94%a8%e7%9a%84java%e7%a8%8b%e5%ba%8f%e5%9d%97.html#comments</comments>
		<pubDate>Tue, 17 Apr 2012 03:37:28 +0000</pubDate>
		<dc:creator>tiger7456</dc:creator>
				<category><![CDATA[Java天地]]></category>

		<guid isPermaLink="false">http://www.buguw.com/?p=312</guid>
		<description><![CDATA[Java语言: Codee#26115 001 //1. 字符串有整型的相互转换 002 String a = String.valueOf(2); //integer to numeric string 003 int i = Integer.parseInt(a); //numeric string to an int 004 005 //2. 向文件末尾添加内容 006 BufferedWriter out = null; 007 try { 008 out = new BufferedWriter(new FileWriter(”filename”, true)); 009 out.write(”aString”); 010 } catch (IOException e) { 011 // error processing [...]]]></description>
			<content:encoded><![CDATA[<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26115/">Codee#26115</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">001</span> <span style="color: rgb(0,128,0)">//1. 字符串有整型的相互转换 </span>    <br /><span style="color: rgb(0,128,0)">002</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">a</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">valueOf</span>(<span style="color: rgb(0,0,0)">2</span>); <span style="color: rgb(0,128,0)">//integer to numeric string </span>    <br /><span style="color: rgb(0,128,0)">003</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">i</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">Integer</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">parseInt</span>(<span style="color: rgb(0,0,0)">a</span>); <span style="color: rgb(0,128,0)">//numeric string to an int</span>    <br /><span style="color: rgb(0,128,0)">004</span>     <br /><span style="color: rgb(248,16,176)">005</span> <span style="color: rgb(0,128,0)">//2. 向文件末尾添加内容 </span>    <br /><span style="color: rgb(0,128,0)">006</span> <span style="color: rgb(0,0,0)">BufferedWriter</span> <span style="color: rgb(0,0,0)">out</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">null</span>;     <br /><span style="color: rgb(0,128,0)">007</span> <span style="color: rgb(0,0,255)">try</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">008</span> <span style="color: rgb(0,0,0)">out</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">BufferedWriter</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">FileWriter</span>(<span style="border-bottom: rgb(255,0,0) 1px solid; border-left: rgb(255,0,0) 1px solid; border-top: rgb(255,0,0) 1px solid; border-right: rgb(255,0,0) 1px solid">”</span><span style="color: rgb(0,0,0)">filename</span><span style="border-bottom: rgb(255,0,0) 1px solid; border-left: rgb(255,0,0) 1px solid; border-top: rgb(255,0,0) 1px solid; border-right: rgb(255,0,0) 1px solid">”</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,255)">true</span>));     <br /><span style="color: rgb(0,128,0)">009</span> <span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">write</span>(<span style="border-bottom: rgb(255,0,0) 1px solid; border-left: rgb(255,0,0) 1px solid; border-top: rgb(255,0,0) 1px solid; border-right: rgb(255,0,0) 1px solid">”</span><span style="color: rgb(0,0,0)">aString</span><span style="border-bottom: rgb(255,0,0) 1px solid; border-left: rgb(255,0,0) 1px solid; border-top: rgb(255,0,0) 1px solid; border-right: rgb(255,0,0) 1px solid">”</span>);     <br /><span style="color: rgb(248,16,176)">010</span> <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">catch</span> (<span style="color: rgb(0,0,0)">IOException</span> <span style="color: rgb(0,0,0)">e</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">011</span> <span style="color: rgb(0,128,0)">// error processing code</span>    <br /><span style="color: rgb(0,128,0)">012</span>     <br /><span style="color: rgb(0,128,0)">013</span> <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">finally</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">014</span> <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">out</span> <span style="color: rgb(0,0,0)">!=</span> <span style="color: rgb(0,0,255)">null</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">015</span> <span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();     <br /><span style="color: rgb(0,128,0)">016</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">017</span>     <br /><span style="color: rgb(0,128,0)">018</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">019</span>     <br /><span style="color: rgb(248,16,176)">020</span> <span style="color: rgb(0,128,0)">//3. 得到当前方法的名字 </span>    <br /><span style="color: rgb(0,128,0)">021</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">methodName</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">Thread</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">currentThread</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">getStackTrace</span><span style="color: rgb(0,0,0)">()[</span><span style="color: rgb(0,0,0)">1</span><span style="color: rgb(0,0,0)">].</span><span style="color: rgb(0,0,0)">getMethodName</span>();    <br /><span style="color: rgb(0,128,0)">022</span>     <br /><span style="color: rgb(0,128,0)">023</span> <span style="color: rgb(0,128,0)">//4. 转字符串到日期 </span>    <br /><span style="color: rgb(0,128,0)">024</span> <span style="color: rgb(0,0,0)">java</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">util</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">Date</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">java</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">text</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">DateFormat</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getDateInstance</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">parse</span>(<span style="color: rgb(0,0,0)">date</span> <span style="color: rgb(0,0,0)">String</span>);     <br /><span style="color: rgb(248,16,176)">025</span> <span style="color: rgb(0,128,0)">//或者是： </span>    <br /><span style="color: rgb(0,128,0)">026</span> <span style="color: rgb(0,0,0)">SimpleDateFormat</span> <span style="color: rgb(0,0,0)">format</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">SimpleDateFormat</span>( <span style="color: rgb(163,21,21)">&quot;dd.MM.yyyy&quot;</span> );     <br /><span style="color: rgb(0,128,0)">027</span> <span style="color: rgb(0,0,0)">Date</span> <span style="color: rgb(0,0,0)">date</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">format</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">parse</span>( <span style="color: rgb(0,0,0)">myString</span> );    <br /><span style="color: rgb(0,128,0)">028</span>     <br /><span style="color: rgb(0,128,0)">029</span> <span style="color: rgb(0,128,0)">//5. 使用JDBC链接Oracle </span>    <br /><span style="color: rgb(248,16,176)">030</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">OracleJdbcTest</span>     <br /><span style="color: rgb(0,128,0)">031</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">032</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">driverClass</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(163,21,21)">&quot;oracle.jdbc.driver.OracleDriver&quot;</span>;    <br /><span style="color: rgb(0,128,0)">033</span>     <br /><span style="color: rgb(0,128,0)">034</span> <span style="color: rgb(0,0,0)">Connection</span> <span style="color: rgb(0,0,0)">con</span>;    <br /><span style="color: rgb(248,16,176)">035</span>     <br /><span style="color: rgb(0,128,0)">036</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">init</span>(<span style="color: rgb(0,0,0)">FileInputStream</span> <span style="color: rgb(0,0,0)">fs</span>) <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">ClassNotFoundException</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">SQLException</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">FileNotFoundException</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">IOException</span>     <br /><span style="color: rgb(0,128,0)">037</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">038</span> <span style="color: rgb(0,0,0)">Properties</span> <span style="color: rgb(0,0,0)">props</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Properties</span>();     <br /><span style="color: rgb(0,128,0)">039</span> <span style="color: rgb(0,0,0)">props</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">load</span>(<span style="color: rgb(0,0,0)">fs</span>);     <br /><span style="color: rgb(248,16,176)">040</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">url</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">props</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getProperty</span>(<span style="color: rgb(163,21,21)">&quot;db.url&quot;</span>);     <br /><span style="color: rgb(0,128,0)">041</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">userName</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">props</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getProperty</span>(<span style="color: rgb(163,21,21)">&quot;db.user&quot;</span>);     <br /><span style="color: rgb(0,128,0)">042</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">password</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">props</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getProperty</span>(<span style="color: rgb(163,21,21)">&quot;db.password&quot;</span>);     <br /><span style="color: rgb(0,128,0)">043</span> <span style="color: rgb(0,0,0)">Class</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">forName</span>(<span style="color: rgb(0,0,0)">driverClass</span>);    <br /><span style="color: rgb(0,128,0)">044</span>     <br /><span style="color: rgb(248,16,176)">045</span> <span style="color: rgb(0,0,0)">con</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">DriverManager</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getConnection</span>(<span style="color: rgb(0,0,0)">url</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">userName</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">password</span>);     <br /><span style="color: rgb(0,128,0)">046</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">047</span>     <br /><span style="color: rgb(0,128,0)">048</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">fetch</span>() <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">SQLException</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">IOException</span>     <br /><span style="color: rgb(0,128,0)">049</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">050</span> <span style="color: rgb(0,0,0)">PreparedStatement</span> ps <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">con</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">prepareStatement</span>(<span style="color: rgb(163,21,21)">&quot;select SYSDATE from dual&quot;</span>);     <br /><span style="color: rgb(0,128,0)">051</span> <span style="color: rgb(0,0,0)">ResultSet</span> <span style="color: rgb(0,0,0)">rs</span> <span style="color: rgb(0,0,0)">=</span> ps<span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">executeQuery</span>();    <br /><span style="color: rgb(0,128,0)">052</span>     <br /><span style="color: rgb(0,128,0)">053</span> <span style="color: rgb(0,0,255)">while</span> (<span style="color: rgb(0,0,0)">rs</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">next</span>())     <br /><span style="color: rgb(0,128,0)">054</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">055</span> <span style="color: rgb(0,128,0)">// do the thing you do </span>    <br /><span style="color: rgb(0,128,0)">056</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">057</span> <span style="color: rgb(0,0,0)">rs</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();     <br /><span style="color: rgb(0,128,0)">058</span> ps<span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();     <br /><span style="color: rgb(0,128,0)">059</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(248,16,176)">060</span>     <br /><span style="color: rgb(0,128,0)">061</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">main</span>(<span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>)     <br /><span style="color: rgb(0,128,0)">062</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">063</span> <span style="color: rgb(0,0,0)">OracleJdbcTest</span> <span style="color: rgb(0,0,0)">test</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">OracleJdbcTest</span>();     <br /><span style="color: rgb(0,128,0)">064</span> <span style="color: rgb(0,0,0)">test</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">init</span>();     <br /><span style="color: rgb(248,16,176)">065</span> <span style="color: rgb(0,0,0)">test</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">fetch</span>();     <br /><span style="color: rgb(0,128,0)">066</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">067</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">068</span>     <br /><span style="color: rgb(0,128,0)">069</span> <span style="color: rgb(0,0,0)">6</span><span style="color: rgb(0,0,0)">.</span> <span style="border-bottom: rgb(255,0,0) 1px solid; border-left: rgb(255,0,0) 1px solid; border-top: rgb(255,0,0) 1px solid; border-right: rgb(255,0,0) 1px solid">把</span> <span style="color: rgb(0,0,0)">Java</span> <span style="color: rgb(0,0,0)">util</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">Date</span> <span style="border-bottom: rgb(255,0,0) 1px solid; border-left: rgb(255,0,0) 1px solid; border-top: rgb(255,0,0) 1px solid; border-right: rgb(255,0,0) 1px solid">转成</span> <span style="color: rgb(0,0,0)">sql</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">Date</span>     <br /><span style="color: rgb(248,16,176)">070</span> <span style="color: rgb(0,0,0)">java</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">util</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">Date</span> <span style="color: rgb(0,0,0)">utilDate</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">java</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">util</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">Date</span>();     <br /><span style="color: rgb(0,128,0)">071</span> <span style="color: rgb(0,0,0)">java</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">sql</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">Date</span> <span style="color: rgb(0,0,0)">sqlDate</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">java</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">sql</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">Date</span>(<span style="color: rgb(0,0,0)">utilDate</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getTime</span>());    <br /><span style="color: rgb(0,128,0)">072</span>     <br /><span style="color: rgb(0,128,0)">073</span> <span style="color: rgb(0,128,0)">//7. 使用NIO进行快速的文件拷贝 </span>    <br /><span style="color: rgb(0,128,0)">074</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">fileCopy</span>( <span style="color: rgb(0,0,0)">File</span> <span style="color: rgb(0,0,0)">in</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">File</span> <span style="color: rgb(0,0,0)">out</span> )     <br /><span style="color: rgb(248,16,176)">075</span> <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">IOException</span>     <br /><span style="color: rgb(0,128,0)">076</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">077</span> <span style="color: rgb(0,0,0)">FileChannel</span> <span style="color: rgb(0,0,0)">inChannel</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">FileInputStream</span>( <span style="color: rgb(0,0,0)">in</span> <span style="color: rgb(0,0,0)">).</span><span style="color: rgb(0,0,0)">getChannel</span>();     <br /><span style="color: rgb(0,128,0)">078</span> <span style="color: rgb(0,0,0)">FileChannel</span> <span style="color: rgb(0,0,0)">outChannel</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">FileOutputStream</span>( <span style="color: rgb(0,0,0)">out</span> <span style="color: rgb(0,0,0)">).</span><span style="color: rgb(0,0,0)">getChannel</span>();     <br /><span style="color: rgb(0,128,0)">079</span> <span style="color: rgb(0,0,255)">try</span>     <br /><span style="color: rgb(248,16,176)">080</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">081</span> <span style="color: rgb(0,128,0)">// inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows</span>    <br /><span style="color: rgb(0,128,0)">082</span>     <br /><span style="color: rgb(0,128,0)">083</span> <span style="color: rgb(0,128,0)">// magic number for Windows, 64Mb - 32Kb) </span>    <br /><span style="color: rgb(0,128,0)">084</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">maxCount</span> <span style="color: rgb(0,0,0)">=</span> (<span style="color: rgb(0,0,0)">64</span> <span style="color: rgb(0,0,0)">*</span> <span style="color: rgb(0,0,0)">1024</span> <span style="color: rgb(0,0,0)">*</span> <span style="color: rgb(0,0,0)">1024</span>) <span style="color: rgb(0,0,0)">-</span> (<span style="color: rgb(0,0,0)">32</span> <span style="color: rgb(0,0,0)">*</span> <span style="color: rgb(0,0,0)">1024</span>);     <br /><span style="color: rgb(248,16,176)">085</span> <span style="color: rgb(43,145,175)">long</span> <span style="color: rgb(0,0,0)">size</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">inChannel</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">size</span>();     <br /><span style="color: rgb(0,128,0)">086</span> <span style="color: rgb(43,145,175)">long</span> <span style="color: rgb(0,0,0)">position</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">0</span>;     <br /><span style="color: rgb(0,128,0)">087</span> <span style="color: rgb(0,0,255)">while</span> ( <span style="color: rgb(0,0,0)">position</span> <span style="color: rgb(0,0,0)">&lt;</span> <span style="color: rgb(0,0,0)">size</span> )     <br /><span style="color: rgb(0,128,0)">088</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">089</span> <span style="color: rgb(0,0,0)">position</span> <span style="color: rgb(0,0,0)">+=</span> <span style="color: rgb(0,0,0)">inChannel</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">transferTo</span>( <span style="color: rgb(0,0,0)">position</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">maxCount</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">outChannel</span> );     <br /><span style="color: rgb(248,16,176)">090</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">091</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">092</span> <span style="color: rgb(0,0,255)">finally</span>     <br /><span style="color: rgb(0,128,0)">093</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">094</span> <span style="color: rgb(0,0,255)">if</span> ( <span style="color: rgb(0,0,0)">inChannel</span> <span style="color: rgb(0,0,0)">!=</span> <span style="color: rgb(0,0,255)">null</span> )     <br /><span style="color: rgb(248,16,176)">095</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">096</span> <span style="color: rgb(0,0,0)">inChannel</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();     <br /><span style="color: rgb(0,128,0)">097</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">098</span> <span style="color: rgb(0,0,255)">if</span> ( <span style="color: rgb(0,0,0)">outChannel</span> <span style="color: rgb(0,0,0)">!=</span> <span style="color: rgb(0,0,255)">null</span> )     <br /><span style="color: rgb(0,128,0)">099</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">100</span> <span style="color: rgb(0,0,0)">outChannel</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();     <br /><span style="color: rgb(0,128,0)">101</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">102</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">103</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">104</span>     <br /><span style="color: rgb(248,16,176)">105</span> <span style="color: rgb(0,128,0)">//8. 创建图片的缩略图 </span>    <br /><span style="color: rgb(0,128,0)">106</span> <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">createThumbnail</span>(<span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">filename</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">thumbWidth</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">thumbHeight</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">quality</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">outFilename</span>)     <br /><span style="color: rgb(0,128,0)">107</span> <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">InterruptedException</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">FileNotFoundException</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">IOException</span>     <br /><span style="color: rgb(0,128,0)">108</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">109</span> <span style="color: rgb(0,128,0)">// load image from filename </span>    <br /><span style="color: rgb(248,16,176)">110</span> <span style="color: rgb(0,0,0)">Image</span> <span style="color: rgb(0,0,0)">image</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">Toolkit</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getDefaultToolkit</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">getImage</span>(<span style="color: rgb(0,0,0)">filename</span>);     <br /><span style="color: rgb(0,128,0)">111</span> <span style="color: rgb(0,0,0)">MediaTracker</span> <span style="color: rgb(0,0,0)">mediaTracker</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">MediaTracker</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Container</span>());     <br /><span style="color: rgb(0,128,0)">112</span> <span style="color: rgb(0,0,0)">mediaTracker</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">addImage</span>(<span style="color: rgb(0,0,0)">image</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">0</span>);     <br /><span style="color: rgb(0,128,0)">113</span> <span style="color: rgb(0,0,0)">mediaTracker</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">waitForID</span>(<span style="color: rgb(0,0,0)">0</span>);     <br /><span style="color: rgb(0,128,0)">114</span> <span style="color: rgb(0,128,0)">// use this to test for errors at this point: System.out.println(mediaTracker.isErrorAny());</span>    <br /><span style="color: rgb(248,16,176)">115</span>     <br /><span style="color: rgb(0,128,0)">116</span> <span style="color: rgb(0,128,0)">// determine thumbnail size from WIDTH and HEIGHT </span>    <br /><span style="color: rgb(0,128,0)">117</span> <span style="color: rgb(43,145,175)">double</span> <span style="color: rgb(0,0,0)">thumbRatio</span> <span style="color: rgb(0,0,0)">=</span> (<span style="color: rgb(43,145,175)">double</span>)<span style="color: rgb(0,0,0)">thumbWidth</span> <span style="color: rgb(0,0,0)">/</span> (<span style="color: rgb(43,145,175)">double</span>)<span style="color: rgb(0,0,0)">thumbHeight</span>;     <br /><span style="color: rgb(0,128,0)">118</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">imageWidth</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">image</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getWidth</span>(<span style="color: rgb(0,0,255)">null</span>);     <br /><span style="color: rgb(0,128,0)">119</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">imageHeight</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">image</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getHeight</span>(<span style="color: rgb(0,0,255)">null</span>);     <br /><span style="color: rgb(248,16,176)">120</span> <span style="color: rgb(43,145,175)">double</span> <span style="color: rgb(0,0,0)">imageRatio</span> <span style="color: rgb(0,0,0)">=</span> (<span style="color: rgb(43,145,175)">double</span>)<span style="color: rgb(0,0,0)">imageWidth</span> <span style="color: rgb(0,0,0)">/</span> (<span style="color: rgb(43,145,175)">double</span>)<span style="color: rgb(0,0,0)">imageHeight</span>;     <br /><span style="color: rgb(0,128,0)">121</span> <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">thumbRatio</span> <span style="color: rgb(0,0,0)">&lt;</span> <span style="color: rgb(0,0,0)">imageRatio</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">122</span> <span style="color: rgb(0,0,0)">thumbHeight</span> <span style="color: rgb(0,0,0)">=</span> (<span style="color: rgb(43,145,175)">int</span>)(<span style="color: rgb(0,0,0)">thumbWidth</span> <span style="color: rgb(0,0,0)">/</span> <span style="color: rgb(0,0,0)">imageRatio</span>);     <br /><span style="color: rgb(0,128,0)">123</span> <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">else</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">124</span> <span style="color: rgb(0,0,0)">thumbWidth</span> <span style="color: rgb(0,0,0)">=</span> (<span style="color: rgb(43,145,175)">int</span>)(<span style="color: rgb(0,0,0)">thumbHeight</span> <span style="color: rgb(0,0,0)">*</span> <span style="color: rgb(0,0,0)">imageRatio</span>);     <br /><span style="color: rgb(248,16,176)">125</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">126</span>     <br /><span style="color: rgb(0,128,0)">127</span> <span style="color: rgb(0,128,0)">// draw original image to thumbnail image object and </span>    <br /><span style="color: rgb(0,128,0)">128</span> <span style="color: rgb(0,128,0)">// scale it to the new size on-the-fly </span>    <br /><span style="color: rgb(0,128,0)">129</span> <span style="color: rgb(0,0,0)">BufferedImage</span> <span style="color: rgb(0,0,0)">thumbImage</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">BufferedImage</span>(<span style="color: rgb(0,0,0)">thumbWidth</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">thumbHeight</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">BufferedImage</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">TYPE_INT_RGB</span>);     <br /><span style="color: rgb(248,16,176)">130</span> <span style="color: rgb(0,0,0)">Graphics2D</span> <span style="color: rgb(0,0,0)">graphics2D</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">thumbImage</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">createGraphics</span>();     <br /><span style="color: rgb(0,128,0)">131</span> <span style="color: rgb(0,0,0)">graphics2D</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setRenderingHint</span>(<span style="color: rgb(0,0,0)">RenderingHints</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">KEY_INTERPOLATION</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">RenderingHints</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">VALUE_INTERPOLATION_BILINEAR</span>);     <br /><span style="color: rgb(0,128,0)">132</span> <span style="color: rgb(0,0,0)">graphics2D</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">drawImage</span>(<span style="color: rgb(0,0,0)">image</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">thumbWidth</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">thumbHeight</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,255)">null</span>);    <br /><span style="color: rgb(0,128,0)">133</span>     <br /><span style="color: rgb(0,128,0)">134</span> <span style="color: rgb(0,128,0)">// save thumbnail image to outFilename </span>    <br /><span style="color: rgb(248,16,176)">135</span> <span style="color: rgb(0,0,0)">BufferedOutputStream</span> <span style="color: rgb(0,0,0)">out</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">BufferedOutputStream</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">FileOutputStream</span>(<span style="color: rgb(0,0,0)">outFilename</span>));     <br /><span style="color: rgb(0,128,0)">136</span> <span style="color: rgb(0,0,0)">JPEGImageEncoder</span> <span style="color: rgb(0,0,0)">encoder</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">JPEGCodec</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">createJPEGEncoder</span>(<span style="color: rgb(0,0,0)">out</span>);     <br /><span style="color: rgb(0,128,0)">137</span> <span style="color: rgb(0,0,0)">JPEGEncodeParam</span> <span style="color: rgb(0,0,0)">param</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">encoder</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getDefaultJPEGEncodeParam</span>(<span style="color: rgb(0,0,0)">thumbImage</span>);     <br /><span style="color: rgb(0,128,0)">138</span> <span style="color: rgb(0,0,0)">quality</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">Math</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">max</span>(<span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">Math</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">min</span>(<span style="color: rgb(0,0,0)">quality</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">100</span>));     <br /><span style="color: rgb(0,128,0)">139</span> <span style="color: rgb(0,0,0)">param</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setQuality</span>((<span style="color: rgb(43,145,175)">float</span>)<span style="color: rgb(0,0,0)">quality</span> <span style="color: rgb(0,0,0)">/</span> <span style="color: rgb(0,0,0)">100.0f</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,255)">false</span>);     <br /><span style="color: rgb(248,16,176)">140</span> <span style="color: rgb(0,0,0)">encoder</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setJPEGEncodeParam</span>(<span style="color: rgb(0,0,0)">param</span>);     <br /><span style="color: rgb(0,128,0)">141</span> <span style="color: rgb(0,0,0)">encoder</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">encode</span>(<span style="color: rgb(0,0,0)">thumbImage</span>);     <br /><span style="color: rgb(0,128,0)">142</span> <span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();     <br /><span style="color: rgb(0,128,0)">143</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">144</span>     <br /><span style="color: rgb(248,16,176)">145</span> <span style="color: rgb(0,128,0)">//9. 创建 JSON 格式的数据 </span>    <br /><span style="color: rgb(0,128,0)">146</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">org.json.JSONObject</span>;     <br /><span style="color: rgb(0,128,0)">147</span> <span style="color: rgb(0,0,0)">...</span>     <br /><span style="color: rgb(0,128,0)">148</span> <span style="color: rgb(0,0,0)">...</span>     <br /><span style="color: rgb(0,128,0)">149</span> <span style="color: rgb(0,0,0)">JSONObject</span> <span style="color: rgb(0,0,0)">json</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">JSONObject</span>();     <br /><span style="color: rgb(248,16,176)">150</span> <span style="color: rgb(0,0,0)">json</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">put</span>(<span style="color: rgb(163,21,21)">&quot;city&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;Mumbai&quot;</span>);     <br /><span style="color: rgb(0,128,0)">151</span> <span style="color: rgb(0,0,0)">json</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">put</span>(<span style="color: rgb(163,21,21)">&quot;country&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;India&quot;</span>);     <br /><span style="color: rgb(0,128,0)">152</span> <span style="color: rgb(0,0,0)">...</span>     <br /><span style="color: rgb(0,128,0)">153</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">output</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">json</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">toString</span>();     <br /><span style="color: rgb(0,128,0)">154</span> <span style="color: rgb(0,0,0)">...</span>    <br /><span style="color: rgb(248,16,176)">155</span>     <br /><span style="color: rgb(0,128,0)">156</span> <span style="color: rgb(0,128,0)">//10. 使用iText JAR生成PDF </span>    <br /><span style="color: rgb(0,128,0)">157</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.io.File</span>;     <br /><span style="color: rgb(0,128,0)">158</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.io.FileOutputStream</span>;     <br /><span style="color: rgb(0,128,0)">159</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.io.OutputStream</span>;     <br /><span style="color: rgb(248,16,176)">160</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.util.Date</span>;    <br /><span style="color: rgb(0,128,0)">161</span>     <br /><span style="color: rgb(0,128,0)">162</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">com.lowagie.text.Document</span>;     <br /><span style="color: rgb(0,128,0)">163</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">com.lowagie.text.Paragraph</span>;     <br /><span style="color: rgb(0,128,0)">164</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">com.lowagie.text.pdf.PdfWriter</span>;    <br /><span style="color: rgb(248,16,176)">165</span>     <br /><span style="color: rgb(0,128,0)">166</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">GeneratePDF</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">167</span>     <br /><span style="color: rgb(0,128,0)">168</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">main</span>(<span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">169</span> <span style="color: rgb(0,0,255)">try</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">170</span> <span style="color: rgb(0,0,0)">OutputStream</span> <span style="color: rgb(0,0,0)">file</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">FileOutputStream</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">File</span>(<span style="color: rgb(163,21,21)">&quot;C:\\Test.pdf&quot;</span>));    <br /><span style="color: rgb(0,128,0)">171</span>     <br /><span style="color: rgb(0,128,0)">172</span> <span style="color: rgb(0,0,0)">Document</span> <span style="color: rgb(0,0,0)">document</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Document</span>();     <br /><span style="color: rgb(0,128,0)">173</span> <span style="color: rgb(0,0,0)">PdfWriter</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getInstance</span>(<span style="color: rgb(0,0,0)">document</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">file</span>);     <br /><span style="color: rgb(0,128,0)">174</span> <span style="color: rgb(0,0,0)">document</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">open</span>();     <br /><span style="color: rgb(248,16,176)">175</span> <span style="color: rgb(0,0,0)">document</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">add</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Paragraph</span>(<span style="color: rgb(163,21,21)">&quot;Hello Kiran&quot;</span>));     <br /><span style="color: rgb(0,128,0)">176</span> <span style="color: rgb(0,0,0)">document</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">add</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Paragraph</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Date</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">toString</span>()));    <br /><span style="color: rgb(0,128,0)">177</span>     <br /><span style="color: rgb(0,128,0)">178</span> <span style="color: rgb(0,0,0)">document</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();     <br /><span style="color: rgb(0,128,0)">179</span> <span style="color: rgb(0,0,0)">file</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();    <br /><span style="color: rgb(248,16,176)">180</span>     <br /><span style="color: rgb(0,128,0)">181</span> <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">catch</span> (<span style="color: rgb(0,0,0)">Exception</span> <span style="color: rgb(0,0,0)">e</span>) <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">182</span>     <br /><span style="color: rgb(0,128,0)">183</span> <span style="color: rgb(0,0,0)">e</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">printStackTrace</span>();     <br /><span style="color: rgb(0,128,0)">184</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(248,16,176)">185</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">186</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">187</span>     <br /><span style="color: rgb(0,128,0)">188</span> <span style="color: rgb(0,128,0)">//11. HTTP 代理设置 </span>    <br /><span style="color: rgb(0,128,0)">189</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getProperties</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">put</span>(<span style="color: rgb(163,21,21)">&quot;http.proxyHost&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;someProxyURL&quot;</span>);     <br /><span style="color: rgb(248,16,176)">190</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getProperties</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">put</span>(<span style="color: rgb(163,21,21)">&quot;http.proxyPort&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;someProxyPort&quot;</span>);     <br /><span style="color: rgb(0,128,0)">191</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getProperties</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">put</span>(<span style="color: rgb(163,21,21)">&quot;http.proxyUser&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;someUserName&quot;</span>);     <br /><span style="color: rgb(0,128,0)">192</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getProperties</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">put</span>(<span style="color: rgb(163,21,21)">&quot;http.proxyPassword&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;somePassword&quot;</span>);    <br /><span style="color: rgb(0,128,0)">193</span>     <br /><span style="color: rgb(0,128,0)">194</span> <span style="color: rgb(0,128,0)">//12. 单实例Singleton 示例 </span>    <br /><span style="color: rgb(248,16,176)">195</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">SimpleSingleton</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">196</span> <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,0)">SimpleSingleton</span> <span style="color: rgb(0,0,0)">singleInstance</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">SimpleSingleton</span>();    <br /><span style="color: rgb(0,128,0)">197</span>     <br /><span style="color: rgb(0,128,0)">198</span> <span style="color: rgb(0,128,0)">//Marking default constructor private </span>    <br /><span style="color: rgb(0,128,0)">199</span> <span style="color: rgb(0,128,0)">//to avoid direct instantiation. </span>    <br /><span style="color: rgb(248,16,176)">200</span> <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,0)">SimpleSingleton</span>() <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">201</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">202</span>     <br /><span style="color: rgb(0,128,0)">203</span> <span style="color: rgb(0,128,0)">//Get instance for class SimpleSingleton </span>    <br /><span style="color: rgb(0,128,0)">204</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,0)">SimpleSingleton</span> <span style="color: rgb(0,0,0)">getInstance</span>() <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(248,16,176)">205</span>     <br /><span style="color: rgb(0,128,0)">206</span> <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">singleInstance</span>;     <br /><span style="color: rgb(0,128,0)">207</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">208</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">209</span>     <br /><span style="color: rgb(248,16,176)">210</span> <span style="color: rgb(0,128,0)">//另一种实现</span>    <br /><span style="color: rgb(0,128,0)">211</span>     <br /><span style="color: rgb(0,128,0)">212</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">enum</span> <span style="color: rgb(0,0,0)">SimpleSingleton</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">213</span> <span style="color: rgb(0,0,0)">INSTANCE</span>;     <br /><span style="color: rgb(0,128,0)">214</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">doSomething</span>() <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">215</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">216</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">217</span>     <br /><span style="color: rgb(0,128,0)">218</span> <span style="color: rgb(0,128,0)">//Call the method from Singleton: </span>    <br /><span style="color: rgb(0,128,0)">219</span> <span style="color: rgb(0,0,0)">SimpleSingleton</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">INSTANCE</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">doSomething</span>();    <br /><span style="color: rgb(248,16,176)">220</span>     <br /><span style="color: rgb(0,128,0)">221</span> <span style="color: rgb(0,128,0)">//13. 抓屏程序 </span>    <br /><span style="color: rgb(0,128,0)">222</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.awt.Dimension</span>;     <br /><span style="color: rgb(0,128,0)">223</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.awt.Rectangle</span>;     <br /><span style="color: rgb(0,128,0)">224</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.awt.Robot</span>;     <br /><span style="color: rgb(248,16,176)">225</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.awt.Toolkit</span>;     <br /><span style="color: rgb(0,128,0)">226</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.awt.image.BufferedImage</span>;     <br /><span style="color: rgb(0,128,0)">227</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">javax.imageio.ImageIO</span>;     <br /><span style="color: rgb(0,128,0)">228</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.io.File</span>;    <br /><span style="color: rgb(0,128,0)">229</span>     <br /><span style="color: rgb(248,16,176)">230</span> <span style="color: rgb(0,0,0)">...</span>     <br /><span style="color: rgb(0,128,0)">231</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">captureScreen</span>(<span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">fileName</span>) <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">Exception</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">232</span>     <br /><span style="color: rgb(0,128,0)">233</span> <span style="color: rgb(0,0,0)">Dimension</span> <span style="color: rgb(0,0,0)">screenSize</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">Toolkit</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getDefaultToolkit</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">getScreenSize</span>();     <br /><span style="color: rgb(0,128,0)">234</span> <span style="color: rgb(0,0,0)">Rectangle</span> <span style="color: rgb(0,0,0)">screenRectangle</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Rectangle</span>(<span style="color: rgb(0,0,0)">screenSize</span>);     <br /><span style="color: rgb(248,16,176)">235</span> <span style="color: rgb(0,0,0)">Robot</span> <span style="color: rgb(0,0,0)">robot</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Robot</span>();     <br /><span style="color: rgb(0,128,0)">236</span> <span style="color: rgb(0,0,0)">BufferedImage</span> <span style="color: rgb(0,0,0)">image</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">robot</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">createScreenCapture</span>(<span style="color: rgb(0,0,0)">screenRectangle</span>);     <br /><span style="color: rgb(0,128,0)">237</span> <span style="color: rgb(0,0,0)">ImageIO</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">write</span>(<span style="color: rgb(0,0,0)">image</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;png&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">File</span>(<span style="color: rgb(0,0,0)">fileName</span>));    <br /><span style="color: rgb(0,128,0)">238</span>     <br /><span style="color: rgb(0,128,0)">239</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(248,16,176)">240</span> <span style="color: rgb(0,128,0)">//14. 列出文件和目录 </span>    <br /><span style="color: rgb(0,128,0)">241</span> <span style="color: rgb(0,0,0)">File</span> <span style="color: rgb(0,0,0)">dir</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">File</span>(<span style="color: rgb(163,21,21)">&quot;directoryName&quot;</span>);     <br /><span style="color: rgb(0,128,0)">242</span> <span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">children</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">dir</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">list</span>();     <br /><span style="color: rgb(0,128,0)">243</span> <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">children</span> <span style="color: rgb(0,0,0)">==</span> <span style="color: rgb(0,0,255)">null</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">244</span> <span style="color: rgb(0,128,0)">// Either dir does not exist or is not a directory </span>    <br /><span style="color: rgb(248,16,176)">245</span> <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">else</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">246</span> <span style="color: rgb(0,0,255)">for</span> (<span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">0</span>; <span style="color: rgb(0,0,0)">i</span> <span style="color: rgb(0,0,0)">&lt;</span> <span style="color: rgb(0,0,0)">children</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">length</span>; <span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">++)</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">247</span> <span style="color: rgb(0,128,0)">// Get filename of file or directory </span>    <br /><span style="color: rgb(0,128,0)">248</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">filename</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">children</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">];</span>     <br /><span style="color: rgb(0,128,0)">249</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(248,16,176)">250</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">251</span>     <br /><span style="color: rgb(0,128,0)">252</span> <span style="color: rgb(0,128,0)">// It is also possible to filter the list of returned files. </span>    <br /><span style="color: rgb(0,128,0)">253</span> <span style="color: rgb(0,128,0)">// This example does not return any files that start with `.’. </span>    <br /><span style="color: rgb(0,128,0)">254</span> <span style="color: rgb(0,0,0)">FilenameFilter</span> <span style="color: rgb(0,0,0)">filter</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">FilenameFilter</span>() <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">255</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(43,145,175)">boolean</span> <span style="color: rgb(0,0,0)">accept</span>(<span style="color: rgb(0,0,0)">File</span> <span style="color: rgb(0,0,0)">dir</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">name</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">256</span> <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">startsWith</span>(<span style="color: rgb(163,21,21)">&quot;.&quot;</span>);     <br /><span style="color: rgb(0,128,0)">257</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">258</span> <span style="color: rgb(0,0,0)">};</span>     <br /><span style="color: rgb(0,128,0)">259</span> <span style="color: rgb(0,0,0)">children</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">dir</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">list</span>(<span style="color: rgb(0,0,0)">filter</span>);    <br /><span style="color: rgb(248,16,176)">260</span>     <br /><span style="color: rgb(0,128,0)">261</span> <span style="color: rgb(0,128,0)">// The list of files can also be retrieved as File objects </span>    <br /><span style="color: rgb(0,128,0)">262</span> <span style="color: rgb(0,0,0)">File</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">files</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">dir</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">listFiles</span>();    <br /><span style="color: rgb(0,128,0)">263</span>     <br /><span style="color: rgb(0,128,0)">264</span> <span style="color: rgb(0,128,0)">// This filter only returns directories </span>    <br /><span style="color: rgb(248,16,176)">265</span> <span style="color: rgb(0,0,0)">FileFilter</span> <span style="color: rgb(0,0,0)">fileFilter</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">FileFilter</span>() <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">266</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(43,145,175)">boolean</span> <span style="color: rgb(0,0,0)">accept</span>(<span style="color: rgb(0,0,0)">File</span> <span style="color: rgb(0,0,0)">file</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">267</span> <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">file</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">isDirectory</span>();     <br /><span style="color: rgb(0,128,0)">268</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">269</span> <span style="color: rgb(0,0,0)">};</span>     <br /><span style="color: rgb(248,16,176)">270</span> <span style="color: rgb(0,0,0)">files</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">dir</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">listFiles</span>(<span style="color: rgb(0,0,0)">fileFilter</span>);    <br /><span style="color: rgb(0,128,0)">271</span>     <br /><span style="color: rgb(0,128,0)">272</span> <span style="color: rgb(0,128,0)">//15. 创建ZIP和JAR文件</span>    <br /><span style="color: rgb(0,128,0)">273</span>     <br /><span style="color: rgb(0,128,0)">274</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.util.zip.*</span>;     <br /><span style="color: rgb(248,16,176)">275</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.io.*</span>;    <br /><span style="color: rgb(0,128,0)">276</span>     <br /><span style="color: rgb(0,128,0)">277</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">ZipIt</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">278</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">main</span>(<span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">args</span><span style="color: rgb(0,0,0)">[])</span> <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">IOException</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">279</span> <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">args</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">length</span> <span style="color: rgb(0,0,0)">&lt;</span> <span style="color: rgb(0,0,0)">2</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">280</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">err</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;usage: java ZipIt Zip.zip file1 file2 file3&quot;</span>);     <br /><span style="color: rgb(0,128,0)">281</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">exit</span><span style="color: rgb(0,0,0)">(-</span><span style="color: rgb(0,0,0)">1</span>);     <br /><span style="color: rgb(0,128,0)">282</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">283</span> <span style="color: rgb(0,0,0)">File</span> <span style="color: rgb(0,0,0)">zipFile</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">File</span>(<span style="color: rgb(0,0,0)">args</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">]);</span>     <br /><span style="color: rgb(0,128,0)">284</span> <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">zipFile</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">exists</span>()) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">285</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">err</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;Zip file already exists, please try another&quot;</span>);     <br /><span style="color: rgb(0,128,0)">286</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">exit</span><span style="color: rgb(0,0,0)">(-</span><span style="color: rgb(0,0,0)">2</span>);     <br /><span style="color: rgb(0,128,0)">287</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">288</span> <span style="color: rgb(0,0,0)">FileOutputStream</span> <span style="color: rgb(0,0,0)">fos</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">FileOutputStream</span>(<span style="color: rgb(0,0,0)">zipFile</span>);     <br /><span style="color: rgb(0,128,0)">289</span> <span style="color: rgb(0,0,0)">ZipOutputStream</span> <span style="color: rgb(0,0,0)">zos</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">ZipOutputStream</span>(<span style="color: rgb(0,0,0)">fos</span>);     <br /><span style="color: rgb(248,16,176)">290</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">bytesRead</span>;     <br /><span style="color: rgb(0,128,0)">291</span> <span style="color: rgb(43,145,175)">byte</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">buffer</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(43,145,175)">byte</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">1024</span><span style="color: rgb(0,0,0)">];</span>     <br /><span style="color: rgb(0,128,0)">292</span> <span style="color: rgb(0,0,0)">CRC32</span> <span style="color: rgb(0,0,0)">crc</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">CRC32</span>();     <br /><span style="color: rgb(0,128,0)">293</span> <span style="color: rgb(0,0,255)">for</span> (<span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">1</span><span style="color: rgb(0,0,0)">,</span> n<span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">args</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">length</span>; <span style="color: rgb(0,0,0)">i</span> <span style="color: rgb(0,0,0)">&lt;</span> n; <span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">++)</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">294</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">name</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">args</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">];</span>     <br /><span style="color: rgb(248,16,176)">295</span> <span style="color: rgb(0,0,0)">File</span> <span style="color: rgb(0,0,0)">file</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">File</span>(<span style="color: rgb(0,0,0)">name</span>);     <br /><span style="color: rgb(0,128,0)">296</span> <span style="color: rgb(0,0,255)">if</span> <span style="color: rgb(0,0,0)">(!</span><span style="color: rgb(0,0,0)">file</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">exists</span>()) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">297</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">err</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;Skipping: &quot;</span> <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(0,0,0)">name</span>);     <br /><span style="color: rgb(0,128,0)">298</span> <span style="color: rgb(0,0,255)">continue</span>;     <br /><span style="color: rgb(0,128,0)">299</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(248,16,176)">300</span> <span style="color: rgb(0,0,0)">BufferedInputStream</span> <span style="color: rgb(0,0,0)">bis</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">BufferedInputStream</span>(     <br /><span style="color: rgb(0,128,0)">301</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">FileInputStream</span>(<span style="color: rgb(0,0,0)">file</span>));     <br /><span style="color: rgb(0,128,0)">302</span> <span style="color: rgb(0,0,0)">crc</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">reset</span>();     <br /><span style="color: rgb(0,128,0)">303</span> <span style="color: rgb(0,0,255)">while</span> ((<span style="color: rgb(0,0,0)">bytesRead</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">bis</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">read</span>(<span style="color: rgb(0,0,0)">buffer</span>)) <span style="color: rgb(0,0,0)">!=</span> <span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">304</span> <span style="color: rgb(0,0,0)">crc</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">update</span>(<span style="color: rgb(0,0,0)">buffer</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">bytesRead</span>);     <br /><span style="color: rgb(248,16,176)">305</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">306</span> <span style="color: rgb(0,0,0)">bis</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();     <br /><span style="color: rgb(0,128,0)">307</span> <span style="color: rgb(0,128,0)">// Reset to beginning of input stream </span>    <br /><span style="color: rgb(0,128,0)">308</span> <span style="color: rgb(0,0,0)">bis</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">BufferedInputStream</span>(     <br /><span style="color: rgb(0,128,0)">309</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">FileInputStream</span>(<span style="color: rgb(0,0,0)">file</span>));     <br /><span style="color: rgb(248,16,176)">310</span> <span style="color: rgb(0,0,0)">ZipEntry</span> <span style="color: rgb(0,0,0)">entry</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">ZipEntry</span>(<span style="color: rgb(0,0,0)">name</span>);     <br /><span style="color: rgb(0,128,0)">311</span> <span style="color: rgb(0,0,0)">entry</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setMethod</span>(<span style="color: rgb(0,0,0)">ZipEntry</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">STORED</span>);     <br /><span style="color: rgb(0,128,0)">312</span> <span style="color: rgb(0,0,0)">entry</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setCompressedSize</span>(<span style="color: rgb(0,0,0)">file</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">length</span>());     <br /><span style="color: rgb(0,128,0)">313</span> <span style="color: rgb(0,0,0)">entry</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setSize</span>(<span style="color: rgb(0,0,0)">file</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">length</span>());     <br /><span style="color: rgb(0,128,0)">314</span> <span style="color: rgb(0,0,0)">entry</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setCrc</span>(<span style="color: rgb(0,0,0)">crc</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getValue</span>());     <br /><span style="color: rgb(248,16,176)">315</span> <span style="color: rgb(0,0,0)">zos</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">putNextEntry</span>(<span style="color: rgb(0,0,0)">entry</span>);     <br /><span style="color: rgb(0,128,0)">316</span> <span style="color: rgb(0,0,255)">while</span> ((<span style="color: rgb(0,0,0)">bytesRead</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">bis</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">read</span>(<span style="color: rgb(0,0,0)">buffer</span>)) <span style="color: rgb(0,0,0)">!=</span> <span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)">1</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">317</span> <span style="color: rgb(0,0,0)">zos</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">write</span>(<span style="color: rgb(0,0,0)">buffer</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">bytesRead</span>);     <br /><span style="color: rgb(0,128,0)">318</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">319</span> <span style="color: rgb(0,0,0)">bis</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();     <br /><span style="color: rgb(248,16,176)">320</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">321</span> <span style="color: rgb(0,0,0)">zos</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">close</span>();     <br /><span style="color: rgb(0,128,0)">322</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">323</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">324</span>     <br /><span style="color: rgb(248,16,176)">325</span> <span style="color: rgb(0,128,0)">//16. 解析/读取XML 文件 </span>    <br /><span style="color: rgb(0,128,0)">326</span> <span style="color: rgb(0,0,0)">XML</span><span style="border-bottom: rgb(255,0,0) 1px solid; border-left: rgb(255,0,0) 1px solid; border-top: rgb(255,0,0) 1px solid; border-right: rgb(255,0,0) 1px solid">文件</span>     <br /><span style="color: rgb(0,128,0)">327</span> <span style="color: rgb(0,0,0)">&lt;?</span><span style="color: rgb(0,0,0)">xml</span> <span style="color: rgb(0,0,0)">version</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(163,21,21)">&quot;1.0&quot;</span><span style="color: rgb(0,0,0)">?&gt;</span>     <br /><span style="color: rgb(0,128,0)">328</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">students</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">329</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">student</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(248,16,176)">330</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)">John</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">331</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">grade</span><span style="color: rgb(0,0,0)">&gt;</span>B<span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">grade</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">332</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">age</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)">12</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">age</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">333</span> <span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">student</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">334</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">student</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(248,16,176)">335</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)">Mary</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">336</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">grade</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)">A</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">grade</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">337</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">age</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)">11</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">age</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">338</span> <span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">student</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">339</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">student</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(248,16,176)">340</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)">Simon</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">name</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">341</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">grade</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)">A</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">grade</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">342</span> <span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">age</span><span style="color: rgb(0,0,0)">&gt;</span><span style="color: rgb(0,0,0)">18</span><span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">age</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">343</span> <span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">student</span><span style="color: rgb(0,0,0)">&gt;</span>     <br /><span style="color: rgb(0,128,0)">344</span> <span style="color: rgb(0,0,0)">&lt;/</span><span style="color: rgb(0,0,0)">students</span><span style="color: rgb(0,0,0)">&gt;</span>    <br /><span style="color: rgb(248,16,176)">345</span>     <br /><span style="color: rgb(0,128,0)">346</span> <span style="color: rgb(0,128,0)">//Java代码 </span>    <br /><span style="color: rgb(0,128,0)">347</span> <span style="color: rgb(0,0,255)">package</span> <span style="color: rgb(0,0,0)">net</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">viralpatel</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">java</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">xmlparser</span>;    <br /><span style="color: rgb(0,128,0)">348</span>     <br /><span style="color: rgb(0,128,0)">349</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.io.File</span>;     <br /><span style="color: rgb(248,16,176)">350</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">javax.xml.parsers.DocumentBuilder</span>;     <br /><span style="color: rgb(0,128,0)">351</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">javax.xml.parsers.DocumentBuilderFactory</span>;    <br /><span style="color: rgb(0,128,0)">352</span>     <br /><span style="color: rgb(0,128,0)">353</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">org.w3c.dom.Document</span>;     <br /><span style="color: rgb(0,128,0)">354</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">org.w3c.dom.Element</span>;     <br /><span style="color: rgb(248,16,176)">355</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">org.w3c.dom.Node</span>;     <br /><span style="color: rgb(0,128,0)">356</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">org.w3c.dom.NodeList</span>;    <br /><span style="color: rgb(0,128,0)">357</span>     <br /><span style="color: rgb(0,128,0)">358</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">XMLParser</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">359</span>     <br /><span style="color: rgb(248,16,176)">360</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">getAllUserNames</span>(<span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">fileName</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">361</span> <span style="color: rgb(0,0,255)">try</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">362</span> <span style="color: rgb(0,0,0)">DocumentBuilderFactory</span> <span style="color: rgb(0,0,0)">dbf</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">DocumentBuilderFactory</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">newInstance</span>();     <br /><span style="color: rgb(0,128,0)">363</span> <span style="color: rgb(0,0,0)">DocumentBuilder</span> <span style="color: rgb(0,0,0)">db</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">dbf</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">newDocumentBuilder</span>();     <br /><span style="color: rgb(0,128,0)">364</span> <span style="color: rgb(0,0,0)">File</span> <span style="color: rgb(0,0,0)">file</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">File</span>(<span style="color: rgb(0,0,0)">fileName</span>);     <br /><span style="color: rgb(248,16,176)">365</span> <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">file</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">exists</span>()) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">366</span> <span style="color: rgb(0,0,0)">Document</span> <span style="color: rgb(0,0,0)">doc</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">db</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">parse</span>(<span style="color: rgb(0,0,0)">file</span>);     <br /><span style="color: rgb(0,128,0)">367</span> <span style="color: rgb(0,0,0)">Element</span> <span style="color: rgb(0,0,0)">docEle</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">doc</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getDocumentElement</span>();    <br /><span style="color: rgb(0,128,0)">368</span>     <br /><span style="color: rgb(0,128,0)">369</span> <span style="color: rgb(0,128,0)">// Print root element of the document </span>    <br /><span style="color: rgb(248,16,176)">370</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;Root element of the document: &quot;</span>     <br /><span style="color: rgb(0,128,0)">371</span> <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(0,0,0)">docEle</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getNodeName</span>());    <br /><span style="color: rgb(0,128,0)">372</span>     <br /><span style="color: rgb(0,128,0)">373</span> <span style="color: rgb(0,0,0)">NodeList</span> <span style="color: rgb(0,0,0)">studentList</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">docEle</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getElementsByTagName</span>(<span style="color: rgb(163,21,21)">&quot;student&quot;</span>);    <br /><span style="color: rgb(0,128,0)">374</span>     <br /><span style="color: rgb(248,16,176)">375</span> <span style="color: rgb(0,128,0)">// Print total student elements in document </span>    <br /><span style="color: rgb(0,128,0)">376</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span>     <br /><span style="color: rgb(0,128,0)">377</span> <span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;Total students: &quot;</span> <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(0,0,0)">studentList</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getLength</span>());    <br /><span style="color: rgb(0,128,0)">378</span>     <br /><span style="color: rgb(0,128,0)">379</span> <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">studentList</span> <span style="color: rgb(0,0,0)">!=</span> <span style="color: rgb(0,0,255)">null</span> <span style="color: rgb(0,0,0)">&amp;&amp;</span> <span style="color: rgb(0,0,0)">studentList</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getLength</span>() <span style="color: rgb(0,0,0)">&gt;</span> <span style="color: rgb(0,0,0)">0</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">380</span> <span style="color: rgb(0,0,255)">for</span> (<span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">i</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">0</span>; <span style="color: rgb(0,0,0)">i</span> <span style="color: rgb(0,0,0)">&lt;</span> <span style="color: rgb(0,0,0)">studentList</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getLength</span>(); <span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">++)</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">381</span>     <br /><span style="color: rgb(0,128,0)">382</span> <span style="color: rgb(0,0,0)">Node</span> <span style="color: rgb(0,0,0)">node</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">studentList</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">item</span>(<span style="color: rgb(0,0,0)">i</span>);    <br /><span style="color: rgb(0,128,0)">383</span>     <br /><span style="color: rgb(0,128,0)">384</span> <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">node</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getNodeType</span>() <span style="color: rgb(0,0,0)">==</span> <span style="color: rgb(0,0,0)">Node</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">ELEMENT_NODE</span>) <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(248,16,176)">385</span>     <br /><span style="color: rgb(0,128,0)">386</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span>     <br /><span style="color: rgb(0,128,0)">387</span> <span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;=====================&quot;</span>);    <br /><span style="color: rgb(0,128,0)">388</span>     <br /><span style="color: rgb(0,128,0)">389</span> <span style="color: rgb(0,0,0)">Element</span> <span style="color: rgb(0,0,0)">e</span> <span style="color: rgb(0,0,0)">=</span> (<span style="color: rgb(0,0,0)">Element</span>) <span style="color: rgb(0,0,0)">node</span>;     <br /><span style="color: rgb(248,16,176)">390</span> <span style="color: rgb(0,0,0)">NodeList</span> <span style="color: rgb(0,0,0)">nodeList</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">e</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getElementsByTagName</span>(<span style="color: rgb(163,21,21)">&quot;name&quot;</span>);     <br /><span style="color: rgb(0,128,0)">391</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;Name: &quot;</span>     <br /><span style="color: rgb(0,128,0)">392</span> <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(0,0,0)">nodeList</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">item</span>(<span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">).</span><span style="color: rgb(0,0,0)">getChildNodes</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">item</span>(<span style="color: rgb(0,0,0)">0</span>)     <br /><span style="color: rgb(0,128,0)">393</span> <span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getNodeValue</span>());    <br /><span style="color: rgb(0,128,0)">394</span>     <br /><span style="color: rgb(248,16,176)">395</span> <span style="color: rgb(0,0,0)">nodeList</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">e</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getElementsByTagName</span>(<span style="color: rgb(163,21,21)">&quot;grade&quot;</span>);     <br /><span style="color: rgb(0,128,0)">396</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;Grade: &quot;</span>     <br /><span style="color: rgb(0,128,0)">397</span> <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(0,0,0)">nodeList</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">item</span>(<span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">).</span><span style="color: rgb(0,0,0)">getChildNodes</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">item</span>(<span style="color: rgb(0,0,0)">0</span>)     <br /><span style="color: rgb(0,128,0)">398</span> <span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getNodeValue</span>());    <br /><span style="color: rgb(0,128,0)">399</span>     <br /><span style="color: rgb(248,16,176)">400</span> <span style="color: rgb(0,0,0)">nodeList</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">e</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getElementsByTagName</span>(<span style="color: rgb(163,21,21)">&quot;age&quot;</span>);     <br /><span style="color: rgb(0,128,0)">401</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;Age: &quot;</span>     <br /><span style="color: rgb(0,128,0)">402</span> <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(0,0,0)">nodeList</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">item</span>(<span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">).</span><span style="color: rgb(0,0,0)">getChildNodes</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">item</span>(<span style="color: rgb(0,0,0)">0</span>)     <br /><span style="color: rgb(0,128,0)">403</span> <span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getNodeValue</span>());     <br /><span style="color: rgb(0,128,0)">404</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(248,16,176)">405</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">406</span> <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">else</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">407</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">exit</span>(<span style="color: rgb(0,0,0)">1</span>);     <br /><span style="color: rgb(0,128,0)">408</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">409</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(248,16,176)">410</span> <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">catch</span> (<span style="color: rgb(0,0,0)">Exception</span> <span style="color: rgb(0,0,0)">e</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">411</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(0,0,0)">e</span>);     <br /><span style="color: rgb(0,128,0)">412</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">413</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">414</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">main</span>(<span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(248,16,176)">415</span>     <br /><span style="color: rgb(0,128,0)">416</span> <span style="color: rgb(0,0,0)">XMLParser</span> <span style="color: rgb(0,0,0)">parser</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">XMLParser</span>();     <br /><span style="color: rgb(0,128,0)">417</span> <span style="color: rgb(0,0,0)">parser</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getAllUserNames</span>(<span style="color: rgb(163,21,21)">&quot;c:\\test.xml&quot;</span>);     <br /><span style="color: rgb(0,128,0)">418</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">419</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(248,16,176)">420</span> <span style="color: rgb(0,128,0)">//17. 把 Array 转换成 Map </span>    <br /><span style="color: rgb(0,128,0)">421</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.util.Map</span>;     <br /><span style="color: rgb(0,128,0)">422</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">org.apache.commons.lang.ArrayUtils</span>;    <br /><span style="color: rgb(0,128,0)">423</span>     <br /><span style="color: rgb(0,128,0)">424</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">Main</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(248,16,176)">425</span>     <br /><span style="color: rgb(0,128,0)">426</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">main</span>(<span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">427</span> <span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">[</span> <span style="color: rgb(0,0,0)">][</span> <span style="color: rgb(0,0,0)">]</span> <span style="color: rgb(0,0,0)">countries</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">{</span> <span style="color: rgb(0,0,0)">{</span> <span style="color: rgb(163,21,21)">&quot;United States&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;New York&quot;</span> <span style="color: rgb(0,0,0)">},</span> <span style="color: rgb(0,0,0)">{</span> <span style="color: rgb(163,21,21)">&quot;United Kingdom&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;London&quot;</span> <span style="color: rgb(0,0,0)">},</span>     <br /><span style="color: rgb(0,128,0)">428</span> <span style="color: rgb(0,0,0)">{</span> <span style="color: rgb(163,21,21)">&quot;Netherland&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;Amsterdam&quot;</span> <span style="color: rgb(0,0,0)">},</span> <span style="color: rgb(0,0,0)">{</span> <span style="color: rgb(163,21,21)">&quot;Japan&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;Tokyo&quot;</span> <span style="color: rgb(0,0,0)">},</span> <span style="color: rgb(0,0,0)">{</span> <span style="color: rgb(163,21,21)">&quot;France&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;Paris&quot;</span> <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,0)">};</span>    <br /><span style="color: rgb(0,128,0)">429</span>     <br /><span style="color: rgb(248,16,176)">430</span> <span style="color: rgb(0,0,0)">Map</span> <span style="color: rgb(0,0,0)">countryCapitals</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">ArrayUtils</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">toMap</span>(<span style="color: rgb(0,0,0)">countries</span>);    <br /><span style="color: rgb(0,128,0)">431</span>     <br /><span style="color: rgb(0,128,0)">432</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;Capital of Japan is &quot;</span> <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(0,0,0)">countryCapitals</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">get</span>(<span style="color: rgb(163,21,21)">&quot;Japan&quot;</span>));     <br /><span style="color: rgb(0,128,0)">433</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;Capital of France is &quot;</span> <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(0,0,0)">countryCapitals</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">get</span>(<span style="color: rgb(163,21,21)">&quot;France&quot;</span>));     <br /><span style="color: rgb(0,128,0)">434</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(248,16,176)">435</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">436</span>     <br /><span style="color: rgb(0,128,0)">437</span> <span style="color: rgb(0,128,0)">//18. 发送邮件 </span>    <br /><span style="color: rgb(0,128,0)">438</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">javax.mail.*</span>;     <br /><span style="color: rgb(0,128,0)">439</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">javax.mail.internet.*</span>;     <br /><span style="color: rgb(248,16,176)">440</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.util.*</span>;    <br /><span style="color: rgb(0,128,0)">441</span>     <br /><span style="color: rgb(0,128,0)">442</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">postMail</span>( <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">recipients</span><span style="color: rgb(0,0,0)">[</span> <span style="color: rgb(0,0,0)">],</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">subject</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">message</span> <span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">from</span>) <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">MessagingException</span>     <br /><span style="color: rgb(0,128,0)">443</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">444</span> <span style="color: rgb(43,145,175)">boolean</span> <span style="color: rgb(0,0,0)">debug</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">false</span>;    <br /><span style="color: rgb(248,16,176)">445</span>     <br /><span style="color: rgb(0,128,0)">446</span> <span style="color: rgb(0,128,0)">//Set the host smtp address </span>    <br /><span style="color: rgb(0,128,0)">447</span> <span style="color: rgb(0,0,0)">Properties</span> <span style="color: rgb(0,0,0)">props</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Properties</span>();     <br /><span style="color: rgb(0,128,0)">448</span> <span style="color: rgb(0,0,0)">props</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">put</span>(<span style="color: rgb(163,21,21)">&quot;mail.smtp.host&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;smtp.example.com&quot;</span>);    <br /><span style="color: rgb(0,128,0)">449</span>     <br /><span style="color: rgb(248,16,176)">450</span> <span style="color: rgb(0,128,0)">// create some properties and get the default Session </span>    <br /><span style="color: rgb(0,128,0)">451</span> <span style="color: rgb(0,0,0)">Session</span> <span style="color: rgb(0,0,0)">session</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">Session</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getDefaultInstance</span>(<span style="color: rgb(0,0,0)">props</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,255)">null</span>);     <br /><span style="color: rgb(0,128,0)">452</span> <span style="color: rgb(0,0,0)">session</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setDebug</span>(<span style="color: rgb(0,0,0)">debug</span>);    <br /><span style="color: rgb(0,128,0)">453</span>     <br /><span style="color: rgb(0,128,0)">454</span> <span style="color: rgb(0,128,0)">// create a message </span>    <br /><span style="color: rgb(248,16,176)">455</span> <span style="color: rgb(0,0,0)">Message</span> <span style="color: rgb(0,0,0)">msg</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">MimeMessage</span>(<span style="color: rgb(0,0,0)">session</span>);    <br /><span style="color: rgb(0,128,0)">456</span>     <br /><span style="color: rgb(0,128,0)">457</span> <span style="color: rgb(0,128,0)">// set the from and to address </span>    <br /><span style="color: rgb(0,128,0)">458</span> <span style="color: rgb(0,0,0)">InternetAddress</span> <span style="color: rgb(0,0,0)">addressFrom</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">InternetAddress</span>(<span style="color: rgb(0,0,0)">from</span>);     <br /><span style="color: rgb(0,128,0)">459</span> <span style="color: rgb(0,0,0)">msg</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setFrom</span>(<span style="color: rgb(0,0,0)">addressFrom</span>);    <br /><span style="color: rgb(248,16,176)">460</span>     <br /><span style="color: rgb(0,128,0)">461</span> <span style="color: rgb(0,0,0)">InternetAddress</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">addressTo</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">InternetAddress</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">recipients</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">length</span><span style="color: rgb(0,0,0)">];</span>     <br /><span style="color: rgb(0,128,0)">462</span> <span style="color: rgb(0,0,255)">for</span> (<span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">i</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">0</span>; <span style="color: rgb(0,0,0)">i</span> <span style="color: rgb(0,0,0)">&lt;</span> <span style="color: rgb(0,0,0)">recipients</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">length</span>; <span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">++)</span>     <br /><span style="color: rgb(0,128,0)">463</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">464</span> <span style="color: rgb(0,0,0)">addressTo</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">]</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">InternetAddress</span>(<span style="color: rgb(0,0,0)">recipients</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">]);</span>     <br /><span style="color: rgb(248,16,176)">465</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">466</span> <span style="color: rgb(0,0,0)">msg</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setRecipients</span>(<span style="color: rgb(0,0,0)">Message</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">RecipientType</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">TO</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">addressTo</span>);    <br /><span style="color: rgb(0,128,0)">467</span>     <br /><span style="color: rgb(0,128,0)">468</span> <span style="color: rgb(0,128,0)">// Optional : You can also set your custom headers in the Email if you Want </span>    <br /><span style="color: rgb(0,128,0)">469</span> <span style="color: rgb(0,0,0)">msg</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">addHeader</span>(<span style="color: rgb(163,21,21)">&quot;MyHeaderName&quot;</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;myHeaderValue&quot;</span>);    <br /><span style="color: rgb(248,16,176)">470</span>     <br /><span style="color: rgb(0,128,0)">471</span> <span style="color: rgb(0,128,0)">// Setting the Subject and Content Type </span>    <br /><span style="color: rgb(0,128,0)">472</span> <span style="color: rgb(0,0,0)">msg</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setSubject</span>(<span style="color: rgb(0,0,0)">subject</span>);     <br /><span style="color: rgb(0,128,0)">473</span> <span style="color: rgb(0,0,0)">msg</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">setContent</span>(<span style="color: rgb(0,0,0)">message</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(163,21,21)">&quot;text/plain&quot;</span>);     <br /><span style="color: rgb(0,128,0)">474</span> <span style="color: rgb(0,0,0)">Transport</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">send</span>(<span style="color: rgb(0,0,0)">msg</span>);     <br /><span style="color: rgb(248,16,176)">475</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">476</span>     <br /><span style="color: rgb(0,128,0)">477</span> <span style="color: rgb(0,128,0)">//19. 发送代数据的HTTP 请求 </span>    <br /><span style="color: rgb(0,128,0)">478</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.io.BufferedReader</span>;     <br /><span style="color: rgb(0,128,0)">479</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.io.InputStreamReader</span>;     <br /><span style="color: rgb(248,16,176)">480</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.net.URL</span>;    <br /><span style="color: rgb(0,128,0)">481</span>     <br /><span style="color: rgb(0,128,0)">482</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">Main</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">483</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">main</span>(<span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">484</span> <span style="color: rgb(0,0,255)">try</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">485</span> <span style="color: rgb(0,0,0)">URL</span> <span style="color: rgb(0,0,0)">my_url</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">URL</span>(<span style="color: rgb(163,21,21)">&quot;http://cocre.com/&quot;</span>);     <br /><span style="color: rgb(0,128,0)">486</span> <span style="color: rgb(0,0,0)">BufferedReader</span> <span style="color: rgb(0,0,0)">br</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">BufferedReader</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">InputStreamReader</span>(<span style="color: rgb(0,0,0)">my_url</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">openStream</span>()));     <br /><span style="color: rgb(0,128,0)">487</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">strTemp</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(163,21,21)">&quot;&quot;</span>;     <br /><span style="color: rgb(0,128,0)">488</span> <span style="color: rgb(0,0,255)">while</span>(<span style="color: rgb(0,0,255)">null</span> <span style="color: rgb(0,0,0)">!=</span> (<span style="color: rgb(0,0,0)">strTemp</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">br</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">readLine</span><span style="color: rgb(0,0,0)">())){</span>     <br /><span style="color: rgb(0,128,0)">489</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(0,0,0)">strTemp</span>);     <br /><span style="color: rgb(248,16,176)">490</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">491</span> <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">catch</span> (<span style="color: rgb(0,0,0)">Exception</span> <span style="color: rgb(0,0,0)">ex</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">492</span> <span style="color: rgb(0,0,0)">ex</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">printStackTrace</span>();     <br /><span style="color: rgb(0,128,0)">493</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">494</span> <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(248,16,176)">495</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">496</span>     <br /><span style="color: rgb(0,128,0)">497</span> <span style="color: rgb(0,128,0)">//20. 改变数组的大小 </span>    <br /><span style="color: rgb(0,128,0)">498</span> <span style="color: rgb(0,128,0)">/** </span>    <br /><span style="color: rgb(0,128,0)">499</span> <span style="color: rgb(0,128,0)">* Reallocates an array with a new size, and copies the contents </span>    <br /><span style="color: rgb(248,16,176)">500</span> <span style="color: rgb(0,128,0)">* of the old array to the new array. </span>    <br /><span style="color: rgb(0,128,0)">501</span> <span style="color: rgb(0,128,0)">* @param oldArray the old array, to be reallocated. </span>    <br /><span style="color: rgb(0,128,0)">502</span> <span style="color: rgb(0,128,0)">* @param newSize the new array size. </span>    <br /><span style="color: rgb(0,128,0)">503</span> <span style="color: rgb(0,128,0)">* @return A new array with the same contents. </span>    <br /><span style="color: rgb(0,128,0)">504</span> <span style="color: rgb(0,128,0)">*/</span>     <br /><span style="color: rgb(248,16,176)">505</span> <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">resizeArray</span> (<span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">oldArray</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">newSize</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">506</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">oldSize</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">java</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">lang</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">reflect</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">Array</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getLength</span>(<span style="color: rgb(0,0,0)">oldArray</span>);     <br /><span style="color: rgb(0,128,0)">507</span> <span style="color: rgb(0,0,0)">Class</span> <span style="color: rgb(0,0,0)">elementType</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">oldArray</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getClass</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">getComponentType</span>();     <br /><span style="color: rgb(0,128,0)">508</span> <span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">newArray</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">java</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">lang</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">reflect</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">Array</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">newInstance</span>(     <br /><span style="color: rgb(0,128,0)">509</span> <span style="color: rgb(0,0,0)">elementType</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(0,0,0)">newSize</span>);     <br /><span style="color: rgb(248,16,176)">510</span> <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">preserveLength</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">Math</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">min</span>(<span style="color: rgb(0,0,0)">oldSize</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(0,0,0)">newSize</span>);     <br /><span style="color: rgb(0,128,0)">511</span> <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">preserveLength</span> <span style="color: rgb(0,0,0)">&gt;</span> <span style="color: rgb(0,0,0)">0</span>)     <br /><span style="color: rgb(0,128,0)">512</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">arraycopy</span> (<span style="color: rgb(0,0,0)">oldArray</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(0,0,0)">newArray</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(0,0,0)">0</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(0,0,0)">preserveLength</span>);     <br /><span style="color: rgb(0,128,0)">513</span> <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">newArray</span>;     <br /><span style="color: rgb(0,128,0)">514</span> <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(248,16,176)">515</span>     <br /><span style="color: rgb(0,128,0)">516</span> <span style="color: rgb(0,128,0)">// Test routine for resizeArray(). </span>    <br /><span style="color: rgb(0,128,0)">517</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">main</span> (<span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">518</span> <span style="color: rgb(43,145,175)">int</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">a</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">{</span><span style="color: rgb(0,0,0)">1</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(0,0,0)">2</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(0,0,0)">3</span><span style="color: rgb(0,0,0)">};</span>     <br /><span style="color: rgb(0,128,0)">519</span> <span style="color: rgb(0,0,0)">a</span> <span style="color: rgb(0,0,0)">=</span> (<span style="color: rgb(43,145,175)">int</span><span style="color: rgb(0,0,0)">[])</span><span style="color: rgb(0,0,0)">resizeArray</span>(<span style="color: rgb(0,0,0)">a</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(0,0,0)">5</span>);     <br /><span style="color: rgb(248,16,176)">520</span> <span style="color: rgb(0,0,0)">a</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">3</span><span style="color: rgb(0,0,0)">]</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">4</span>;     <br /><span style="color: rgb(0,128,0)">521</span> <span style="color: rgb(0,0,0)">a</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">4</span><span style="color: rgb(0,0,0)">]</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">5</span>;     <br /><span style="color: rgb(0,128,0)">522</span> <span style="color: rgb(0,0,255)">for</span> (<span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">0</span>; <span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">&lt;</span><span style="color: rgb(0,0,0)">a</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">length</span>; <span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">++)</span>&#160; <br /><span style="color: rgb(0,128,0)">523</span> <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span> (<span style="color: rgb(0,0,0)">a</span><span style="color: rgb(0,0,0)">[</span><span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">]);</span>     <br /><span style="color: rgb(0,128,0)">524</span> <span style="color: rgb(0,0,0)">}</span></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,0,0)">转：<a title="http://my.oschina.net/mrlin/blog/48672" href="http://my.oschina.net/mrlin/blog/48672">http://my.oschina.net/mrlin/blog/48672</a></span>    </div>
]]></content:encoded>
			<wfw:commentRss>http://www.buguw.com/20%e4%b8%aa%e5%b8%b8%e7%94%a8%e7%9a%84java%e7%a8%8b%e5%ba%8f%e5%9d%97.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java中的委托模式</title>
		<link>http://www.buguw.com/java%e4%b8%ad%e7%9a%84%e5%a7%94%e6%89%98%e6%a8%a1%e5%bc%8f.html</link>
		<comments>http://www.buguw.com/java%e4%b8%ad%e7%9a%84%e5%a7%94%e6%89%98%e6%a8%a1%e5%bc%8f.html#comments</comments>
		<pubDate>Tue, 17 Apr 2012 03:33:55 +0000</pubDate>
		<dc:creator>tiger7456</dc:creator>
				<category><![CDATA[Java天地]]></category>

		<guid isPermaLink="false">http://www.buguw.com/?p=310</guid>
		<description><![CDATA[委托模式是软件设计模式中的一项基本技巧。在委托模式中，有两个对象参与处理同一个请求，接受请求的对象将请求委托给另一个对象来处理。委托模式是一项基本技巧，许多其他的模式，如状态模式、策略模式、访问者模式本质上是在更特殊的场合采用了委托模式。委托模式使得我们可以用聚合来替代继承，它还使我们可以模拟mixin。 “委托”在C#中是一个语言级特性，而在Java语言中没有直接的对应，但是我们可以通过动态代理来实现委托！ Java语言: Codee#26113 01 import java.lang.reflect.InvocationHandler; 02 import java.lang.reflect.Method; 03 import java.lang.reflect.Proxy; 04&#160;&#160;&#160;&#160; 05 public abstract class Delegator implements InvocationHandler { 06&#160;&#160;&#160;&#160;&#160; //-------------------------------------------- 07 08&#160;&#160;&#160;&#160; protected Object obj_orgin = null; //原始对象 09&#160;&#160;&#160;&#160;&#160; protected Object obj_proxy = null; //代理对象 10&#160;&#160;&#160;&#160;&#160; //-------------------------------------------- 11 12&#160;&#160;&#160;&#160; public Delegator() { 13&#160;&#160;&#160;&#160;&#160; } 14 15&#160;&#160;&#160;&#160; public Delegator(Object orgin) { 16&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>委托模式是软件设计模式中的一项基本技巧。在委托模式中，有两个对象参与处理同一个请求，接受请求的对象将请求委托给另一个对象来处理。委托模式是一项基本技巧，许多其他的模式，如状态模式、策略模式、访问者模式本质上是在更特殊的场合采用了委托模式。委托模式使得我们可以用聚合来替代继承，它还使我们可以模拟mixin。    <br />“委托”在C#中是一个语言级特性，而在Java语言中没有直接的对应，但是我们可以通过动态代理来实现委托！</p>
<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26113/">Codee#26113</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">01</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.lang.reflect.InvocationHandler</span>;     <br /><span style="color: rgb(0,128,0)">02</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.lang.reflect.Method</span>;     <br /><span style="color: rgb(0,128,0)">03</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.lang.reflect.Proxy</span>;     <br /><span style="color: rgb(0,128,0)">04</span>&#160;&#160;&#160;&#160; <br /><span style="color: rgb(248,16,176)">05</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">abstract</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">Delegator</span> <span style="color: rgb(0,0,255)">implements</span> <span style="color: rgb(0,0,0)">InvocationHandler</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">06</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,128,0)">//--------------------------------------------</span>     <br /><span style="color: rgb(0,128,0)">07</span>     <br /><span style="color: rgb(0,128,0)">08</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">protected</span> <span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">obj_orgin</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">null</span>; <span style="color: rgb(0,128,0)">//原始对象</span>     <br /><span style="color: rgb(0,128,0)">09</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">protected</span> <span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">obj_proxy</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">null</span>; <span style="color: rgb(0,128,0)">//代理对象</span>     <br /><span style="color: rgb(248,16,176)">10</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,128,0)">//--------------------------------------------</span>     <br /><span style="color: rgb(0,128,0)">11</span>     <br /><span style="color: rgb(0,128,0)">12</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,0)">Delegator</span>() <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">13</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">14</span>     <br /><span style="color: rgb(248,16,176)">15</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,0)">Delegator</span>(<span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">orgin</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">createProxy</span>(<span style="color: rgb(0,0,0)">orgin</span>);     <br /><span style="color: rgb(0,128,0)">17</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">18</span>&#160;&#160;&#160;&#160; <br /><span style="color: rgb(0,128,0)">19</span>     <br /><span style="color: rgb(248,16,176)">20</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">protected</span> <span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">createProxy</span>(<span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">orgin</span>) <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">obj_orgin</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">orgin</span>;     <br /><span style="color: rgb(0,128,0)">22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,128,0)">//下面语句中orgin.getClass().getClassLoader()为加载器，orgin.getClass().getInterfaces()为接口集</span>     <br /><span style="color: rgb(0,128,0)">23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">obj_proxy</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">Proxy</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">newProxyInstance</span>(<span style="color: rgb(0,0,0)">orgin</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getClass</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">getClassLoader</span><span style="color: rgb(0,0,0)">(),</span> <span style="color: rgb(0,0,0)">orgin</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getClass</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">getInterfaces</span><span style="color: rgb(0,0,0)">(),</span> <span style="color: rgb(0,0,255)">this</span>); <span style="color: rgb(0,128,0)">//委托</span>     <br /><span style="color: rgb(0,128,0)">24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">obj_proxy</span>;     <br /><span style="color: rgb(248,16,176)">25</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">26</span>&#160;&#160;&#160;&#160; <br /><span style="color: rgb(0,128,0)">27</span>     <br /><span style="color: rgb(0,128,0)">28</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">protected</span> <span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">invokeSuper</span>(<span style="color: rgb(0,0,0)">Method</span> <span style="color: rgb(0,0,0)">method</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">Object</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">Throwable</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(0,128,0)">29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">method</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">invoke</span>(<span style="color: rgb(0,0,0)">obj_orgin</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">args</span>);     <br /><span style="color: rgb(248,16,176)">30</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">31</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,128,0)">//--------------实现InvocationHandler接口，要求覆盖------------</span>     <br /><span style="color: rgb(0,128,0)">32</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,128,0)">//下面实现的方法是当委托的类调用toString()方法时，操作其他方法而不是该类默认的toString()，这个类的其他方法则不会。</span>     <br /><span style="color: rgb(0,128,0)">33</span>     <br /><span style="color: rgb(0,128,0)">34</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">invoke</span>(<span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">obj</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">Method</span> <span style="color: rgb(0,0,0)">method</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">Object</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">Throwable</span> <span style="color: rgb(0,0,0)">{</span>     <br /><span style="color: rgb(248,16,176)">35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,128,0)">// 缺省实现：委托给obj_orgin完成对应的操作</span>     <br /><span style="color: rgb(0,128,0)">36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">method</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getName</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">equals</span>(<span style="color: rgb(163,21,21)">&quot;toString&quot;</span>)) <span style="color: rgb(0,0,0)">{</span> <span style="color: rgb(0,128,0)">//对其做额外处理</span>     <br /><span style="color: rgb(0,128,0)">37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">invokeSuper</span>(<span style="color: rgb(0,0,0)">method</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(163,21,21)">&quot;$Proxy&quot;</span>;     <br /><span style="color: rgb(0,128,0)">38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">else</span> <span style="color: rgb(0,0,0)">{</span> <span style="color: rgb(0,128,0)">//注意，调用原始对象的方法，而不是代理的（obj==obj_proxy）</span>     <br /><span style="color: rgb(0,128,0)">39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,255)">this</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">invokeSuper</span>(<span style="color: rgb(0,0,0)">method</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">args</span>);     <br /><span style="color: rgb(248,16,176)">40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">41</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>     <br /><span style="color: rgb(0,128,0)">42</span> <span style="color: rgb(0,0,0)">}</span></div>
<p><span style="color: rgb(0,0,0)"></span></p>
<p>下面的代码，则是作为一个委托的例子，实现Map的功能。</p>
<p>&#160;</p>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source">&#160;</div>
<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26114/">Codee#26114</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">01</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.io.IOException</span>;    <br /><span style="color: rgb(0,128,0)">02</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.lang.reflect.Method</span>;    <br /><span style="color: rgb(0,128,0)">03</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.util.Hashtable</span>;    <br /><span style="color: rgb(0,128,0)">04</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.util.Map</span>;    <br /><span style="color: rgb(248,16,176)">05</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">org.apache.commons.logging.Log</span>;    <br /><span style="color: rgb(0,128,0)">06</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">org.apache.commons.logging.LogFactory</span>;    <br /><span style="color: rgb(0,128,0)">07</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">com.bs2.core.UtilLog</span>;    <br /><span style="color: rgb(0,128,0)">08</span>     <br /><span style="color: rgb(0,128,0)">09</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">Delegator4Map</span> <span style="color: rgb(0,0,255)">extends</span> <span style="color: rgb(0,0,0)">Delegator</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(248,16,176)">10</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,0)">Log</span> <span style="color: rgb(0,0,0)">_log</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">LogFactory</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getLog</span>(<span style="color: rgb(0,0,0)">Delegator4Map</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">class</span>);    <br /><span style="color: rgb(0,128,0)">11</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,0)">Map</span> <span style="color: rgb(0,0,0)">orginClass</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">null</span>; <span style="color: rgb(0,128,0)">//原始对象</span>    <br /><span style="color: rgb(0,128,0)">12</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,0)">Map</span> <span style="color: rgb(0,0,0)">proxyClass</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">null</span>; <span style="color: rgb(0,128,0)">//代理对象</span>    <br /><span style="color: rgb(0,128,0)">13</span>     <br /><span style="color: rgb(0,128,0)">14</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,0)">Map</span> <span style="color: rgb(0,0,0)">getOrgin</span>() <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(248,16,176)">15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">orginClass</span>;    <br /><span style="color: rgb(0,128,0)">16</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">17</span>     <br /><span style="color: rgb(0,128,0)">18</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,0)">Map</span> <span style="color: rgb(0,0,0)">getProxy</span>() <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">proxyClass</span>;    <br /><span style="color: rgb(248,16,176)">20</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">21</span>     <br /><span style="color: rgb(0,128,0)">22</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,0)">Delegator4Map</span>(<span style="color: rgb(0,0,0)">Map</span> <span style="color: rgb(0,0,0)">orgin</span>) <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">super</span>(<span style="color: rgb(0,0,0)">orgin</span>);    <br /><span style="color: rgb(0,128,0)">24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">orginClass</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">orgin</span>;    <br /><span style="color: rgb(248,16,176)">25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">proxyClass</span> <span style="color: rgb(0,0,0)">=</span> (<span style="color: rgb(0,0,0)">Map</span>) <span style="color: rgb(0,0,255)">super</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">obj_proxy</span>;    <br /><span style="color: rgb(0,128,0)">26</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">27</span>     <br /><span style="color: rgb(0,128,0)">28</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">invoke</span>(<span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">obj</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">Method</span> <span style="color: rgb(0,0,0)">method</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">Object</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">Throwable</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">method</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getName</span><span style="color: rgb(0,0,0)">().</span><span style="color: rgb(0,0,0)">equals</span>(<span style="color: rgb(163,21,21)">&quot;size&quot;</span>)) <span style="color: rgb(0,0,0)">{</span> <span style="color: rgb(0,128,0)">//修改size处理逻辑</span>    <br /><span style="color: rgb(248,16,176)">30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">Object</span> <span style="color: rgb(0,0,0)">res2</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Integer</span><span style="color: rgb(0,0,0)">(-</span><span style="color: rgb(0,0,0)">1</span>);    <br /><span style="color: rgb(0,128,0)">31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;调用委托的方法&quot;</span>);    <br /><span style="color: rgb(0,128,0)">32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">res2</span>;    <br /><span style="color: rgb(0,128,0)">33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">else</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">System</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">out</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">println</span>(<span style="color: rgb(163,21,21)">&quot;调用原始的方法&quot;</span>);    <br /><span style="color: rgb(248,16,176)">35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,255)">super</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">invoke</span>(<span style="color: rgb(0,0,0)">obj</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">method</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">args</span>);    <br /><span style="color: rgb(0,128,0)">36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">37</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">38</span>     <br /><span style="color: rgb(0,128,0)">39</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(43,145,175)">void</span> <span style="color: rgb(0,0,0)">main</span>(<span style="color: rgb(0,0,0)">String</span><span style="color: rgb(0,0,0)">[]</span> <span style="color: rgb(0,0,0)">args</span>) <span style="color: rgb(0,0,255)">throws</span> <span style="color: rgb(0,0,0)">IOException</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(248,16,176)">40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">Delegator4Map</span> <span style="color: rgb(0,0,0)">rtm</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Delegator4Map</span>(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Hashtable</span>());    <br /><span style="color: rgb(0,128,0)">41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">Map</span> <span style="color: rgb(0,0,0)">m</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">rtm</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">getProxy</span>();    <br /><span style="color: rgb(0,128,0)">42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">m</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">size</span>();    <br /><span style="color: rgb(0,128,0)">43</span>&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">44</span> <span style="color: rgb(0,0,0)">}</span>    </div>
<p>代码编写有个这样的原则：能不用继承就不用继承，能使用委托实现的就不使用继承。两个类有明显示的层级关系时使用继承，没有明显的层级关系，仅仅是为了在一个类中使用另一个类的方法时应该使用委托。   <br />根据《重构》一书称：现在有滥用继承的趋势，JDK 中 Stack 就是一个滥用继承的典型！    <br />java.util.Stack 继承自 java.util.Vector，其实 Stack 与 Vector 在用途上完全是风马牛不相及的两个容器。</p>
<p>原文地址：<a title="http://my.oschina.net/u/108286/blog/33103" href="http://my.oschina.net/u/108286/blog/33103">http://my.oschina.net/u/108286/blog/33103</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.buguw.com/java%e4%b8%ad%e7%9a%84%e5%a7%94%e6%89%98%e6%a8%a1%e5%bc%8f.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java生成流水号</title>
		<link>http://www.buguw.com/java%e7%94%9f%e6%88%90%e6%b5%81%e6%b0%b4%e5%8f%b7.html</link>
		<comments>http://www.buguw.com/java%e7%94%9f%e6%88%90%e6%b5%81%e6%b0%b4%e5%8f%b7.html#comments</comments>
		<pubDate>Tue, 17 Apr 2012 03:27:14 +0000</pubDate>
		<dc:creator>tiger7456</dc:creator>
				<category><![CDATA[Java天地]]></category>

		<guid isPermaLink="false">http://www.buguw.com/?p=308</guid>
		<description><![CDATA[&#160; 项目中，需要生成一个流水账号作为数据表主键，完成后，觉得还可以，不敢独享，遂贴出以供拍砖。 流水号格式为yyyyMMddXXXX，规定每天只能到9999，代码如下： &#160; &#160; Java语言: Codee#26112 01 import java.text.DecimalFormat; 02 import java.text.SimpleDateFormat; 03 import java.util.Date; 04 05 public class PrimaryGenerater { 06 07&#160;&#160;&#160;&#160; private static final String SERIAL_NUMBER = &#34;XXXX&#34;; // 流水号格式 08&#160;&#160;&#160;&#160; private static PrimaryGenerater primaryGenerater = null; 09 10&#160;&#160;&#160;&#160; private PrimaryGenerater() { 11&#160;&#160;&#160;&#160; } 12 13&#160;&#160;&#160;&#160; /** 14 &#160;&#160;&#160;&#160; * 取得PrimaryGenerater的单例实现 15 [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>项目中，需要生成一个流水账号作为数据表主键，完成后，觉得还可以，不敢独享，遂贴出以供拍砖。</p>
<p>流水号格式为yyyyMMddXXXX，规定每天只能到9999，代码如下：</p>
<p>&#160;</p>
<p>&#160;</p>
<div style="background: rgb(253,253,253); color: black"><u>Java语言</u>: <a href="http://fayaa.com/code/view/26112/">Codee#26112</a></div>
<div style="background-color: rgb(249,247,237); font-family: &quot;Microsoft Yahei&quot;,&quot;Consolas&quot;,&quot;Lucida Console&quot;,&quot;Courier New&quot;; color: rgb(0,0,0)" class="source"><span style="color: rgb(0,128,0)">01</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.text.DecimalFormat</span>;    <br /><span style="color: rgb(0,128,0)">02</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.text.SimpleDateFormat</span>;    <br /><span style="color: rgb(0,128,0)">03</span> <span style="color: rgb(0,0,255)">import</span> <span style="color: rgb(0,0,0)">java.util.Date</span>;    <br /><span style="color: rgb(0,128,0)">04</span>     <br /><span style="color: rgb(248,16,176)">05</span> <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">PrimaryGenerater</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">06</span>     <br /><span style="color: rgb(0,128,0)">07</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,255)">final</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">SERIAL_NUMBER</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(163,21,21)">&quot;XXXX&quot;</span>; <span style="color: rgb(0,128,0)">// 流水号格式</span>    <br /><span style="color: rgb(0,128,0)">08</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,0)">PrimaryGenerater</span> <span style="color: rgb(0,0,0)">primaryGenerater</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">null</span>;    <br /><span style="color: rgb(0,128,0)">09</span>     <br /><span style="color: rgb(248,16,176)">10</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,0)">PrimaryGenerater</span>() <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">11</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">12</span>     <br /><span style="color: rgb(0,128,0)">13</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,128,0)">/**</span>    <br /><span style="color: rgb(0,128,0)">14</span> <span style="color: rgb(0,128,0)">&#160;&#160;&#160;&#160; * 取得PrimaryGenerater的单例实现</span>    <br /><span style="color: rgb(248,16,176)">15</span> <span style="color: rgb(0,128,0)">&#160;&#160;&#160;&#160; * </span>    <br /><span style="color: rgb(0,128,0)">16</span> <span style="color: rgb(0,128,0)">&#160;&#160;&#160;&#160; * @return</span>    <br /><span style="color: rgb(0,128,0)">17</span> <span style="color: rgb(0,128,0)">&#160;&#160;&#160;&#160; */</span>    <br /><span style="color: rgb(0,128,0)">18</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,0)">PrimaryGenerater</span> <span style="color: rgb(0,0,0)">getInstance</span>() <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">primaryGenerater</span> <span style="color: rgb(0,0,0)">==</span> <span style="color: rgb(0,0,255)">null</span>) <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(248,16,176)">20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">synchronized</span> (<span style="color: rgb(0,0,0)">PrimaryGenerater</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">class</span>) <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">primaryGenerater</span> <span style="color: rgb(0,0,0)">==</span> <span style="color: rgb(0,0,255)">null</span>) <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">primaryGenerater</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">PrimaryGenerater</span>();    <br /><span style="color: rgb(0,128,0)">23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(248,16,176)">25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">primaryGenerater</span>;    <br /><span style="color: rgb(0,128,0)">27</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">28</span>     <br /><span style="color: rgb(0,128,0)">29</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,128,0)">/**</span>    <br /><span style="color: rgb(248,16,176)">30</span> <span style="color: rgb(0,128,0)">&#160;&#160;&#160;&#160; * 生成下一个编号</span>    <br /><span style="color: rgb(0,128,0)">31</span> <span style="color: rgb(0,128,0)">&#160;&#160;&#160;&#160; */</span>    <br /><span style="color: rgb(0,128,0)">32</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">synchronized</span> <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">generaterNextNumber</span>(<span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">sno</span>) <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">String</span> <span style="color: rgb(0,0,0)">id</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">null</span>;    <br /><span style="color: rgb(0,128,0)">34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">Date</span> <span style="color: rgb(0,0,0)">date</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">Date</span>();    <br /><span style="color: rgb(248,16,176)">35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">SimpleDateFormat</span> <span style="color: rgb(0,0,0)">formatter</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">SimpleDateFormat</span>(<span style="color: rgb(163,21,21)">&quot;yyyyMMdd&quot;</span>);    <br /><span style="color: rgb(0,128,0)">36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">if</span> (<span style="color: rgb(0,0,0)">sno</span> <span style="color: rgb(0,0,0)">==</span> <span style="color: rgb(0,0,255)">null</span>) <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">id</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">formatter</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">format</span>(<span style="color: rgb(0,0,0)">date</span>) <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(163,21,21)">&quot;0001&quot;</span>;    <br /><span style="color: rgb(0,128,0)">38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span> <span style="color: rgb(0,0,255)">else</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">count</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">SERIAL_NUMBER</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">length</span>();    <br /><span style="color: rgb(248,16,176)">40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">StringBuilder</span> sb <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">StringBuilder</span>();    <br /><span style="color: rgb(0,128,0)">41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">for</span> (<span style="color: rgb(43,145,175)">int</span> <span style="color: rgb(0,0,0)">i</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">0</span>; <span style="color: rgb(0,0,0)">i</span> <span style="color: rgb(0,0,0)">&lt;</span> <span style="color: rgb(0,0,0)">count</span>; <span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">++)</span> <span style="color: rgb(0,0,0)">{</span>    <br /><span style="color: rgb(0,128,0)">42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sb<span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">append</span>(<span style="color: rgb(163,21,21)">&quot;0&quot;</span>);    <br /><span style="color: rgb(0,128,0)">43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">DecimalFormat</span> <span style="color: rgb(0,0,0)">df</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(0,0,0)">DecimalFormat</span>(<span style="color: rgb(163,21,21)">&quot;0000&quot;</span>);    <br /><span style="color: rgb(248,16,176)">45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">id</span> <span style="color: rgb(0,0,0)">=</span> <span style="color: rgb(0,0,0)">formatter</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">format</span>(<span style="color: rgb(0,0,0)">date</span>)    <br /><span style="color: rgb(0,128,0)">46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(0,0,0)">df</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">format</span>(<span style="color: rgb(0,0,0)">1</span> <span style="color: rgb(0,0,0)">+</span> <span style="color: rgb(0,0,0)">Integer</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">parseInt</span>(<span style="color: rgb(0,0,0)">sno</span><span style="color: rgb(0,0,0)">.</span><span style="color: rgb(0,0,0)">substring</span>(<span style="color: rgb(0,0,0)">8</span><span style="color: rgb(0,0,0)">,</span> <span style="color: rgb(0,0,0)">12</span>)));    <br /><span style="color: rgb(0,128,0)">47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(0,128,0)">48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(0,0,0)">id</span>;    <br /><span style="color: rgb(0,128,0)">49</span>&#160;&#160;&#160;&#160; <span style="color: rgb(0,0,0)">}</span>    <br /><span style="color: rgb(248,16,176)">50</span> <span style="color: rgb(0,0,0)">}</span>    </div>
<p>转：<a title="http://my.oschina.net/se77en/blog/41122" href="http://my.oschina.net/se77en/blog/41122">http://my.oschina.net/se77en/blog/41122</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.buguw.com/java%e7%94%9f%e6%88%90%e6%b5%81%e6%b0%b4%e5%8f%b7.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ant执行的时候内存溢出解决办法</title>
		<link>http://www.buguw.com/ant%e6%89%a7%e8%a1%8c%e7%9a%84%e6%97%b6%e5%80%99%e5%86%85%e5%ad%98%e6%ba%a2%e5%87%ba%e8%a7%a3%e5%86%b3%e5%8a%9e%e6%b3%95.html</link>
		<comments>http://www.buguw.com/ant%e6%89%a7%e8%a1%8c%e7%9a%84%e6%97%b6%e5%80%99%e5%86%85%e5%ad%98%e6%ba%a2%e5%87%ba%e8%a7%a3%e5%86%b3%e5%8a%9e%e6%b3%95.html#comments</comments>
		<pubDate>Tue, 17 Apr 2012 03:17:15 +0000</pubDate>
		<dc:creator>tiger7456</dc:creator>
				<category><![CDATA[Java天地]]></category>
		<category><![CDATA[ant]]></category>

		<guid isPermaLink="false">http://www.buguw.com/?p=306</guid>
		<description><![CDATA[ant执行的时候内存溢出 1、在ant.bat文件中增加缓存，追加-Xms128m -Xmx256m参数： java -Xms128m -Xmx256m -classpath &#34;%ANTCLASSPATH%&#34; org.apache.tools.ant.Main %ANT_ARGS% 2、使用eclipse运行ant时，应如下设定参数： build.xml文件右键-〉运行-〉外部工具-〉选择JRE页-〉VM参数处设定 -Xms128m -Xmx256m即可。 转至：http://hi.baidu.com/fan_run/blog/item/c5d2963406952f46241f1411.html]]></description>
			<content:encoded><![CDATA[<p>ant执行的时候内存溢出</p>
<p>1、在ant.bat文件中增加缓存，追加-Xms128m -Xmx256m参数：</p>
<p>java -Xms128m -Xmx256m -classpath &quot;%ANTCLASSPATH%&quot; org.apache.tools.ant.Main %ANT_ARGS%</p>
<p>2、使用eclipse运行ant时，应如下设定参数：</p>
<p>build.xml文件右键-〉运行-〉外部工具-〉选择JRE页-〉VM参数处设定 -Xms128m -Xmx256m即可。 </p>
<p>转至：<a title="http://hi.baidu.com/fan_run/blog/item/c5d2963406952f46241f1411.html" href="http://hi.baidu.com/fan_run/blog/item/c5d2963406952f46241f1411.html">http://hi.baidu.com/fan_run/blog/item/c5d2963406952f46241f1411.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.buguw.com/ant%e6%89%a7%e8%a1%8c%e7%9a%84%e6%97%b6%e5%80%99%e5%86%85%e5%ad%98%e6%ba%a2%e5%87%ba%e8%a7%a3%e5%86%b3%e5%8a%9e%e6%b3%95.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MyEclipse10.0 采用lnk方法安装SVN1.8.5</title>
		<link>http://www.buguw.com/myeclipse10-0-%e9%87%87%e7%94%a8lnk%e6%96%b9%e6%b3%95%e5%ae%89%e8%a3%85svn1-8-5.html</link>
		<comments>http://www.buguw.com/myeclipse10-0-%e9%87%87%e7%94%a8lnk%e6%96%b9%e6%b3%95%e5%ae%89%e8%a3%85svn1-8-5.html#comments</comments>
		<pubDate>Thu, 12 Apr 2012 03:42:55 +0000</pubDate>
		<dc:creator>tiger7456</dc:creator>
				<category><![CDATA[Java天地]]></category>
		<category><![CDATA[MyEclipse]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://www.buguw.com/?p=304</guid>
		<description><![CDATA[一、到官方上下载svn1.8.3，下载后的文件名叫site-1.8.3.zip 二、解压，删除解压后得到的文件中除了名叫features和plugins的两个文件夹以外的其他所有文件。 三、在MyEclipse10.0的安装目录中自己新建一个文件夹（我的叫MyPlugins）然后把解压并删除过文件后得到的那个SVN文件夹 复制到该目录； 四、在MyEclipse10.0的安装目录下的dropins文件夹中建立一个文本文件，内容输path=你的svn路径，（我的是path=D:\\MyEclipse 10\\MyPlugins\\SVN-1.8.5），然后保存，保存后将该文件名重命名成SVN.lnk(记住扩展名是是.lnk不是网上有网友说的link). 五、接下来删除 MyEclipse10.0的安装目录下configuration文件夹中的org.eclipse.update文件夹。 六、重启－〉OK。]]></description>
			<content:encoded><![CDATA[<p>一、到官方上下载svn1.8.3，下载后的文件名叫site-1.8.3.zip</p>
<p>二、解压，删除解压后得到的文件中除了名叫features和plugins的两个文件夹以外的其他所有文件。</p>
<p>三、在MyEclipse10.0的安装目录中自己新建一个文件夹（我的叫MyPlugins）然后把解压并删除过文件后得到的那个SVN文件夹 复制到该目录；</p>
<p>四、在MyEclipse10.0的安装目录下的dropins文件夹中建立一个文本文件，内容输path=你的svn路径，（我的是path=D:\\MyEclipse 10\\MyPlugins\\SVN-1.8.5），然后保存，保存后将该文件名重命名成SVN.lnk(记住扩展名是是.lnk不是网上有网友说的link).</p>
<p>五、接下来删除 MyEclipse10.0的安装目录下configuration文件夹中的org.eclipse.update文件夹。</p>
<p>六、重启－〉OK。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.buguw.com/myeclipse10-0-%e9%87%87%e7%94%a8lnk%e6%96%b9%e6%b3%95%e5%ae%89%e8%a3%85svn1-8-5.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>eclipse的svn插件找不到 javaHL</title>
		<link>http://www.buguw.com/eclipse%e7%9a%84svn%e6%8f%92%e4%bb%b6%e6%89%be%e4%b8%8d%e5%88%b0-javahl.html</link>
		<comments>http://www.buguw.com/eclipse%e7%9a%84svn%e6%8f%92%e4%bb%b6%e6%89%be%e4%b8%8d%e5%88%b0-javahl.html#comments</comments>
		<pubDate>Thu, 12 Apr 2012 01:52:18 +0000</pubDate>
		<dc:creator>tiger7456</dc:creator>
				<category><![CDATA[编程学习经验]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://www.buguw.com/?p=302</guid>
		<description><![CDATA[Subclipse是一个很优秀的SVN客户端。它提供了ClientAdapter、JavaHL和SVNKit等方式于SVN服务交互。官方强烈推荐使用JavaHL的方式。JavaHL使用JNI的调用SVN的本地库。速度快，稳定可靠。 但是在64位的Win7下，就比较麻烦了。默认安装好后， Subclipse的配置界面中，显示的javaHL的状态是“unavailable“，如图。这是因为系统中没有安装svn的javahl。 Eclipse 中使用Subsvn时的错误提示 原因是SVN接口 Unavailable 解决办法是安装 SlikSVN，可以在http://www.sliksvn.com/en/download下载SlikSVN，不同的Subsvn要安装不同的SlikSVN。 当前版本对应 Subclipse Version SVN/JavaHL Version 1.6.x 1.6.x 1.4.x 1.5.x 1.2.x 1.4.x 1.0.x 1.4.x 下载安装完SlikSVN后，配置系统环境变量，把SlikSVN安装目录下的bin目录加入到系统环境变量： 重新启动Eclipse，你会看到Subsvn已经有JavaHL库了 转载至：http://hkp.iteye.com/blog/1174499]]></description>
			<content:encoded><![CDATA[<p>Subclipse是一个很优秀的SVN客户端。它提供了ClientAdapter、JavaHL和SVNKit等方式于SVN服务交互。官方强烈推荐使用JavaHL的方式。JavaHL使用JNI的调用SVN的本地库。速度快，稳定可靠。</p>
<p>但是在64位的Win7下，就比较麻烦了。默认安装好后， Subclipse的配置界面中，显示的javaHL的状态是“unavailable“，如图。这是因为系统中没有安装svn的javahl。</p>
<p><img alt="" src="http://dl.iteye.com/upload/picture/pic/99103/0aff37d6-2575-3c6f-bd26-93c1b4830049.jpg" width="263" height="294" /> <img alt="" src="http://dl.iteye.com/upload/picture/pic/99101/52f714bd-a98d-33f7-8ce6-7445b7bdf6c6.jpg" width="263" height="294" /></p>
<p><strong>Eclipse 中使用Subsvn时的错误提示 </strong><strong>原因是SVN接口 Unavailable </strong></p>
<p>解决办法是安装 SlikSVN，可以在<a href="http://www.sliksvn.com/en/download">http://www.sliksvn.com/en/download</a>下载SlikSVN，不同的Subsvn要安装不同的SlikSVN。</p>
<p><strong><em>当前版本</em></strong><strong><em>对应</em></strong></p>
<p><strong>Subclipse Version</strong></p>
<p><strong>SVN/JavaHL Version</strong></p>
<p>1.6.x</p>
<p>1.6.x</p>
<p>1.4.x</p>
<p>1.5.x</p>
<p>1.2.x</p>
<p>1.4.x</p>
<p>1.0.x</p>
<p>1.4.x</p>
<p>下载安装完SlikSVN后，配置系统环境变量，把SlikSVN安装目录下的bin目录加入到系统环境变量：</p>
<p><img alt="" src="http://dl.iteye.com/upload/picture/pic/99109/77dcfe08-39b7-3183-bc74-dbdb63af4e61.jpg" width="395" height="406" /></p>
<p>重新启动Eclipse，你会看到Subsvn已经有JavaHL库了</p>
<p><img title="点击查看原始大小图片" alt="" src="http://dl.iteye.com/upload/picture/pic/99111/0a0134d4-1127-355f-a98e-4c96f6532a32.jpg" width="700" height="610" /></p>
<p>转载至：<a title="http://hkp.iteye.com/blog/1174499" href="http://hkp.iteye.com/blog/1174499">http://hkp.iteye.com/blog/1174499</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.buguw.com/eclipse%e7%9a%84svn%e6%8f%92%e4%bb%b6%e6%89%be%e4%b8%8d%e5%88%b0-javahl.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>位运算简介及实用技巧（一）：基础篇</title>
		<link>http://www.buguw.com/%e4%bd%8d%e8%bf%90%e7%ae%97%e7%ae%80%e4%bb%8b%e5%8f%8a%e5%ae%9e%e7%94%a8%e6%8a%80%e5%b7%a7%ef%bc%88%e4%b8%80%ef%bc%89%ef%bc%9a%e5%9f%ba%e7%a1%80%e7%af%87.html</link>
		<comments>http://www.buguw.com/%e4%bd%8d%e8%bf%90%e7%ae%97%e7%ae%80%e4%bb%8b%e5%8f%8a%e5%ae%9e%e7%94%a8%e6%8a%80%e5%b7%a7%ef%bc%88%e4%b8%80%ef%bc%89%ef%bc%9a%e5%9f%ba%e7%a1%80%e7%af%87.html#comments</comments>
		<pubDate>Fri, 06 Apr 2012 02:34:33 +0000</pubDate>
		<dc:creator>tiger7456</dc:creator>
				<category><![CDATA[数据结构 & 算法]]></category>
		<category><![CDATA[位运算]]></category>

		<guid isPermaLink="false">http://www.buguw.com/?p=298</guid>
		<description><![CDATA[什么是位运算？ 程序中的所有数在计算机内存中都是以二进制的形式储存的。位运算说穿了，就是直接对整数在内存中的二进制位进行操作。比如，and运算本来是一个逻辑运算符，但整数与整数之间也可以进行and运算。举个例子，6的二进制是110，11的二进制是1011，那么6 and 11的结果就是2，它是二进制对应位进行逻辑运算的结果（0表示False，1表示True，空位都当0处理）： 110 AND 1011 ---------- 0010 --&#62; 2 由于位运算直接对内存数据进行操作，不需要转成十进制，因此处理速度非常快。当然有人会说，这个快了有什么用，计算6 and 11没有什么实际意义啊。这一系列的文章就将告诉你，位运算到底可以干什么，有些什么经典应用，以及如何用位运算优化你的程序。 Pascal和C中的位运算符号 下面的a和b都是整数类型，则： C语言   &#124; Pascal语言 ------- +------------- a &#38; b  &#124; a and b a &#124; b    &#124; a or b a ^ b   &#124; a xor b ~a      &#124; not a a &#60;&#60; b &#124; a shl b a &#62;&#62; [...]]]></description>
			<content:encoded><![CDATA[<p><strong>什么是位运算？</strong><br />
程序中的所有数在计算机内存中都是以二进制的形式储存的。位运算说穿了，就是直接对整数在内存中的二进制位进行操作。比如，and运算本来是一个逻辑运算符，但整数与整数之间也可以进行and运算。举个例子，6的二进制是110，11的二进制是1011，那么6 and 11的结果就是2，它是二进制对应位进行逻辑运算的结果（0表示False，1表示True，空位都当0处理）：<br />
110<br />
AND 1011<br />
----------<br />
0010 --&gt; 2<br />
由于位运算直接对内存数据进行操作，不需要转成十进制，因此处理速度非常快。当然有人会说，这个快了有什么用，计算6 and 11没有什么实际意义啊。这一系列的文章就将告诉你，位运算到底可以干什么，有些什么经典应用，以及如何用位运算优化你的程序。<br />
<strong>Pascal和C中的位运算符号</strong><br />
下面的a和b都是整数类型，则：<br />
C语言   | Pascal语言<br />
------- +-------------<br />
a &amp; b  | a and b<br />
a | b    | a or b<br />
a ^ b   | a xor b<br />
~a      | not a<br />
a &lt;&lt; b | a shl b<br />
a &gt;&gt; b | a shr b<br />
注意C中的逻辑运算和位运算符号是不同的。520|1314=1834，但520||1314=1，因为逻辑运算时520和1314都相当于True。同样的，!a和~a也是有区别的。<br />
<strong>各种位运算的使用</strong><br />
=== 1. and运算 ===<br />
and运算通常用于二进制取位操作，例如一个数 and 1的结果就是取二进制的最末位。这可以用来判断一个整数的奇偶，二进制的最末位为0表示该数为偶数，最末位为1表示该数为奇数.<br />
=== 2. or运算 ===<br />
or运算通常用于二进制特定位上的无条件赋值，例如一个数or 1的结果就是把二进制最末位强行变成1。如果需要把二进制最末位变成0，对这个数or 1之后再减一就可以了，其实际意义就是把这个数强行变成最接近的偶数。<br />
=== 3. xor运算 ===<br />
xor运算通常用于对二进制的特定一位进行取反操作，因为异或可以这样定义：0和1异或0都不变，异或1则取反。<br />
xor运算的逆运算是它本身，也就是说两次异或同一个数最后结果不变，即(a xor b) xor b = a。xor运算可以用于简单的加密，比如我想对我MM说1314520，但怕别人知道，于是双方约定拿我的生日19880516作为密钥。1314520 xor 19880516 = 20665500，我就把20665500告诉MM。MM再次计算20665500 xor 19880516的值，得到1314520，于是她就明白了我的企图。<br />
下面我们看另外一个东西。定义两个符号#和@（我怎么找不到那个圈里有个叉的字符），这两个符号互为逆运算，也就是说(x # y) @ y = x。现在依次执行下面三条命令，结果是什么？</p>
<p>&nbsp;</p>
<table width="400" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="400"><code>x &lt;- x # y<br />
y &lt;- x @ y<br />
x &lt;- x @ y</code></td>
</tr>
</tbody>
</table>
<p>执行了第一句后x变成了x # y。那么第二句实质就是y &lt;- x # y @ y，由于#和@互为逆运算，那么此时的y变成了原来的x。第三句中x实际上被赋值为(x # y) @ x，如果#运算具有交换律，那么赋值后x就变成最初的y了。这三句话的结果是，x和y的位置互换了。<br />
加法和减法互为逆运算，并且加法满足交换律。把#换成+，把@换成-，我们可以写出一个不需要临时变量的swap过程(Pascal)。</p>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">procedure</span> swap(<span style="color: #0000ff;">var</span> a,b:longint);</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">begin</span></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a:=a + b;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   b:=a - b;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a:=a - b;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">end</span>;</pre>
<p>好了，刚才不是说xor的逆运算是它本身吗？于是我们就有了一个看起来非常诡异的swap过程：</p>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">procedure</span> swap(<span style="color: #0000ff;">var</span> a,b:longint);</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">begin</span></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a:=a <span style="color: #0000ff;">xor</span> b;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   b:=a <span style="color: #0000ff;">xor</span> b;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a:=a <span style="color: #0000ff;">xor</span> b;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">end</span>;</pre>
<p>=== 4. not运算 ===</p>
<p>not运算的定义是把内存中的0和1全部取反。使用not运算时要格外小心，你需要注意整数类型有没有符号。如果not的对象是无符号整数（不能表示负数），那么得到的值就是它与该类型上界的差，因为无符号类型的数是用$0000到$FFFF依次表示的。下面的两个程序（仅语言不同）均返回65435。</p>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">var</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a:word;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">begin</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a:=100;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a:=<span style="color: #0000ff;">not</span> a;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   writeln(a);</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">end</span>.</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">#include &lt;stdio.h&gt;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">int</span> main()</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">{</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">short</span> a=100;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    a = ~a;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    printf( "<span style="color: #8b0000;">%d\n</span>", a );</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">return</span> 0;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">}</pre>
<p>&nbsp;</p>
<p>如果not的对象是有符号的整数，情况就不一样了，稍后我们会在“整数类型的储存”小节中提到。</p>
<p>=== 5. shl运算 ===</p>
<p>a shl b就表示把a转为二进制后左移b位（在后面添b个0）。例如100的二进制为1100100，而110010000转成十进制是400，那么100 shl 2 = 400。可以看出，a shl b的值实际上就是a乘以2的b次方，因为在二进制数后添一个0就相当于该数乘以2。</p>
<p>通常认为a shl 1比a * 2更快，因为前者是更底层一些的操作。因此程序中乘以2的操作请尽量用左移一位来代替。</p>
<p>定义一些常量可能会用到shl运算。你可以方便地用1 shl 16 - 1来表示65535。很多算法和数据结构要求数据规模必须是2的幂，此时可以用shl来定义Max_N等常量。</p>
<p>=== 6. shr运算 ===</p>
<p>和shl相似，a shr b表示二进制右移b位（去掉末b位），相当于a除以2的b次方（取整）。我们也经常用shr 1来代替div 2，比如二分查找、堆的插入操作等等。想办法用shr代替除法运算可以使程序效率大大提高。最大公约数的二进制算法用除以2操作来代替慢得出奇的mod运算，效率可以提高60%。</p>
<p><strong>位运算的简单应用</strong></p>
<p>有时我们的程序需要一个规模不大的Hash表来记录状态。比如，做数独时我们需要27个Hash表来统计每一行、每一列和每一个小九宫格里已经有哪些数了。此时，我们可以用27个小于2^9的整数进行记录。例如，一个只填了2和5的小九宫格就用数字18表示（二进制为000010010），而某一行的状态为511则表示这一行已经填满。需要改变状态时我们不需要把这个数转成二进制修改后再转回去，而是直接进行位操作。在搜索时，把状态表示成整数可以更好地进行判重等操作。<a href="http://www.vijos.cn/Problem_Show.asp?id=1197">这道题</a>是在搜索中使用位运算加速的经典例子。以后我们会看到更多的例子。</p>
<p>下面列举了一些常见的二进制位的变换操作。</p>
<p>&nbsp;</p>
<p>功能                    | 示例                                | 位运算</p>
<p>----------------------+---------------------------+--------------------</p>
<p>去掉最后一位     | (101101-&gt;10110)            | x shr 1</p>
<p>在最后加一个0   | (101101-&gt;1011010)         | x shl 1</p>
<p>在最后加一个1   | (101101-&gt;1011011)         | x shl 1+1</p>
<p>把最后一位变成1 | (101100-&gt;101101)          | x or 1</p>
<p>把最后一位变成0 | (101101-&gt;101100)          | x or 1-1</p>
<p>最后一位取反      | (101101-&gt;101100)          | x xor 1</p>
<p>把右数第k位变成1 | (101001-&gt;101101,k=3) | x or (1 shl (k-1))</p>
<p>把右数第k位变成0 | (101101-&gt;101001,k=3) | x and not (1 shl (k-1))</p>
<p>右数第k位取反 | (101001-&gt;101101,k=3) | x xor (1 shl (k-1))</p>
<p>取末三位 | (1101101-&gt;101) | x and 7</p>
<p>取末k位 | (1101101-&gt;1101,k=5) | x and (1 shl k-1)</p>
<p>取右数第k位 | (1101101-&gt;1,k=4) | x shr (k-1) and 1</p>
<p>把末k位变成1 | (101001-&gt;101111,k=4) | x or (1 shl k-1)</p>
<p>末k位取反 | (101001-&gt;100110,k=4) | x xor (1 shl k-1)</p>
<p>把右边连续的1变成0 | (100101111-&gt;100100000) | x and (x+1)</p>
<p>把右起第一个0变成1 | (100101111-&gt;100111111) | x or (x+1)</p>
<p>把右边连续的0变成1 | (11011000-&gt;11011111) | x or (x-1)</p>
<p>取右边连续的1 | (100101111-&gt;1111) | (x xor (x+1)) shr 1</p>
<p>去掉右起第一个1的左边 | (100101000-&gt;1000) | x and (x xor (x-1))</p>
<p>最后这一个在树状数组中会用到。</p>
<p><strong>Pascal和C中的16进制表示</strong></p>
<p>Pascal中需要在16进制数前加$符号表示，C中需要在前面加0x来表示。这个以后我们会经常用到。</p>
<p><strong>整数类型的储存</strong></p>
<p>我们前面所说的位运算都没有涉及负数，都假设这些运算是在unsigned/word类型（只能表示正数的整型）上进行操作。但计算机如何处理有正负符号的整数类型呢？下面两个程序都是考察16位整数的储存方式（只是语言不同）。</p>
<p>&nbsp;</p>
<table width="400" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="400">
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">var</span></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a,b:integer;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">begin</span></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a:=$0000;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   b:=$0001;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   write(a,' ',b,' ');</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a:=$FFFE;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   b:=$FFFF;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   write(a,' ',b,' ');</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   a:=$7FFF;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   b:=$8000;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">   writeln(a,' ',b);</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">end</span>.</pre>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table width="400" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="400">
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">#include &lt;stdio.h&gt;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">int</span> main()</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">{</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">short</span> <span style="color: #0000ff;">int</span> a, b;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    a = 0x0000;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    b = 0x0001;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    printf( "<span style="color: #8b0000;">%d %d </span>", a, b );</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    a = 0xFFFE;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    b = 0xFFFF;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    printf( "<span style="color: #8b0000;">%d %d </span>", a, b );</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    a = 0x7FFF;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    b = 0x8000;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    printf( "<span style="color: #8b0000;">%d %d\n</span>", a, b );</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">return</span> 0;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">}</pre>
</td>
</tr>
</tbody>
</table>
<p>两个程序的输出均为0 1 -2 -1 32767 -32768。其中前两个数是内存值最小的时候，中间两个数则是内存值最大的时候，最后输出的两个数是正数与负数的分界处。由此你可以清楚地看到计算机是如何储存一个整数的：计算机用$0000到$7FFF依次表示0到32767的数，剩下的$8000到$FFFF依次表示-32768到-1的数。32位有符号整数的储存方式也是类似的。稍加注意你会发现，二进制的第一位是用来表示正负号的，0表示正，1表示负。这里有一个问题：0本来既不是正数，也不是负数，但它占用了$0000的位置，因此有符号的整数类型范围中正数个数比负数少一个。对一个有符号的数进行not运算后，最高位的变化将导致正负颠倒，并且数的绝对值会差1。也就是说，not a实际上等于-a-1。这种整数储存方式叫做“补码”。</p>
<p>转自：<a title="http://www.matrix67.com/blog/archives/263" href="http://www.matrix67.com/blog/archives/263">http://www.matrix67.com/blog/archives/263</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.buguw.com/%e4%bd%8d%e8%bf%90%e7%ae%97%e7%ae%80%e4%bb%8b%e5%8f%8a%e5%ae%9e%e7%94%a8%e6%8a%80%e5%b7%a7%ef%bc%88%e4%b8%80%ef%bc%89%ef%bc%9a%e5%9f%ba%e7%a1%80%e7%af%87.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hamming weight高效统计整数二进制表示中1的个数</title>
		<link>http://www.buguw.com/hamming-weight%e9%ab%98%e6%95%88%e7%bb%9f%e8%ae%a1%e6%95%b4%e6%95%b0%e4%ba%8c%e8%bf%9b%e5%88%b6%e8%a1%a8%e7%a4%ba%e4%b8%ad1%e7%9a%84%e4%b8%aa%e6%95%b0.html</link>
		<comments>http://www.buguw.com/hamming-weight%e9%ab%98%e6%95%88%e7%bb%9f%e8%ae%a1%e6%95%b4%e6%95%b0%e4%ba%8c%e8%bf%9b%e5%88%b6%e8%a1%a8%e7%a4%ba%e4%b8%ad1%e7%9a%84%e4%b8%aa%e6%95%b0.html#comments</comments>
		<pubDate>Thu, 05 Apr 2012 03:49:28 +0000</pubDate>
		<dc:creator>tiger7456</dc:creator>
				<category><![CDATA[数据结构 & 算法]]></category>

		<guid isPermaLink="false">http://www.buguw.com/?p=289</guid>
		<description><![CDATA[这是一个很有意思的问题，也是在面试中最容易被问到的问题之一。这个问题有个正式的名字叫Hamming_weight，而且wikipedia上也提供了很好的位运算解决的方法，这个下面也会提到。 解决这个问题的第一想法是一位一位的观察，判断是否为1，是则计数器加一，否则跳到下一位，于是很容易有这样的程序。 int test(int n){ int count=0; while(n != 0){ if(n%2 ==1) count++; n /= 2; } return count;} 或者和其等价的位运算版本： int test(int n){ int count=0; while(n != 0){ count += n&#38;1; n &#62;&#62;= 1; } return count; } 这样的方法复杂度为二进制的位数，即，于是可是想一下，有没有只与二进制中1的位数相关的算法呢。 可以考虑每次找到从最低位开始遇到的第一个1，计数，再把它清零，清零的位运算操作是与一个零，但是在有1的这一位与零的操作要同时不影响未统计过的位数和已经统计过的位数，于是可以有这样一个操作 n&#38;(n-1) ，这个操作对比当前操作位高的位没有影响，对低位则完全清零。拿6（110）来做例子，第一次 110&#38;101=100，这次操作成功的把从低位起第一个1消掉了，同时计数器加1，第二次100&#38;011=000，同理又统计了高位的一个1，此时n已变为0，不需要再继续了，于是110中有2个1。 代码如下： int test(int n){ int count=0; while(n != 0){ n &#38;= n-1; [...]]]></description>
			<content:encoded><![CDATA[<p>这是一个很有意思的问题，也是在面试中最容易被问到的问题之一。这个问题有个正式的名字叫<a href="http://en.wikipedia.org/wiki/Hamming_weight">Hamming_weight</a>，而且wikipedia上也提供了很好的位运算解决的方法，这个下面也会提到。</p>
<p>解决这个问题的第一想法是一位一位的观察，判断是否为1，是则计数器加一，否则跳到下一位，于是很容易有这样的程序。</p>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">int</span> test(<span style="color: #0000ff;">int</span> n){</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  <span style="color: #0000ff;">int</span> count=0;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  <span style="color: #0000ff;">while</span>(n != 0){</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">      <span style="color: #0000ff;">if</span>(n%2 ==1)</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">      count++;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">      n /= 2;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> }</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"> <span style="color: #0000ff;">return</span> count;}</pre>
<p>或者和其等价的位运算版本：</p>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">int</span> test(<span style="color: #0000ff;">int</span> n){</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">int</span> count=0;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">while</span>(n != 0){</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">        count += n&amp;1;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">        n &gt;&gt;= 1;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    }</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">return</span> count;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">}</pre>
<p>这样的方法复杂度为二进制的位数，即<img src="http://latex.codecogs.com/gif.latex?\log_2n" alt="" />，于是可是想一下，有没有只与二进制中1的位数相关的算法呢。</p>
<p>可以考虑每次找到从最低位开始遇到的第一个1，计数，再把它清零，清零的位运算操作是与一个零，但是在有1的这一位与零的操作要同时不影响未统计过的位数和已经统计过的位数，于是可以有这样一个操作 n&amp;(n-1) ，这个操作对比当前操作位高的位没有影响，对低位则完全清零。拿6（110）来做例子，第一次 110&amp;101=100，这次操作成功的把从低位起第一个1消掉了，同时计数器加1，第二次100&amp;011=000，同理又统计了高位的一个1，此时n已变为0，不需要再继续了，于是110中有2个1。</p>
<p>代码如下：</p>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">int</span> test(<span style="color: #0000ff;">int</span> n){</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">int</span> count=0;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">while</span>(n != 0){</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">        n &amp;= n-1;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">        count ++;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    }</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">return</span> count;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">}</pre>
<p>&nbsp;</p>
<p>还有查表法：</p>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> idx[256]={0,1,1,,8}<span style="color: #008000;">//0~255中含1的个数</span></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">int</span> OneCount(<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> x)</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">{</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  <span style="color: #0000ff;">int</span> count=0;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  <span style="color: #0000ff;">for</span>(; x&gt;0; x&gt;&gt;=8)</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">     count+=idx[x&amp;255];</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  <span style="color: #0000ff;">return</span> count;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">}</pre>
<p>上面算法最多只需要4次循环，用空间换取时间。</p>
<p>这种算法的另一种形式：</p>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> idx[256]={0,1,1,..,8}</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">int</span> OneCount(<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> x)</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">{</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span>* p=(<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">char</span>*)&amp;x;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">  <span style="color: #0000ff;">return</span> idx[*p]+idx[*(p+1)]+idx[*(p+2)]+idx[*(p+3)];</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">}</pre>
<p>&nbsp;</p>
<p>这几个方法虽然也用到了位运算，但是并没有体现其神奇之处，下面这个版本则彰显位运算的强大能力，若不告诉这个函数的功能，一般一眼看上去是想不到这是做什么的，这也是wikipedia上给出的计算hamming_weight方法。</p>
<p>&nbsp;</p>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><span style="color: #0000ff;">int</span> test(<span style="color: #0000ff;">int</span> n){</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    n = (n&amp;0x55555555) + ((n&gt;&gt;1)&amp;0x55555555);</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    n = (n&amp;0x33333333) + ((n&gt;&gt;2)&amp;0x33333333);</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    n = (n&amp;0x0f0f0f0f) + ((n&gt;&gt;4)&amp;0x0f0f0f0f);</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    n = (n&amp;0x00ff00ff) + ((n&gt;&gt;8)&amp;0x00ff00ff);</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    n = (n&amp;0x0000ffff) + ((n&gt;&gt;16)&amp;0x0000ffff);</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">    <span style="color: #0000ff;">return</span> n;</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">}</pre>
<p>有循环，5个位运算语句，一次搞定。</p>
<p>比如这个例子，143的二进制表示是10001111，这里只有8位，高位的0怎么进行与的位运算也是0，所以只考虑低位的运算，按照这个算法走一次</p>
<p>+---+---+---+---+---+---+---+---+</p>
<p>| 1 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | &lt;---143</p>
<p>+---+---+---+---+---+---+---+---+</p>
<p>| 0 1 | 0 0 | 1 0 | 1 0 | &lt;---第一次运算后</p>
<p>+-------+-------+-------+-------+</p>
<p>| 0 0 0 1 | 0 1 0 0 | &lt;---第二次运算后</p>
<p>+---------------+---------------+</p>
<p>| 0 0 0 0 0 1 0 1 | &lt;---第三次运算后，得数为5</p>
<p>+-------------------------------+</p>
<p>这里运用了分治的思想，先计算每对相邻的2位中有几个1，再计算每相邻的4位中有几个1，下来8位，16位，32位，因为2^5＝32，所以对于32位的机器，5条位运算语句就够了。</p>
<p>像这里第二行第一个格子中，01就表示前两位有1个1，00表示下来的两位中没有1，其实同理。再下来01+00=0001表示前四位中有1个1，同样的10+10=0100表示低四位中有4个1，最后一步0001+0100=00000101表示整个8位中有5个1。</p>
<p>Hamming weight原理讲解：<a href="http://www.buguw.com/wp-content/uploads/2012/04/Hamming-weight.pdf">Hamming weight</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.buguw.com/hamming-weight%e9%ab%98%e6%95%88%e7%bb%9f%e8%ae%a1%e6%95%b4%e6%95%b0%e4%ba%8c%e8%bf%9b%e5%88%b6%e8%a1%a8%e7%a4%ba%e4%b8%ad1%e7%9a%84%e4%b8%aa%e6%95%b0.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL索引背后的数据结构及算法原理</title>
		<link>http://www.buguw.com/mysql%e7%b4%a2%e5%bc%95%e8%83%8c%e5%90%8e%e7%9a%84%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e5%8f%8a%e7%ae%97%e6%b3%95%e5%8e%9f%e7%90%86.html</link>
		<comments>http://www.buguw.com/mysql%e7%b4%a2%e5%bc%95%e8%83%8c%e5%90%8e%e7%9a%84%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e5%8f%8a%e7%ae%97%e6%b3%95%e5%8e%9f%e7%90%86.html#comments</comments>
		<pubDate>Fri, 30 Mar 2012 09:23:00 +0000</pubDate>
		<dc:creator>tiger7456</dc:creator>
				<category><![CDATA[数据结构 & 算法]]></category>

		<guid isPermaLink="false">http://www.buguw.com/?p=287</guid>
		<description><![CDATA[原文地址：http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-1 摘要 本文以MySQL数据库为研究对象，讨论与数据库索引相关的一些话题。特别需要说明的是，MySQL支持诸多存储引擎，而各种存储引擎对索引的支持也各不相同，因此MySQL数据库支持多种索引类型，如BTree索引，哈希索引，全文索引等等。为了避免混乱，本文将只关注于BTree索引，因为这是平常使用MySQL时主要打交道的索引，至于哈希索引和全文索引本文暂不讨论。 文章主要内容分为三个部分。 第一部分主要从数据结构及算法理论层面讨论MySQL数据库索引的数理基础。 第二部分结合MySQL数据库中MyISAM和InnoDB数据存储引擎中索引的架构实现讨论聚集索引、非聚集索引及覆盖索引等话题。 第三部分根据上面的理论基础，讨论MySQL中高性能使用索引的策略。 摘要 数据结构及算法基础 索引的本质 B-Tree和B+Tree 为什么使用B-Tree（B+Tree） MySQL索引实现 MyISAM索引实现 InnoDB索引实现 索引使用策略及优化 示例数据库 最左前缀原理与相关优化 索引选择性与前缀索引 InnoDB的主键选择与插入优化 后记 参考文献 数据结构及算法基础 索引的本质 MySQL官方对索引的定义为：索引（Index）是帮助MySQL高效获取数据的数据结构。提取句子主干，就可以得到索引的本质：索引是数据结构。 我们知道，数据库查询是数据库的最主要功能之一。我们都希望查询数据的速度能尽可能的快，因此数据库系统的设计者会从查询算法的角度进行优化。最基本的查询算法当然是顺序查找（linear search），这种复杂度为O(n)的算法在数据量很大时显然是糟糕的，好在计算机科学的发展提供了很多更优秀的查找算法，例如二分查找（binary search）、二叉树查找（binary tree search）等。如果稍微分析一下会发现，每种查找算法都只能应用于特定的数据结构之上，例如二分查找要求被检索数据有序，而二叉树查找只能应用于二叉查找树上，但是数据本身的组织结构不可能完全满足各种数据结构（例如，理论上不可能同时将两列都按顺序进行组织），所以，在数据之外，数据库系统还维护着满足特定查找算法的数据结构，这些数据结构以某种方式引用（指向）数据，这样就可以在这些数据结构上实现高级查找算法。这种数据结构，就是索引。 看一个例子： 图1 图1展示了一种可能的索引方式。左边是数据表，一共有两列七条记录，最左边的是数据记录的物理地址（注意逻辑上相邻的记录在磁盘上也并不是一定物理相邻的）。为了加快Col2的查找，可以维护一个右边所示的二叉查找树，每个节点分别包含索引键值和一个指向对应数据记录物理地址的指针，这样就可以运用二叉查找在O(log2n)的复杂度内获取到相应数据。 虽然这是一个货真价实的索引，但是实际的数据库系统几乎没有使用二叉查找树或其进化品种红黑树（red-black tree）实现的，原因会在下文介绍。 B-Tree和B+Tree 目前大部分数据库系统及文件系统都采用B-Tree或其变种B+Tree作为索引结构，在本文的下一节会结合存储器原理及计算机存取原理讨论为什么B-Tree和B+Tree在被如此广泛用于索引，这一节先单纯从数据结构角度描述它们。 B-Tree 为了描述B-Tree，首先定义一条数据记录为一个二元组[key, data]，key为记录的键值，对于不同数据记录，key是互不相同的；data为数据记录除key外的数据。那么B-Tree是满足下列条件的数据结构： d为大于1的一个正整数，称为B-Tree的度。 h为一个正整数，称为B-Tree的高度。 每个非叶子节点由n-1个key和n个指针组成，其中d&#60;=n&#60;=2d。 每个叶子节点最少包含一个key和两个指针，最多包含2d-1个key和2d个指针，叶节点的指针均为null 。 所有叶节点具有相同的深度，等于树高h。 key和指针互相间隔，节点两端是指针。 一个节点中的key从左到右非递减排列。 所有节点组成树结构。 每个指针要么为null，要么指向另外一个节点。 如果某个指针在节点node最左边且不为null，则其指向节点的所有key小于v(key1)，其中v(key1)为node的第一个key的值。 如果某个指针在节点node最右边且不为null，则其指向节点的所有key大于v(keym)，其中v(keym)为node的最后一个key的值。 如果某个指针在节点node的左右相邻key分别是keyi和keyi+1且不为null，则其指向节点的所有key小于v(keyi+1)且大于v(keyi)。 图2是一个d=2的B-Tree示意图。 图2 [...]]]></description>
			<content:encoded><![CDATA[<p>原文地址：<a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-1">http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-1</a></p>
<h3>摘要</h3>
<p>本文以MySQL数据库为研究对象，讨论与数据库索引相关的一些话题。特别需要说明的是，MySQL支持诸多存储引擎，而各种存储引擎对索引的支持也各不相同，因此MySQL数据库支持多种索引类型，如BTree索引，哈希索引，全文索引等等。为了避免混乱，本文将只关注于BTree索引，因为这是平常使用MySQL时主要打交道的索引，至于哈希索引和全文索引本文暂不讨论。</p>
<p>文章主要内容分为三个部分。</p>
<p>第一部分主要从数据结构及算法理论层面讨论MySQL数据库索引的数理基础。</p>
<p>第二部分结合MySQL数据库中MyISAM和InnoDB数据存储引擎中索引的架构实现讨论聚集索引、非聚集索引及覆盖索引等话题。</p>
<p>第三部分根据上面的理论基础，讨论MySQL中高性能使用索引的策略。</p>
<p><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-1">摘要</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2">数据结构及算法基础</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-1">索引的本质</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-2">B-Tree和B+Tree</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-3">为什么使用B-Tree（B+Tree）</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-3">MySQL索引实现</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-3-1">MyISAM索引实现</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-3-2">InnoDB索引实现</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-4">索引使用策略及优化</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-4-1">示例数据库</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-4-2">最左前缀原理与相关优化</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-4-3">索引选择性与前缀索引</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-4-4">InnoDB的主键选择与插入优化</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-5">后记</a>    <br /><a href="http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-6">参考文献</a></p>
<h3><a name="nav-2"></a>数据结构及算法基础</h3>
<h4><a name="nav-2-1"></a>索引的本质</h4>
<p>MySQL官方对索引的定义为：<strong>索引（Index）是帮助MySQL高效获取数据的数据结构。</strong>提取句子主干，就可以得到索引的本质：索引是数据结构。</p>
<p>我们知道，数据库查询是数据库的最主要功能之一。我们都希望查询数据的速度能尽可能的快，因此数据库系统的设计者会从查询算法的角度进行优化。最基本的查询算法当然是<a href="http://en.wikipedia.org/wiki/Linear_search">顺序查找</a>（linear search），这种复杂度为O(n)的算法在数据量很大时显然是糟糕的，好在计算机科学的发展提供了很多更优秀的查找算法，例如<a href="http://en.wikipedia.org/wiki/Binary_search_algorithm">二分查找</a>（binary search）、<a href="http://en.wikipedia.org/wiki/Binary_search_tree">二叉树查找</a>（binary tree search）等。如果稍微分析一下会发现，每种查找算法都只能应用于特定的数据结构之上，例如二分查找要求被检索数据有序，而二叉树查找只能应用于<a href="http://en.wikipedia.org/wiki/Binary_search_tree">二叉查找树</a>上，但是数据本身的组织结构不可能完全满足各种数据结构（例如，理论上不可能同时将两列都按顺序进行组织），所以，<strong>在数据之外，数据库系统还维护着满足特定查找算法的数据结构，这些数据结构以某种方式引用（指向）数据，这样就可以在这些数据结构上实现高级查找算法。这种数据结构，就是索引</strong>。</p>
<p>看一个例子：</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image2.png" width="588" height="297" /></p>
<p><strong>图1</strong></p>
<p>图1展示了一种可能的索引方式。左边是数据表，一共有两列七条记录，最左边的是数据记录的物理地址（注意逻辑上相邻的记录在磁盘上也并不是一定物理相邻的）。为了加快Col2的查找，可以维护一个右边所示的二叉查找树，每个节点分别包含索引键值和一个指向对应数据记录物理地址的指针，这样就可以运用二叉查找在O(log<sub>2</sub>n)的复杂度内获取到相应数据。</p>
<p>虽然这是一个货真价实的索引，但是实际的数据库系统几乎没有使用二叉查找树或其进化品种<a href="http://en.wikipedia.org/wiki/Red-black_tree">红黑树</a>（red-black tree）实现的，原因会在下文介绍。</p>
<h4><a name="nav-2-2"></a>B-Tree和B+Tree</h4>
<p>目前大部分数据库系统及文件系统都采用B-Tree或其变种B+Tree作为索引结构，在本文的下一节会结合存储器原理及计算机存取原理讨论为什么B-Tree和B+Tree在被如此广泛用于索引，这一节先单纯从数据结构角度描述它们。</p>
<h5>B-Tree</h5>
<p>为了描述B-Tree，首先定义一条数据记录为一个二元组[key, data]，key为记录的键值，对于不同数据记录，key是互不相同的；data为数据记录除key外的数据。那么B-Tree是满足下列条件的数据结构：</p>
<ol>
<li>d为大于1的一个正整数，称为B-Tree的度。 </li>
<li>h为一个正整数，称为B-Tree的高度。 </li>
<li>每个非叶子节点由n-1个key和n个指针组成，其中d&lt;=n&lt;=2d。 </li>
<li>每个叶子节点最少包含一个key和两个指针，最多包含2d-1个key和2d个指针，叶节点的指针均为null 。 </li>
<li>所有叶节点具有相同的深度，等于树高h。 </li>
<li>key和指针互相间隔，节点两端是指针。 </li>
<li>一个节点中的key从左到右非递减排列。 </li>
<li>所有节点组成树结构。 </li>
<li>每个指针要么为null，要么指向另外一个节点。 </li>
<li>如果某个指针在节点node最左边且不为null，则其指向节点的所有key小于v(key<sub>1</sub>)，其中v(key<sub>1</sub>)为node的第一个key的值。 </li>
<li>如果某个指针在节点node最右边且不为null，则其指向节点的所有key大于v(key<sub>m</sub>)，其中v(key<sub>m</sub>)为node的最后一个key的值。 </li>
<li>如果某个指针在节点node的左右相邻key分别是key<sub>i</sub>和key<sub>i+1</sub>且不为null，则其指向节点的所有key小于v(key<sub>i+1</sub>)且大于v(key<sub>i</sub>)。</li>
</ol>
<p>图2是一个d=2的B-Tree示意图。</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image281.png" width="458" height="115" /></p>
<p><strong>图2</strong></p>
<p>由于B-Tree的特性，在B-Tree中按key检索数据的算法非常直观：首先从根节点进行二分查找，如果找到则返回对应节点的data，否则对相应区间的指针指向的节点递归进行查找，直到找到节点或找到null指针，前者查找成功，后者查找失败。B-Tree上查找算法的伪代码如下：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>
<p>11</p>
<p>12</p>
<p>13</p>
<p>14</p>
<p><code>BTree_Search(node, key)</code></p>
<p><code>{</code></p>
<p><code></code><code>if</code><code>(node == null) </code><code>return</code> <code>null;</code></p>
<p><code></code><code>foreach(node.key)</code></p>
<p><code></code><code>{</code></p>
<p><code></code><code>if</code><code>(node.key[i] == key) </code><code>return</code> <code>node.data[i];</code></p>
<p><code></code><code>if</code><code>(node.key[i] &gt; key) </code><code>return</code> <code>BTree_Search(point[i]-&gt;node);</code></p>
<p><code></code><code>}</code></p>
<p><code></code><code>return</code> <code>BTree_Search(point[i+1]-&gt;node);</code></p>
<p><code>}</code></p>
<p><code>data = BTree_Search(root, my_key);</code></p>
<p>关于B-Tree有一系列有趣的性质，例如<strong>一个度为d的B-Tree，设其索引N个key，则其树高h的上限为log<sub>d</sub>((N+1)/2)，检索一个key，其查找节点个数的渐进复杂度为O(log<sub>d</sub>N)。</strong>从这点可以看出，B-Tree是一个非常有效率的索引数据结构。</p>
<p>另外，由于插入删除新的数据记录会破坏B-Tree的性质，因此在插入删除时，需要对树进行一个分裂、合并、转移等操作以保持B-Tree性质，本文不打算完整讨论B-Tree这些内容，因为已经有许多资料详细说明了B-Tree的数学性质及插入删除算法，有兴趣的朋友可以在本文末的参考文献一栏找到相应的资料进行阅读。</p>
<h5>B+Tree</h5>
<p>B-Tree有许多变种，其中最常见的是B+Tree，例如MySQL就普遍使用B+Tree实现其索引结构。</p>
<p>与B-Tree相比，B+Tree有以下不同点：</p>
<ol>
<li>每个节点的指针上限为2d而不是2d+1。 </li>
<li>内节点不存储data，只存储key；叶子节点不存储指针。</li>
</ol>
<p>图3是一个简单的B+Tree示意。</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/11/image4.png" width="543" height="191" /></p>
<p><strong>图3</strong></p>
<p>由于并不是所有节点都具有相同的域，因此B+Tree中叶节点和内节点一般大小不同。这点与B-Tree不同，虽然B-Tree中不同节点存放的key和指针可能数量不一致，但是每个节点的域和上限是一致的，所以在实现中B-Tree往往对每个节点申请同等大小的空间。</p>
<p>一般来说，B+Tree比B-Tree更适合实现外存储索引结构，具体原因与外存储器原理及计算机存取原理有关，将在下面讨论。</p>
<h5>带有顺序访问指针的B+Tree</h5>
<p>一般在数据库系统或文件系统中使用的B+Tree结构都在经典B+Tree的基础上进行了优化，增加了顺序访问指针。</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image381.png" width="543" height="191" /></p>
<p><strong>图4</strong></p>
<p>如图4所示，在B+Tree的每个叶子节点增加一个指向相邻叶子节点的指针，就形成了带有顺序访问指针的B+Tree。做这个优化的目的是为了提高区间访问的性能，例如图4中如果要查询key为从18到49的所有数据记录，当找到18后，只需顺着节点和指针顺序遍历就可以一次性访问到所有数据节点，极大提到了区间查询效率。</p>
<p>这一节对B-Tree和B+Tree进行了一个简单的介绍，下一节结合存储器存取原理介绍为什么目前B+Tree是数据库系统实现索引的首选数据结构。</p>
<h4><a name="nav-2-3"></a>为什么使用B-Tree（B+Tree）</h4>
<p>上文说过，红黑树等数据结构也可以用来实现索引，但是文件系统及数据库系统普遍采用B-/+Tree作为索引结构，这一节将结合计算机组成原理相关知识讨论B-/+Tree作为索引的理论基础。</p>
<p>一般来说，索引本身也很大，不可能全部存储在内存中，因此索引往往以索引文件的形式存储的磁盘上。这样的话，索引查找过程中就要产生磁盘I/O消耗，相对于内存存取，I/O存取的消耗要高几个数量级，所以评价一个数据结构作为索引的优劣最重要的指标就是在查找过程中磁盘I/O操作次数的渐进复杂度。换句话说，索引的结构组织要尽量减少查找过程中磁盘I/O的存取次数。下面先介绍内存和磁盘存取原理，然后再结合这些原理分析B-/+Tree作为索引的效率。</p>
<h5>主存存取原理</h5>
<p>目前计算机使用的主存基本都是随机读写存储器（RAM），现代RAM的结构和存取原理比较复杂，这里本文抛却具体差别，抽象出一个十分简单的存取模型来说明RAM的工作原理。</p>
<p><a href="http://www.codinglabs.org/wp-content/uploads/2011/10/image4.png" class="highslide-image" onclick="return hs.expand(this);"><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image4_thumb.png" width="382" height="229" /></a></p>
<p><strong>图5</strong></p>
<p>从抽象角度看，主存是一系列的存储单元组成的矩阵，每个存储单元存储固定大小的数据。每个存储单元有唯一的地址，现代主存的编址规则比较复杂，这里将其简化成一个二维地址：通过一个行地址和一个列地址可以唯一定位到一个存储单元。图5展示了一个4 x 4的主存模型。</p>
<p>主存的存取过程如下：</p>
<p>当系统需要读取主存时，则将地址信号放到地址总线上传给主存，主存读到地址信号后，解析信号并定位到指定存储单元，然后将此存储单元数据放到数据总线上，供其它部件读取。</p>
<p>写主存的过程类似，系统将要写入单元地址和数据分别放在地址总线和数据总线上，主存读取两个总线的内容，做相应的写操作。</p>
<p>这里可以看出，主存存取的时间仅与存取次数呈线性关系，因为不存在机械操作，两次存取的数据的“距离”不会对时间有任何影响，例如，先取A0再取A1和先取A0再取D3的时间消耗是一样的。</p>
<h5>磁盘存取原理</h5>
<p>上文说过，索引一般以文件形式存储在磁盘上，索引检索需要磁盘I/O操作。与主存不同，磁盘I/O存在机械运动耗费，因此磁盘I/O的时间消耗是巨大的。</p>
<p>图6是磁盘的整体结构示意图。</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image6111.png" width="267" height="172" /></p>
<p><strong>图6</strong></p>
<p>一个磁盘由大小相同且同轴的圆形盘片组成，磁盘可以转动（各个磁盘必须同步转动）。在磁盘的一侧有磁头支架，磁头支架固定了一组磁头，每个磁头负责存取一个磁盘的内容。磁头不能转动，但是可以沿磁盘半径方向运动（实际是斜切向运动），每个磁头同一时刻也必须是同轴的，即从正上方向下看，所有磁头任何时候都是重叠的（不过目前已经有多磁头独立技术，可不受此限制）。</p>
<p>图7是磁盘结构的示意图。</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image1111.png" width="269" height="246" /></p>
<p><strong>图7</strong></p>
<p>盘片被划分成一系列同心环，圆心是盘片中心，每个同心环叫做一个磁道，所有半径相同的磁道组成一个柱面。磁道被沿半径线划分成一个个小的段，每个段叫做一个扇区，每个扇区是磁盘的最小存储单元。为了简单起见，我们下面假设磁盘只有一个盘片和一个磁头。</p>
<p>当需要从磁盘读取数据时，系统会将数据逻辑地址传给磁盘，磁盘的控制电路按照寻址逻辑将逻辑地址翻译成物理地址，即确定要读的数据在哪个磁道，哪个扇区。为了读取这个扇区的数据，需要将磁头放到这个扇区上方，为了实现这一点，磁头需要移动对准相应磁道，这个过程叫做寻道，所耗费时间叫做寻道时间，然后磁盘旋转将目标扇区旋转到磁头下，这个过程耗费的时间叫做旋转时间。</p>
<h5>局部性原理与磁盘预读</h5>
<p>由于存储介质的特性，磁盘本身存取就比主存慢很多，再加上机械运动耗费，磁盘的存取速度往往是主存的几百分分之一，因此为了提高效率，要尽量减少磁盘I/O。为了达到这个目的，磁盘往往不是严格按需读取，而是每次都会预读，即使只需要一个字节，磁盘也会从这个位置开始，顺序向后读取一定长度的数据放入内存。这样做的理论依据是计算机科学中著名的局部性原理：</p>
<p>当一个数据被用到时，其附近的数据也通常会马上被使用。</p>
<p>程序运行期间所需要的数据通常比较集中。</p>
<p>由于磁盘顺序读取的效率很高（不需要寻道时间，只需很少的旋转时间），因此对于具有局部性的程序来说，预读可以提高I/O效率。</p>
<p>预读的长度一般为页（page）的整倍数。页是计算机管理存储器的逻辑块，硬件及操作系统往往将主存和磁盘存储区分割为连续的大小相等的块，每个存储块称为一页（在许多操作系统中，页得大小通常为4k），主存和磁盘以页为单位交换数据。当程序要读取的数据不在主存中时，会触发一个缺页异常，此时系统会向磁盘发出读盘信号，磁盘会找到数据的起始位置并向后连续读取一页或几页载入内存中，然后异常返回，程序继续运行。</p>
<h5>B-/+Tree索引的性能分析</h5>
<p>到这里终于可以分析B-/+Tree索引的性能了。</p>
<p>上文说过一般使用磁盘I/O次数评价索引结构的优劣。先从B-Tree分析，根据B-Tree的定义，可知检索一次最多需要访问h个节点。数据库系统的设计者巧妙利用了磁盘预读原理，将一个节点的大小设为等于一个页，这样每个节点只需要一次I/O就可以完全载入。为了达到这个目的，在实际实现B-Tree还需要使用如下技巧：</p>
<p>每次新建节点时，直接申请一个页的空间，这样就保证一个节点物理上也存储在一个页里，加之计算机存储分配都是按页对齐的，就实现了一个node只需一次I/O。</p>
<p><strong>B-Tree中一次检索最多需要h-1次I/O（根节点常驻内存），渐进复杂度为O(h)=O(log<sub>d</sub>N)。</strong>一般实际应用中，出度d是非常大的数字，通常超过100，因此h非常小（通常不超过3）。</p>
<p>综上所述，用B-Tree作为索引结构效率是非常高的。</p>
<p>而红黑树这种结构，h明显要深的多。由于逻辑上很近的节点（父子）物理上可能很远，无法利用局部性，所以红黑树的I/O渐进复杂度也为O(h)，效率明显比B-Tree差很多。</p>
<p>上文还说过，B+Tree更适合外存索引，原因和内节点出度d有关。从上面分析可以看到，d越大索引的性能越好，而出度的上限取决于节点内key和data的大小：</p>
<p>d<sub>max</sub> = <em><strong>floor</strong></em>(pagesize / (keysize + datasize + pointsize)) <em>(pagesize – dmax &gt;= pointsize)</em></p>
<p>或</p>
<p>d<sub>max</sub> = <em><strong>floor</strong></em>(pagesize / (keysize + datasize + pointsize)) – 1 <em>(pagesize – dmax &lt; pointsize)</em></p>
<p>floor表示向下取整。由于B+Tree内节点去掉了data域，因此可以拥有更大的出度，拥有更好的性能。</p>
<p>这一章从理论角度讨论了与索引相关的数据结构与算法问题，下一章将讨论B+Tree是如何具体实现为MySQL中索引，同时将结合MyISAM和InnDB存储引擎介绍非聚集索引和聚集索引两种不同的索引实现形式。</p>
<h3><a name="nav-3"></a>MySQL索引实现</h3>
<p>在MySQL中，索引属于存储引擎级别的概念，不同存储引擎对索引的实现方式是不同的，本文主要讨论MyISAM和InnoDB两个存储引擎的索引实现方式。</p>
<h4><a name="nav-3-1"></a>MyISAM索引实现</h4>
<p>MyISAM引擎使用B+Tree作为索引结构，叶节点的data域存放的是数据记录的地址。下图是MyISAM索引的原理图：</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image11111.png" width="664" height="534" /></p>
<p><strong>图8</strong></p>
<p>这里设表一共有三列，假设我们以Col1为主键，则图8是一个MyISAM表的主索引（Primary key）示意。可以看出MyISAM的索引文件仅仅保存数据记录的地址。<strong>在MyISAM中，主索引和辅助索引（Secondary key）在结构上没有任何区别，只是主索引要求key是唯一的，而辅助索引的key可以重复。</strong>如果我们在Col2上建立一个辅助索引，则此索引的结构如下图所示：</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image61.png" width="664" height="534" /></p>
<p><strong>图9</strong></p>
<p>同样也是一颗B+Tree，data域保存数据记录的地址。因此，<strong>MyISAM中索引检索的算法为首先按照B+Tree搜索算法搜索索引，如果指定的Key存在，则取出其data域的值，然后以data域的值为地址，读取相应数据记录。</strong></p>
<p>MyISAM的索引方式也叫做“非聚集”的，之所以这么称呼是为了与InnoDB的聚集索引区分。</p>
<h4><a name="nav-3-2"></a>InnoDB索引实现</h4>
<p>虽然InnoDB也使用B+Tree作为索引结构，但具体实现方式却与MyISAM截然不同。</p>
<p><strong>第一个重大区别是InnoDB的数据文件本身就是索引文件。</strong>从上文知道，MyISAM索引文件和数据文件是分离的，索引文件仅保存数据记录的地址。而在InnoDB中，表数据文件本身就是按B+Tree组织的一个索引结构，这棵树的叶节点data域保存了完整的数据记录。这个索引的key是数据表的主键，因此InnoDB表数据文件本身就是主索引。</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image111.png" width="543" height="241" /></p>
<p><strong>图10</strong></p>
<p>图10是InnoDB主索引（同时也是数据文件）的示意图，可以看到叶节点包含了完整的数据记录。这种索引叫做聚集索引。因为InnoDB的数据文件本身要按主键聚集，所以InnoDB要求表必须有主键（MyISAM可以没有），如果没有显式指定，则MySQL系统会自动选择一个可以唯一标识数据记录的列作为主键，如果不存在这种列，则MySQL自动为InnoDB表生成一个隐含字段作为主键，这个字段长度为6个字节，类型为长整形。</p>
<p><strong>第二个与MyISAM索引的不同是InnoDB的辅助索引data域存储相应记录主键的值而不是地址。</strong>换句话说，InnoDB的所有辅助索引都引用主键作为data域。例如，图11为定义在Col3上的一个辅助索引：</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/11/image5.png" width="543" height="222" /></p>
<p><strong>图11</strong></p>
<p>这里以英文字符的ASCII码作为比较准则。<strong>聚集索引这种实现方式使得按主键的搜索十分高效，但是辅助索引搜索需要检索两遍索引：首先检索辅助索引获得主键，然后用主键到主索引中检索获得记录。</strong></p>
<p>了解不同存储引擎的索引实现方式对于正确使用和优化索引都非常有帮助，例如知道了InnoDB的索引实现后，就很容易明白为什么不建议使用过长的字段作为主键，因为所有辅助索引都引用主索引，过长的主索引会令辅助索引变得过大。再例如，用非单调的字段作为主键在InnoDB中不是个好主意，因为InnoDB数据文件本身是一颗B+Tree，非单调的主键会造成在插入新记录时数据文件为了维持B+Tree的特性而频繁的分裂调整，十分低效，而使用自增字段作为主键则是一个很好的选择。</p>
<p>下一章将具体讨论这些与索引有关的优化策略。</p>
<h3><a name="nav-4"></a>索引使用策略及优化</h3>
<p>MySQL的优化主要分为结构优化（Scheme optimization）和查询优化（Query optimization）。本章讨论的高性能索引策略主要属于结构优化范畴。本章的内容完全基于上文的理论基础，实际上一旦理解了索引背后的机制，那么选择高性能的策略就变成了纯粹的推理，并且可以理解这些策略背后的逻辑。</p>
<h4><a name="nav-4-1"></a>示例数据库</h4>
<p>为了讨论索引策略，需要一个数据量不算小的数据库作为示例。本文选用MySQL官方文档中提供的示例数据库之一：employees。这个数据库关系复杂度适中，且数据量较大。下图是这个数据库的E-R关系图（引用自MySQL官方手册）：</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image11.png" width="666" height="524" /></p>
<p><strong>图12</strong></p>
<p>MySQL官方文档中关于此数据库的页面为<a href="http://dev.mysql.com/doc/employee/en/employee.html">http://dev.mysql.com/doc/employee/en/employee.html</a>。里面详细介绍了此数据库，并提供了下载地址和导入方法，如果有兴趣导入此数据库到自己的MySQL可以参考文中内容。</p>
<h4><a name="nav-4-2"></a>最左前缀原理与相关优化</h4>
<p>高效使用索引的首要条件是知道什么样的查询会使用到索引，这个问题和B+Tree中的“最左前缀原理”有关，下面通过例子说明最左前缀原理。</p>
<p>这里先说一下联合索引的概念。在上文中，我们都是假设索引只引用了单个的列，实际上，MySQL中的索引可以以一定顺序引用多个列，这种索引叫做联合索引，一般的，一个联合索引是一个有序元组&lt;a1, a2, …, an&gt;，其中各个元素均为数据表的一列，实际上要严格定义索引需要用到关系代数，但是这里我不想讨论太多关系代数的话题，因为那样会显得很枯燥，所以这里就不再做严格定义。另外，单列索引可以看成联合索引元素数为1的特例。</p>
<p>以employees.titles表为例，下面先查看其上都有哪些索引：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p><code>SHOW </code><code>INDEX</code> <code>FROM</code> <code>employees.titles;</code></p>
<p><code>+</code><code>--------+------------+----------+--------------+-------------+-----------+-------------+------+------------+</code></p>
<p><code>| </code><code>Table</code> <code>| Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | </code><code>Null</code> <code>| Index_type |</code></p>
<p><code>+</code><code>--------+------------+----------+--------------+-------------+-----------+-------------+------+------------+</code></p>
<p><code>| titles | 0 | </code><code>PRIMARY</code> <code>| 1 | emp_no | A | </code><code>NULL</code> <code>| | BTREE |</code></p>
<p><code>| titles | 0 | </code><code>PRIMARY</code> <code>| 2 | title | A | </code><code>NULL</code> <code>| | BTREE |</code></p>
<p><code>| titles | 0 | </code><code>PRIMARY</code> <code>| 3 | from_date | A | 443308 | | BTREE |</code></p>
<p><code>| titles | 1 | emp_no | 1 | emp_no | A | 443308 | | BTREE |</code></p>
<p><code>+</code><code>--------+------------+----------+--------------+-------------+-----------+-------------+------+------------+</code></p>
<p>从结果中可以到titles表的主索引为&lt;emp_no, title, from_date&gt;，还有一个辅助索引&lt;emp_no&gt;。为了避免多个索引使事情变复杂（MySQL的SQL优化器在多索引时行为比较复杂），这里我们将辅助索引drop掉：</p>
<p>1</p>
<p><code>ALTER</code> <code>TABLE</code> <code>employees.titles </code><code>DROP</code> <code>INDEX</code> <code>emp_no;</code></p>
<p>这样就可以专心分析索引PRIMARY的行为了。</p>
<h5>情况一：全列匹配。</h5>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>emp_no=</code><code>'10001'</code> <code>AND</code> <code>title=</code><code>'Senior Engineer'</code> <code>AND</code> <code>from_date=</code><code>'1986-06-26'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+</code></p>
<p><code>| 1 | SIMPLE | titles | const | </code><code>PRIMARY</code> <code>| </code><code>PRIMARY</code> <code>| 59 | const,const,const | 1 | |</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+</code></p>
<p>很明显，当按照索引中所有列进行精确匹配（这里精确匹配指“=”或“IN”匹配）时，索引可以被用到。这里有一点需要注意，理论上索引对顺序是敏感的，但是由于MySQL的查询优化器会自动调整where子句的条件顺序以使用适合的索引，例如我们将where中的条件顺序颠倒：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>from_date=</code><code>'1986-06-26'</code> <code>AND</code> <code>emp_no=</code><code>'10001'</code> <code>AND</code> <code>title=</code><code>'Senior Engineer'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+</code></p>
<p><code>| 1 | SIMPLE | titles | const | </code><code>PRIMARY</code> <code>| </code><code>PRIMARY</code> <code>| 59 | const,const,const | 1 | |</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+</code></p>
<p>效果是一样的。</p>
<h5>情况二：最左前缀匹配。</h5>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>emp_no=</code><code>'10001'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+---------+---------+-------+------+-------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+---------+---------+-------+------+-------+</code></p>
<p><code>| 1 | SIMPLE | titles | ref | </code><code>PRIMARY</code> <code>| </code><code>PRIMARY</code> <code>| 4 | const | 1 | |</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+---------+---------+-------+------+-------+</code></p>
<p>当查询条件精确匹配索引的左边连续一个或几个列时，如&lt;emp_no&gt;或&lt;emp_no, title&gt;，所以可以被用到，但是只能用到一部分，即条件所组成的最左前缀。上面的查询从分析结果看用到了PRIMARY索引，但是key_len为4，说明只用到了索引的第一列前缀。</p>
<h5>情况三：查询条件用到了索引中列的精确匹配，但是中间某个条件未提供。</h5>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>emp_no=</code><code>'10001'</code> <code>AND</code> <code>from_date=</code><code>'1986-06-26'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+</code></p>
<p><code>| 1 | SIMPLE | titles | ref | </code><code>PRIMARY</code> <code>| </code><code>PRIMARY</code> <code>| 4 | const | 1 | Using </code><code>where</code> <code>|</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+</code></p>
<p>此时索引使用情况和情况二相同，因为title未提供，所以查询只用到了索引的第一列，而后面的from_date虽然也在索引中，但是由于title不存在而无法和左前缀连接，因此需要对结果进行扫描过滤from_date（这里由于emp_no唯一，所以不存在扫描）。如果想让from_date也使用索引而不是where过滤，可以增加一个辅助索引&lt;emp_no, from_date&gt;，此时上面的查询会使用这个索引。除此之外，还可以使用一种称之为“隔离列”的优化方法，将emp_no与from_date之间的“坑”填上。</p>
<p>首先我们看下title一共有几种不同的值：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>
<p>11</p>
<p>12</p>
<p><code>SELECT</code> <code>DISTINCT</code><code>(title) </code><code>FROM</code> <code>employees.titles;</code></p>
<p><code>+</code><code>--------------------+</code></p>
<p><code>| title |</code></p>
<p><code>+</code><code>--------------------+</code></p>
<p><code>| Senior Engineer |</code></p>
<p><code>| Staff |</code></p>
<p><code>| Engineer |</code></p>
<p><code>| Senior Staff |</code></p>
<p><code>| Assistant Engineer |</code></p>
<p><code>| Technique Leader |</code></p>
<p><code>| Manager |</code></p>
<p><code>+</code><code>--------------------+</code></p>
<p>只有7种。在这种成为“坑”的列值比较少的情况下，可以考虑用“IN”来填补这个“坑”从而形成最左前缀：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles</code></p>
<p><code>WHERE</code> <code>emp_no=</code><code>'10001'</code></p>
<p><code>AND</code> <code>title </code><code>IN</code> <code>(</code><code>'Senior Engineer'</code><code>, </code><code>'Staff'</code><code>, </code><code>'Engineer'</code><code>, </code><code>'Senior Staff'</code><code>, </code><code>'Assistant Engineer'</code><code>, </code><code>'Technique Leader'</code><code>, </code><code>'Manager'</code><code>)</code></p>
<p><code>AND</code> <code>from_date=</code><code>'1986-06-26'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p><code>| 1 | SIMPLE | titles | range | </code><code>PRIMARY</code> <code>| </code><code>PRIMARY</code> <code>| 59 | </code><code>NULL</code> <code>| 7 | Using </code><code>where</code> <code>|</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p>这次key_len为59，说明索引被用全了，但是从type和rows看出IN实际上执行了一个range查询，这里检查了7个key。看下两种查询的性能比较：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p><code>SHOW PROFILES;</code></p>
<p><code>+</code><code>----------+------------+-------------------------------------------------------------------------------+</code></p>
<p><code>| Query_ID | Duration | Query |</code></p>
<p><code>+</code><code>----------+------------+-------------------------------------------------------------------------------+</code></p>
<p><code>| 10 | 0.00058000 | </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>emp_no=</code><code>'10001'</code> <code>AND</code> <code>from_date=</code><code>'1986-06-26'</code><code>|</code></p>
<p><code>| 11 | 0.00052500 | </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>emp_no=</code><code>'10001'</code> <code>AND</code> <code>title </code><code>IN</code> <code>... |</code></p>
<p><code>+</code><code>----------+------------+-------------------------------------------------------------------------------+</code></p>
<p>“填坑”后性能提升了一点。如果经过emp_no筛选后余下很多数据，则后者性能优势会更加明显。当然，如果title的值很多，用填坑就不合适了，必须建立辅助索引。</p>
<h5>情况四：查询条件没有指定索引第一列。</h5>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>from_date=</code><code>'1986-06-26'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+------+---------+------+--------+-------------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+------+---------+------+--------+-------------+</code></p>
<p><code>| 1 | SIMPLE | titles | </code><code>ALL</code> <code>| </code><code>NULL</code> <code>| </code><code>NULL</code> <code>| </code><code>NULL</code> <code>| </code><code>NULL</code> <code>| 443308 | Using </code><code>where</code> <code>|</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+------+---------+------+--------+-------------+</code></p>
<p>由于不是最左前缀，索引这样的查询显然用不到索引。</p>
<h5>情况五：匹配某列的前缀字符串。</h5>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>emp_no=</code><code>'10001'</code> <code>AND</code> <code>title </code><code>LIKE</code> <code>'Senior%'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p><code>| 1 | SIMPLE | titles | range | </code><code>PRIMARY</code> <code>| </code><code>PRIMARY</code> <code>| 56 | </code><code>NULL</code> <code>| 1 | Using </code><code>where</code> <code>|</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p>此时可以用到索引，但是如果通配符不是只出现在末尾，则无法使用索引。（原文表述有误，如果通配符%不出现在开头，则可以用到索引，但根据具体情况不同可能只会用其中一个前缀）</p>
<h5>情况六：范围查询。</h5>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>emp_no &lt; </code><code>'10010'</code> <code>and</code> <code>title=</code><code>'Senior Engineer'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p><code>| 1 | SIMPLE | titles | range | </code><code>PRIMARY</code> <code>| </code><code>PRIMARY</code> <code>| 4 | </code><code>NULL</code> <code>| 16 | Using </code><code>where</code> <code>|</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p>范围列可以用到索引（必须是最左前缀），但是范围列后面的列无法用到索引。同时，索引最多用于一个范围列，因此如果查询条件中有两个范围列则无法全用到索引。</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles</code></p>
<p><code>WHERE</code> <code>emp_no &lt; 10010</code><code>'</code></p>
<p><code>AND title='</code><code>Senior Engineer</code><code>'</code></p>
<p><code>AND from_date BETWEEN '</code><code>1986-01-01</code><code>' AND '</code><code>1986-12-31';</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p><code>| 1 | SIMPLE | titles | range | </code><code>PRIMARY</code> <code>| </code><code>PRIMARY</code> <code>| 4 | </code><code>NULL</code> <code>| 16 | Using </code><code>where</code> <code>|</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p>可以看到索引对第二个范围索引无能为力。这里特别要说明MySQL一个有意思的地方，那就是仅用explain可能无法区分范围索引和多值匹配，因为在type中这两者都显示为range。同时，用了“between”并不意味着就是范围查询，例如下面的查询：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles</code></p>
<p><code>WHERE</code> <code>emp_no </code><code>BETWEEN</code> <code>'10001'</code> <code>AND</code> <code>'10010'</code></p>
<p><code>AND</code> <code>title=</code><code>'Senior Engineer'</code></p>
<p><code>AND</code> <code>from_date </code><code>BETWEEN</code> <code>'1986-01-01'</code> <code>AND</code> <code>'1986-12-31'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p><code>| 1 | SIMPLE | titles | range | </code><code>PRIMARY</code> <code>| </code><code>PRIMARY</code> <code>| 59 | </code><code>NULL</code> <code>| 16 | Using </code><code>where</code> <code>|</code></p>
<p><code>+</code><code>----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+</code></p>
<p>看起来是用了两个范围查询，但作用于emp_no上的“BETWEEN”实际上相当于“IN”，也就是说emp_no实际是多值精确匹配。可以看到这个查询用到了索引全部三个列。因此在MySQL中要谨慎地区分多值匹配和范围匹配，否则会对MySQL的行为产生困惑。</p>
<h5>情况七：查询条件中含有函数或表达式。</h5>
<p>很不幸，如果查询条件中含有函数或表达式，则MySQL不会为这列使用索引（虽然某些在数学意义上可以使用）。例如：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>emp_no=</code><code>'10001'</code> <code>AND</code> <code>left</code><code>(title, 6)=</code><code>'Senior'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+</code></p>
<p><code>| 1 | SIMPLE | titles | ref | </code><code>PRIMARY</code> <code>| </code><code>PRIMARY</code> <code>| 4 | const | 1 | Using </code><code>where</code> <code>|</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+</code></p>
<p>虽然这个查询和情况五中功能相同，但是由于使用了函数left，则无法为title列应用索引，而情况五中用LIKE则可以。再如：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.titles </code><code>WHERE</code> <code>emp_no - 1=</code><code>'10000'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+------+---------+------+--------+-------------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+------+---------+------+--------+-------------+</code></p>
<p><code>| 1 | SIMPLE | titles | </code><code>ALL</code> <code>| </code><code>NULL</code> <code>| </code><code>NULL</code> <code>| </code><code>NULL</code> <code>| </code><code>NULL</code> <code>| 443308 | Using </code><code>where</code> <code>|</code></p>
<p><code>+</code><code>----+-------------+--------+------+---------------+------+---------+------+--------+-------------+</code></p>
<p>显然这个查询等价于查询emp_no为10001的函数，但是由于查询条件是一个表达式，MySQL无法为其使用索引。看来MySQL还没有智能到自动优化常量表达式的程度，因此在写查询语句时尽量避免表达式出现在查询中，而是先手工私下代数运算，转换为无表达式的查询语句。</p>
<h4><a name="nav-4-3"></a>索引选择性与前缀索引</h4>
<p>既然索引可以加快查询速度，那么是不是只要是查询语句需要，就建上索引？答案是否定的。因为索引虽然加快了查询速度，但索引也是有代价的：索引文件本身要消耗存储空间，同时索引会加重插入、删除和修改记录时的负担，另外，MySQL在运行时也要消耗资源维护索引，因此索引并不是越多越好。一般两种情况下不建议建索引。</p>
<p>第一种情况是表记录比较少，例如一两千条甚至只有几百条记录的表，没必要建索引，让查询做全表扫描就好了。至于多少条记录才算多，这个个人有个人的看法，我个人的经验是以2000作为分界线，记录数不超过 2000可以考虑不建索引，超过2000条可以酌情考虑索引。</p>
<p>另一种不建议建索引的情况是索引的选择性较低。所谓索引的选择性（Selectivity），是指不重复的索引值（也叫基数，Cardinality）与表记录数（#T）的比值：</p>
<p>Index Selectivity = Cardinality / #T</p>
<p>显然选择性的取值范围为(0, 1]，选择性越高的索引价值越大，这是由B+Tree的性质决定的。例如，上文用到的employees.titles表，如果title字段经常被单独查询，是否需要建索引，我们看一下它的选择性：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>SELECT</code> <code>count</code><code>(</code><code>DISTINCT</code><code>(title))/</code><code>count</code><code>(*) </code><code>AS</code> <code>Selectivity </code><code>FROM</code> <code>employees.titles;</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>| Selectivity |</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>| 0.0000 |</code></p>
<p><code>+</code><code>-------------+</code></p>
<p>title的选择性不足0.0001（精确值为0.00001579），所以实在没有什么必要为其单独建索引。</p>
<p>有一种与索引选择性有关的索引优化策略叫做前缀索引，就是用列的前缀代替整个列作为索引key，当前缀长度合适时，可以做到既使得前缀索引的选择性接近全列索引，同时因为索引key变短而减少了索引文件的大小和维护开销。下面以employees.employees表为例介绍前缀索引的选择和使用。</p>
<p>从图12可以看到employees表只有一个索引&lt;emp_no&gt;，那么如果我们想按名字搜索一个人，就只能全表扫描了：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>EXPLAIN </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.employees </code><code>WHERE</code> <code>first_name=</code><code>'Eric'</code> <code>AND</code> <code>last_name=</code><code>'Anido'</code><code>;</code></p>
<p><code>+</code><code>----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+</code></p>
<p><code>| id | select_type | </code><code>table</code> <code>| type | possible_keys | </code><code>key</code> <code>| key_len | ref | </code><code>rows</code> <code>| Extra |</code></p>
<p><code>+</code><code>----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+</code></p>
<p><code>| 1 | SIMPLE | employees | </code><code>ALL</code> <code>| </code><code>NULL</code> <code>| </code><code>NULL</code> <code>| </code><code>NULL</code> <code>| </code><code>NULL</code> <code>| 300024 | Using </code><code>where</code> <code>|</code></p>
<p><code>+</code><code>----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+</code></p>
<p>如果频繁按名字搜索员工，这样显然效率很低，因此我们可以考虑建索引。有两种选择，建&lt;first_name&gt;或&lt;first_name, last_name&gt;，看下两个索引的选择性：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>10</p>
<p>11</p>
<p>12</p>
<p>13</p>
<p><code>SELECT</code> <code>count</code><code>(</code><code>DISTINCT</code><code>(first_name))/</code><code>count</code><code>(*) </code><code>AS</code> <code>Selectivity </code><code>FROM</code> <code>employees.employees;</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>| Selectivity |</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>| 0.0042 |</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>SELECT</code> <code>count</code><code>(</code><code>DISTINCT</code><code>(concat(first_name, last_name)))/</code><code>count</code><code>(*) </code><code>AS</code> <code>Selectivity </code><code>FROM</code> <code>employees.employees;</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>| Selectivity |</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>| 0.9313 |</code></p>
<p><code>+</code><code>-------------+</code></p>
<p>&lt;first_name&gt;显然选择性太低，&lt;first_name, last_name&gt;选择性很好，但是first_name和last_name加起来长度为30，有没有兼顾长度和选择性的办法？可以考虑用first_name和last_name的前几个字符建立索引，例如&lt;first_name, left(last_name, 3)&gt;，看看其选择性：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>SELECT</code> <code>count</code><code>(</code><code>DISTINCT</code><code>(concat(first_name, </code><code>left</code><code>(last_name, 3))))/</code><code>count</code><code>(*) </code><code>AS</code> <code>Selectivity </code><code>FROM</code> <code>employees.employees;</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>| Selectivity |</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>| 0.7879 |</code></p>
<p><code>+</code><code>-------------+</code></p>
<p>选择性还不错，但离0.9313还是有点距离，那么把last_name前缀加到4：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p><code>SELECT</code> <code>count</code><code>(</code><code>DISTINCT</code><code>(concat(first_name, </code><code>left</code><code>(last_name, 4))))/</code><code>count</code><code>(*) </code><code>AS</code> <code>Selectivity </code><code>FROM</code> <code>employees.employees;</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>| Selectivity |</code></p>
<p><code>+</code><code>-------------+</code></p>
<p><code>| 0.9007 |</code></p>
<p><code>+</code><code>-------------+</code></p>
<p>这时选择性已经很理想了，而这个索引的长度只有18，比&lt;first_name, last_name&gt;短了接近一半，我们把这个前缀索引 建上：</p>
<p>1</p>
<p>2</p>
<p><code>ALTER</code> <code>TABLE</code> <code>employees.employees</code></p>
<p><code>ADD</code> <code>INDEX</code> <code>`first_name_last_name4` (first_name, last_name(4));</code></p>
<p>此时再执行一遍按名字查询，比较分析一下与建索引前的结果：</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p><code>SHOW PROFILES;</code></p>
<p><code>+</code><code>----------+------------+---------------------------------------------------------------------------------+</code></p>
<p><code>| Query_ID | Duration | Query |</code></p>
<p><code>+</code><code>----------+------------+---------------------------------------------------------------------------------+</code></p>
<p><code>| 87 | 0.11941700 | </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.employees </code><code>WHERE</code> <code>first_name=</code><code>'Eric'</code> <code>AND</code> <code>last_name=</code><code>'Anido'</code> <code>|</code></p>
<p><code>| 90 | 0.00092400 | </code><code>SELECT</code> <code>* </code><code>FROM</code> <code>employees.employees </code><code>WHERE</code> <code>first_name=</code><code>'Eric'</code> <code>AND</code> <code>last_name=</code><code>'Anido'</code> <code>|</code></p>
<p><code>+</code><code>----------+------------+---------------------------------------------------------------------------------+</code></p>
<p>性能的提升是显著的，查询速度提高了120多倍。</p>
<p>前缀索引兼顾索引大小和查询速度，但是其缺点是不能用于ORDER BY和GROUP BY操作，也不能用于Covering index（即当索引本身包含查询所需全部数据时，不再访问数据文件本身）。</p>
<h4><a name="nav-4-4"></a>InnoDB的主键选择与插入优化</h4>
<p>在使用InnoDB存储引擎时，如果没有特别的需要，请永远使用一个与业务无关的自增字段作为主键。</p>
<p>经常看到有帖子或博客讨论主键选择问题，有人建议使用业务无关的自增主键，有人觉得没有必要，完全可以使用如学号或身份证号这种唯一字段作为主键。不论支持哪种论点，大多数论据都是业务层面的。如果从数据库索引优化角度看，使用InnoDB引擎而不使用自增主键绝对是一个糟糕的主意。</p>
<p>上文讨论过InnoDB的索引实现，InnoDB使用聚集索引，数据记录本身被存于主索引（一颗B+Tree）的叶子节点上。这就要求同一个叶子节点内（大小为一个内存页或磁盘页）的各条数据记录按主键顺序存放，因此每当有一条新的记录插入时，MySQL会根据其主键将其插入适当的节点和位置，如果页面达到装载因子（InnoDB默认为15/16），则开辟一个新的页（节点）。</p>
<p>如果表使用自增主键，那么每次插入新的记录，记录就会顺序添加到当前索引节点的后续位置，当一页写满，就会自动开辟一个新的页。如下图所示：</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image12.png" width="437" height="175" /></p>
<p><strong>图13</strong></p>
<p>这样就会形成一个紧凑的索引结构，近似顺序填满。由于每次插入时也不需要移动已有数据，因此效率很高，也不会增加很多开销在维护索引上。</p>
<p>如果使用非自增主键（如果身份证号或学号等），由于每次插入主键的值近似于随机，因此每次新纪录都要被插到现有索引页得中间某个位置：</p>
<p><img title="image" border="0" alt="image" src="http://www.codinglabs.org/wp-content/uploads/2011/10/image6.png" width="229" height="175" /></p>
<p><strong>图14</strong></p>
<p>此时MySQL不得不为了将新记录插到合适位置而移动数据，甚至目标页面可能已经被回写到磁盘上而从缓存中清掉，此时又要从磁盘上读回来，这增加了很多开销，同时频繁的移动、分页操作造成了大量的碎片，得到了不够紧凑的索引结构，后续不得不通过OPTIMIZE TABLE来重建表并优化填充页面。</p>
<p>因此，只要可以，请尽量在InnoDB上采用自增字段做主键。</p>
<h3><a name="nav-5"></a>后记</h3>
<p>这篇文章断断续续写了半个月，主要内容就是上面这些了。不可否认，这篇文章在一定程度上有纸上谈兵之嫌，因为我本人对MySQL的使用属于菜鸟级别，更没有太多数据库调优的经验，在这里大谈数据库索引调优有点大言不惭。就当是我个人的一篇学习笔记了。</p>
<p>其实数据库索引调优是一项技术活，不能仅仅靠理论，因为实际情况千变万化，而且MySQL本身存在很复杂的机制，如查询优化策略和各种引擎的实现差异等都会使情况变得更加复杂。但同时这些理论是索引调优的基础，只有在明白理论的基础上，才能对调优策略进行合理推断并了解其背后的机制，然后结合实践中不断的实验和摸索，从而真正达到高效使用MySQL索引的目的。</p>
<p>另外，MySQL索引及其优化涵盖范围非常广，本文只是涉及到其中一部分。如与排序（ORDER BY）相关的索引优化及覆盖索引（Covering index）的话题本文并未涉及，同时除B-Tree索引外MySQL还根据不同引擎支持的哈希索引、全文索引等等本文也并未涉及。如果有机会，希望再对本文未涉及的部分进行补充吧。</p>
<h3><a name="nav-6"></a>参考文献</h3>
<p>[1] Baron Scbwartz等 著，王小东等 译；高性能MySQL（High Performance MySQL）；电子工业出版社，2010</p>
<p>[2] Michael Kofler 著，杨晓云等 译；MySQL5权威指南（The Definitive Guide to MySQL5）；人民邮电出版社，2006</p>
<p>[3] 姜承尧 著；MySQL技术内幕-InnoDB存储引擎；机械工业出版社，2011</p>
<p>[4] D Comer, Ubiquitous B-tree; ACM Computing Surveys (CSUR), 1979</p>
<p>[5] Codd, E. F. (1970). “A relational model of data for large shared data banks”. Communications of the ACM, , Vol. 13, No. 6, pp. 377-387</p>
<p>[6] MySQL5.1参考手册 – <a href="http://dev.mysql.com/doc/refman/5.1/zh/index.html">http://dev.mysql.com/doc/refman/5.1/zh/index.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.buguw.com/mysql%e7%b4%a2%e5%bc%95%e8%83%8c%e5%90%8e%e7%9a%84%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e5%8f%8a%e7%ae%97%e6%b3%95%e5%8e%9f%e7%90%86.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

