<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet href='http://feed.feedsky.com/styles/podcast2.xsl' type='text/xsl' ?><!--这是一个由Feedsy提供技术支持的Feed，为了提高读者阅读的体验，以及满足用户美化自己Feed的需要，我们设计了多种精美的Feed模板，提供给大家选择，所有最终呈现出来的样式，皆由用户自愿选择使用，未经许可，任何团体和个人，请不要擅自修改样式或者盗用，这是对于用户选择权的尊重。--><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:fs="http://www.feedsky.com/namespace/feed" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link href="http://feed.feedsky.com/buguw" type="application/rss+xml" rel="self"></atom:link><fs:self_link href="http://feed.feedsky.com/buguw" type="application/rss+xml"></fs:self_link><lastBuildDate>Tue, 17 Apr 2012 03:47:34 GMT</lastBuildDate><title>布谷屋</title><description>电脑技术交流！软件编程！精品素材！影音娱乐！</description><link>http://www.buguw.com</link><sy:updatePeriod>hourly</sy:updatePeriod><sy:updateFrequency>1</sy:updateFrequency><language>en</language><pubDate>Tue, 17 Apr 2012 03:47:34 GMT</pubDate><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><content:encoded>&lt;p&gt;一、服务提供者告诉你interface，你可以使用如下三种方式来开发：    &lt;br /&gt;YourService即是服务提供者告诉给你的一个interface(当然，也可以根据WSDL的定义，自己定义一个同样的interface)。 &lt;/p&gt;
&lt;p&gt;1、简单的方式&lt;/p&gt;
&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26116/&quot;&gt;Codee#26116&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Service&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;serviceModel&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ObjectServiceFactory&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;create&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;YourService&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;class&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;YourService&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;service&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;YourService&lt;/span&gt;)&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;XFireProxyFactory&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;create&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;serviceModel&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&lt;a href=&quot;http://your/remote/url&quot;&gt;http://your/remote/url&lt;/a&gt;&lt;/span&gt;);     &lt;/div&gt;
&lt;p&gt;2、JSR 181注释的方式&lt;/p&gt;
&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26117/&quot;&gt;Codee#26117&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Service&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;serviceModel&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;AnnotationServiceFactory&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;create&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;YourService&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;class&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;YourService&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;YourService&lt;/span&gt;)&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;XFireProxyFactory&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;create&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;serviceModel&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;http://your/remote/url&amp;quot;&lt;/span&gt;);     &lt;/div&gt;
&lt;p&gt;3、混合方式&lt;/p&gt;
&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26118/&quot;&gt;Codee#26118&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Service&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;serviceModel&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;AnnotationServiceFactory&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Jsr181WebAnnotations&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;(),&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;XFireFactory&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newInstance&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getXFire&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getTransportManager&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;(),&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;AegisBindingProvider&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;JaxbTypeRegistry&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;())).&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;create&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;YourService&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;class&lt;/span&gt;);     &lt;/div&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;二、通过WSDL创建一个动态的客户端，如下：&lt;/p&gt;
&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26119/&quot;&gt;Codee#26119&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;01&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.net.MalformedURLException&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;02&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.net.URL&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;03&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;04&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;org.codehaus.xfire.client.Client&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;05&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;06&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;DynamicClientTest&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;07&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;MalformedURLException&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;08&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Client&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Client&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;URL&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;http://localhost:8080/xfiretest/services/TestService?wsdl&amp;quot;&lt;/span&gt;));     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;09&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;results&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;client&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;invoke&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;sayHello&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Firends&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;});&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;results&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;]);&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;12&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;/div&gt;
&lt;p&gt;三，使用ANT工具或命令行通过WSDL生成一个客户端：    &lt;br /&gt;1、使用ANT生成客户端，ANT脚本如下：&lt;/p&gt;
&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26120/&quot;&gt;Codee#26120&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;01&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;xml&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;?&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;02&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;project&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;wsgen&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;wsgen&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;basedir&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;03&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;path&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;classpathId&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;04&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fileset&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;dir&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;./WebRoot/WEB-INF/lib&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;05&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;include&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;*.jar&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;/&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;06&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fileset&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;07&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;path&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;08&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;taskdef&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;classpathref&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;classpathId&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;wsgen&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;classname&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;org.codehaus.xfire.gen.WsGenTask&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;09&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;taskdef&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;target&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;wsgen&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;description&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;generate client&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;wsgen&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;outputDirectory&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;./src/&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;wsdl&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;abc.wsdl&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;binding&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;xmlbeans&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;com.abc.p&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;overwrite&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;/&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;12&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;13&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;project&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;/div&gt;
&lt;p&gt;请注意，脚本中有一个参数binding，可以指定如下两种不同的方式：    &lt;br /&gt;(1)jaxb(Java Architecture for XML Binding，&lt;a href=&quot;https://jaxb.dev.java.net/&quot;&gt;https://jaxb.dev.java.net/&lt;/a&gt;)：使用此种方式时，会自动生成更多的Request和Resopnse类。     &lt;br /&gt;(2)xmlbeans&lt;/p&gt;
&lt;p&gt;调用方式如下：&lt;/p&gt;
&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26121/&quot;&gt;Codee#26121&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;AbcServiceClient&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;AbcServiceClient&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;url&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;http://localhost:8080/xfireTest/services/TestService&amp;quot;&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;result&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;client&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getAbcPort&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;sayHello&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Robin&amp;quot;&lt;/span&gt;);     &lt;/div&gt;
&lt;p&gt;2、使用命令生成客户端的命令如下：&lt;/p&gt;
&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26122/&quot;&gt;Codee#26122&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;gpath&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;xfire&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;all&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1.2&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SNAPSHOT&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ant&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1.6&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jaxb&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;api&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;2.0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;EA3&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;stax&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;api&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jdom&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jaxb&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;impl&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;2.0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;EA3&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;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&quot;&gt;\&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jaxb&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;xjc&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;2.0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ea3&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;wstx&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;asl&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;2.9&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;commons&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;logging&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;activation&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1.1&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;wsdl4j&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1.5&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;XmlSchema&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;xfire&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jsr181&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;api&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;M1&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;jar&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;3&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;cp&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;$gpath&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;org&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;codehaus&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;xfire&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;gen&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;WsGen&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;wsdl&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;http:&lt;/span&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//localhost:8080/xfire/services/Bookservice?wsdl -o . -p pl.tomeks.client -overwrite true&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;其结果与ANT生成的一样。   &lt;br /&gt;四、参考资源：    &lt;br /&gt;1、XFire 1.2.6手册(&lt;a href=&quot;http://xfire.codehaus.org/User%27s+Guide&quot;&gt;http://xfire.codehaus.org/User%27s+Guide&lt;/a&gt;)    &lt;br /&gt;2、&lt;a href=&quot;http://xfire.codehaus.org/Client+API&quot;&gt;http://xfire.codehaus.org/Client+API&lt;/a&gt;    &lt;br /&gt;3、&lt;a href=&quot;http://xfire.codehaus.org/Dynamic+Client&quot;&gt;http://xfire.codehaus.org/Dynamic+Client&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;转至：&lt;a title=&quot;http://my.oschina.net/lovedreamland/blog/52977&quot; href=&quot;http://my.oschina.net/lovedreamland/blog/52977&quot;&gt;http://my.oschina.net/lovedreamland/blog/52977&lt;/a&gt;&lt;/p&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;/div&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/628643005/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</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><description>一、服务提供者告诉你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, &amp;#34;http://your/remote/url&amp;#34;); 3、混合方式 Java语言: Codee#26118 1 Service serviceModel = new AnnotationServiceFactory(new Jsr181WebAnnotations(), XFireFactory.newInstance().getXFire().getTransportManager(), new AegisBindingProvider(new JaxbTypeRegistry())).create(YourService.class); &amp;#160; 二、通过WSDL创建一个动态的客户端，如下： Java语言: Codee#26119 01 import [...]&lt;img src=&quot;http://www1.feedsky.com/t1/628643005/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</description><category>XFire</category><category>Java天地</category><pubDate>Tue, 17 Apr 2012 11:47:34 +0800</pubDate><author>tiger7456</author><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><guid isPermaLink="false">http://www.buguw.com/?p=314</guid><dc:creator>tiger7456</dc:creator><fs:srclink>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</fs:srclink><fs:srcfeed>http://www.buguw.com/?feed=rss2</fs:srcfeed><fs:itemid>feedsky/buguw/~8163640/628643005/6267358</fs:itemid></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><content:encoded>&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26115/&quot;&gt;Codee#26115&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;001&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//1. 字符串有整型的相互转换 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;002&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;a&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;valueOf&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;2&lt;/span&gt;); &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//integer to numeric string &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;003&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Integer&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;parseInt&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;a&lt;/span&gt;); &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//numeric string to an int&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;004&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;005&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//2. 向文件末尾添加内容 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;006&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedWriter&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;007&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;008&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedWriter&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileWriter&lt;/span&gt;(&lt;span style=&quot;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&quot;&gt;”&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;filename&lt;/span&gt;&lt;span style=&quot;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&quot;&gt;”&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;true&lt;/span&gt;));     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;009&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;write&lt;/span&gt;(&lt;span style=&quot;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&quot;&gt;”&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;aString&lt;/span&gt;&lt;span style=&quot;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&quot;&gt;”&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;010&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;IOException&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;e&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;011&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// error processing code&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;012&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;013&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;finally&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;014&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;015&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;016&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;017&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;018&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;019&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;020&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//3. 得到当前方法的名字 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;021&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;methodName&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Thread&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;currentThread&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getStackTrace&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;()[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getMethodName&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;022&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;023&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//4. 转字符串到日期 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;024&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;util&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;DateFormat&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getDateInstance&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;parse&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;date&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;025&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//或者是： &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;026&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SimpleDateFormat&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;format&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SimpleDateFormat&lt;/span&gt;( &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;dd.MM.yyyy&amp;quot;&lt;/span&gt; );     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;027&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;date&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;parse&lt;/span&gt;( &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;myString&lt;/span&gt; );    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;028&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;029&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//5. 使用JDBC链接Oracle &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;030&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;OracleJdbcTest&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;031&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;032&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;driverClass&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;oracle.jdbc.driver.OracleDriver&amp;quot;&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;033&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;034&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Connection&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;con&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;035&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;036&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;init&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileInputStream&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fs&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ClassNotFoundException&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SQLException&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileNotFoundException&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;IOException&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;037&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;038&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Properties&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;props&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Properties&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;039&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;props&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;load&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fs&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;040&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;url&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;props&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getProperty&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;db.url&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;041&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;userName&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;props&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getProperty&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;db.user&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;042&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;password&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;props&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getProperty&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;db.password&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;043&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Class&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;forName&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;driverClass&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;044&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;045&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;con&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;DriverManager&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getConnection&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;userName&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;password&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;046&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;047&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;048&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fetch&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SQLException&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;IOException&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;049&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;050&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;PreparedStatement&lt;/span&gt; ps &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;con&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;prepareStatement&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;select SYSDATE from dual&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;051&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ResultSet&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;rs&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; ps&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;executeQuery&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;052&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;053&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;rs&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;next&lt;/span&gt;())     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;054&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;055&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// do the thing you do &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;056&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;057&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;rs&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;058&lt;/span&gt; ps&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;059&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;060&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;061&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;)     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;062&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;063&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;OracleJdbcTest&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;test&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;OracleJdbcTest&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;064&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;init&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;065&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fetch&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;066&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;067&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;068&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;069&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt; &lt;span style=&quot;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&quot;&gt;把&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Java&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;util&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt; &lt;span style=&quot;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&quot;&gt;转成&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;070&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;util&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;utilDate&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;util&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;071&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;sqlDate&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;utilDate&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getTime&lt;/span&gt;());    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;072&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;073&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//7. 使用NIO进行快速的文件拷贝 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;074&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fileCopy&lt;/span&gt;( &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt; )     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;075&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;IOException&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;076&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;077&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileChannel&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;inChannel&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileInputStream&lt;/span&gt;( &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getChannel&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;078&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileChannel&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;outChannel&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileOutputStream&lt;/span&gt;( &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getChannel&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;079&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;try&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;080&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;081&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;082&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;083&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// magic number for Windows, 64Mb - 32Kb) &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;084&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;maxCount&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;64&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1024&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1024&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;32&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1024&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;085&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;size&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;inChannel&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;size&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;086&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;position&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;087&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;while&lt;/span&gt; ( &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;position&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;size&lt;/span&gt; )     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;088&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;089&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;position&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;inChannel&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;transferTo&lt;/span&gt;( &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;position&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;maxCount&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;outChannel&lt;/span&gt; );     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;090&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;091&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;092&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;finally&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;093&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;094&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; ( &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;inChannel&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt; )     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;095&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;096&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;inChannel&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;097&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;098&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; ( &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;outChannel&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt; )     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;099&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;100&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;outChannel&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;101&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;102&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;103&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;104&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;105&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//8. 创建图片的缩略图 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;106&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;createThumbnail&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;filename&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbWidth&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbHeight&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;quality&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;outFilename&lt;/span&gt;)     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;107&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;InterruptedException&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileNotFoundException&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;IOException&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;108&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;109&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// load image from filename &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;110&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Image&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;image&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Toolkit&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getDefaultToolkit&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getImage&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;filename&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;111&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;MediaTracker&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;mediaTracker&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;MediaTracker&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Container&lt;/span&gt;());     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;112&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;mediaTracker&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;addImage&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;image&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;113&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;mediaTracker&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;waitForID&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;114&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// use this to test for errors at this point: System.out.println(mediaTracker.isErrorAny());&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;115&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;116&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// determine thumbnail size from WIDTH and HEIGHT &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;117&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbRatio&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;double&lt;/span&gt;)&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbWidth&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;/&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;double&lt;/span&gt;)&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbHeight&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;118&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;imageWidth&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;image&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getWidth&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;119&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;imageHeight&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;image&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getHeight&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;120&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;imageRatio&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;double&lt;/span&gt;)&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;imageWidth&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;/&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;double&lt;/span&gt;)&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;imageHeight&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;121&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbRatio&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;imageRatio&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;122&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbHeight&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt;)(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbWidth&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;/&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;imageRatio&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;123&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;124&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbWidth&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt;)(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbHeight&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;imageRatio&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;125&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;126&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;127&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// draw original image to thumbnail image object and &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;128&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// scale it to the new size on-the-fly &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;129&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedImage&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbImage&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedImage&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbWidth&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbHeight&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedImage&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;TYPE_INT_RGB&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;130&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Graphics2D&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;graphics2D&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbImage&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;createGraphics&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;131&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;graphics2D&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setRenderingHint&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;RenderingHints&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;KEY_INTERPOLATION&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;RenderingHints&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;VALUE_INTERPOLATION_BILINEAR&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;132&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;graphics2D&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;drawImage&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;image&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbWidth&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbHeight&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;133&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;134&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// save thumbnail image to outFilename &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;135&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedOutputStream&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedOutputStream&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileOutputStream&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;outFilename&lt;/span&gt;));     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;136&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;JPEGImageEncoder&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;encoder&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;JPEGCodec&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;createJPEGEncoder&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;137&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;JPEGEncodeParam&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;param&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;encoder&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getDefaultJPEGEncodeParam&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbImage&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;138&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;quality&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Math&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;max&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Math&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;min&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;quality&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;100&lt;/span&gt;));     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;139&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;param&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setQuality&lt;/span&gt;((&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;float&lt;/span&gt;)&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;quality&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;/&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;100.0f&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;false&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;140&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;encoder&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setJPEGEncodeParam&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;param&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;141&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;encoder&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;encode&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;thumbImage&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;142&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;143&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;144&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;145&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//9. 创建 JSON 格式的数据 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;146&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;org.json.JSONObject&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;147&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;...&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;148&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;...&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;149&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;JSONObject&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;json&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;JSONObject&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;150&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;json&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;put&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;city&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Mumbai&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;151&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;json&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;put&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;country&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;India&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;152&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;...&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;153&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;output&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;json&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;toString&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;154&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;...&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;155&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;156&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//10. 使用iText JAR生成PDF &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;157&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.io.File&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;158&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.io.FileOutputStream&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;159&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.io.OutputStream&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;160&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.util.Date&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;161&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;162&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;com.lowagie.text.Document&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;163&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;com.lowagie.text.Paragraph&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;164&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;com.lowagie.text.pdf.PdfWriter&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;165&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;166&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;GeneratePDF&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;167&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;168&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;169&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;170&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;OutputStream&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileOutputStream&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;C:\\Test.pdf&amp;quot;&lt;/span&gt;));    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;171&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;172&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Document&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;document&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Document&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;173&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;PdfWriter&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getInstance&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;document&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;174&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;document&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;open&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;175&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;document&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;add&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Paragraph&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Hello Kiran&amp;quot;&lt;/span&gt;));     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;176&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;document&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;add&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Paragraph&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;toString&lt;/span&gt;()));    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;177&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;178&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;document&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;179&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;180&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;181&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;e&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;182&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;183&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;printStackTrace&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;184&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;185&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;186&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;187&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;188&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//11. HTTP 代理设置 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;189&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getProperties&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;put&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;http.proxyHost&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;someProxyURL&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;190&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getProperties&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;put&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;http.proxyPort&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;someProxyPort&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;191&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getProperties&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;put&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;http.proxyUser&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;someUserName&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;192&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getProperties&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;put&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;http.proxyPassword&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;somePassword&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;193&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;194&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//12. 单实例Singleton 示例 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;195&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;SimpleSingleton&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;196&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SimpleSingleton&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;singleInstance&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SimpleSingleton&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;197&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;198&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//Marking default constructor private &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;199&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//to avoid direct instantiation. &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;200&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SimpleSingleton&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;201&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;202&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;203&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//Get instance for class SimpleSingleton &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;204&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SimpleSingleton&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getInstance&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;205&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;206&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;singleInstance&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;207&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;208&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;209&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;210&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//另一种实现&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;211&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;212&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;enum&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SimpleSingleton&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;213&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;INSTANCE&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;214&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;doSomething&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;215&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;216&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;217&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;218&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//Call the method from Singleton: &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;219&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SimpleSingleton&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;INSTANCE&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;doSomething&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;220&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;221&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//13. 抓屏程序 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;222&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.awt.Dimension&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;223&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.awt.Rectangle&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;224&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.awt.Robot&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;225&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.awt.Toolkit&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;226&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.awt.image.BufferedImage&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;227&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;javax.imageio.ImageIO&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;228&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.io.File&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;229&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;230&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;...&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;231&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;captureScreen&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fileName&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;232&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;233&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Dimension&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;screenSize&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Toolkit&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getDefaultToolkit&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getScreenSize&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;234&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Rectangle&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;screenRectangle&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Rectangle&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;screenSize&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;235&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Robot&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;robot&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Robot&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;236&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedImage&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;image&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;robot&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;createScreenCapture&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;screenRectangle&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;237&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ImageIO&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;write&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;image&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;png&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fileName&lt;/span&gt;));    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;238&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;239&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;240&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//14. 列出文件和目录 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;241&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;dir&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;directoryName&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;242&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;children&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;dir&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;list&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;243&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;children&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;244&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// Either dir does not exist or is not a directory &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;245&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;246&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;children&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;length&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;++)&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;247&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// Get filename of file or directory &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;248&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;filename&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;children&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;];&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;249&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;250&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;251&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;252&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// It is also possible to filter the list of returned files. &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;253&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// This example does not return any files that start with `.’. &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;254&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FilenameFilter&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;filter&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FilenameFilter&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;255&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;boolean&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;accept&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;dir&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;256&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;startsWith&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;.&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;257&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;258&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;};&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;259&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;children&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;dir&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;list&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;filter&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;260&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;261&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// The list of files can also be retrieved as File objects &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;262&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;files&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;dir&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;listFiles&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;263&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;264&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// This filter only returns directories &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;265&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileFilter&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fileFilter&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileFilter&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;266&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;boolean&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;accept&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;267&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;isDirectory&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;268&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;269&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;};&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;270&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;files&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;dir&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;listFiles&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fileFilter&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;271&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;272&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//15. 创建ZIP和JAR文件&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;273&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;274&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.util.zip.*&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;275&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.io.*&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;276&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;277&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;ZipIt&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;278&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[])&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;IOException&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;279&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;length&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;2&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;280&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;err&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;usage: java ZipIt Zip.zip file1 file2 file3&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;281&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;exit&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;(-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;282&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;283&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;zipFile&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;]);&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;284&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;zipFile&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;exists&lt;/span&gt;()) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;285&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;err&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Zip file already exists, please try another&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;286&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;exit&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;(-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;2&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;287&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;288&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileOutputStream&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fos&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileOutputStream&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;zipFile&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;289&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ZipOutputStream&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;zos&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ZipOutputStream&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fos&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;290&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bytesRead&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;291&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;byte&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;buffer&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;byte&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;];&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;292&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;CRC32&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;crc&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;CRC32&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;293&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; n&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;length&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt; n; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;++)&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;294&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;];&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;295&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;296&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;(!&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;exists&lt;/span&gt;()) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;297&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;err&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Skipping: &amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;298&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;continue&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;299&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;300&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedInputStream&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bis&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedInputStream&lt;/span&gt;(     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;301&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileInputStream&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;));     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;302&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;crc&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;reset&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;303&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;while&lt;/span&gt; ((&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bytesRead&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bis&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;read&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;buffer&lt;/span&gt;)) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;304&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;crc&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;update&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;buffer&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bytesRead&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;305&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;306&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bis&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;307&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// Reset to beginning of input stream &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;308&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bis&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedInputStream&lt;/span&gt;(     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;309&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;FileInputStream&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;));     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;310&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ZipEntry&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;entry&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ZipEntry&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;311&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;entry&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setMethod&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ZipEntry&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;STORED&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;312&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;entry&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setCompressedSize&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;length&lt;/span&gt;());     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;313&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;entry&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setSize&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;length&lt;/span&gt;());     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;314&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;entry&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setCrc&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;crc&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getValue&lt;/span&gt;());     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;315&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;zos&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;putNextEntry&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;entry&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;316&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;while&lt;/span&gt; ((&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bytesRead&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bis&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;read&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;buffer&lt;/span&gt;)) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;317&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;zos&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;write&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;buffer&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bytesRead&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;318&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;319&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;bis&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;320&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;321&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;zos&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;close&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;322&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;323&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;324&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;325&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//16. 解析/读取XML 文件 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;326&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;XML&lt;/span&gt;&lt;span style=&quot;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&quot;&gt;文件&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;327&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;xml&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;?&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;328&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;students&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;329&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;student&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;330&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;John&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;331&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;grade&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;B&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;grade&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;332&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;age&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;age&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;333&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;student&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;334&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;student&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;335&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Mary&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;336&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;grade&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;grade&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;337&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;age&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;age&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;338&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;student&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;339&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;student&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;340&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Simon&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;341&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;grade&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;grade&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;342&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;age&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;age&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;343&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;student&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;344&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;students&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;345&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;346&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//Java代码 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;347&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;package&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;net&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;viralpatel&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;xmlparser&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;348&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;349&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.io.File&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;350&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;javax.xml.parsers.DocumentBuilder&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;351&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;javax.xml.parsers.DocumentBuilderFactory&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;352&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;353&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;org.w3c.dom.Document&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;354&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;org.w3c.dom.Element&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;355&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;org.w3c.dom.Node&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;356&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;org.w3c.dom.NodeList&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;357&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;358&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;XMLParser&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;359&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;360&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getAllUserNames&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fileName&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;361&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;362&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;DocumentBuilderFactory&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;dbf&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;DocumentBuilderFactory&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newInstance&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;363&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;DocumentBuilder&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;db&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;dbf&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newDocumentBuilder&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;364&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;File&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;fileName&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;365&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;exists&lt;/span&gt;()) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;366&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Document&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;doc&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;db&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;parse&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;file&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;367&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Element&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;docEle&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getDocumentElement&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;368&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;369&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// Print root element of the document &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;370&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Root element of the document: &amp;quot;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;371&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;docEle&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getNodeName&lt;/span&gt;());    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;372&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;373&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;NodeList&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;studentList&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;docEle&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getElementsByTagName&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;student&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;374&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;375&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// Print total student elements in document &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;376&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;377&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Total students: &amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;studentList&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getLength&lt;/span&gt;());    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;378&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;379&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;studentList&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;studentList&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getLength&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;380&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;studentList&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getLength&lt;/span&gt;(); &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;++)&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;381&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;382&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Node&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;node&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;studentList&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;item&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;383&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;384&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;node&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getNodeType&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Node&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ELEMENT_NODE&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;385&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;386&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;387&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;=====================&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;388&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;389&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Element&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;e&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Element&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;node&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;390&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;NodeList&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;nodeList&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getElementsByTagName&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;391&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Name: &amp;quot;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;392&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;nodeList&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;item&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getChildNodes&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;item&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;)     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;393&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getNodeValue&lt;/span&gt;());    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;394&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;395&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;nodeList&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getElementsByTagName&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;grade&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;396&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Grade: &amp;quot;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;397&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;nodeList&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;item&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getChildNodes&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;item&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;)     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;398&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getNodeValue&lt;/span&gt;());    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;399&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;400&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;nodeList&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getElementsByTagName&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;age&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;401&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Age: &amp;quot;&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;402&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;nodeList&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;item&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getChildNodes&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;item&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;)     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;403&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getNodeValue&lt;/span&gt;());     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;404&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;405&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;406&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;407&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;exit&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;408&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;409&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;410&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;e&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;411&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;e&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;412&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;413&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;414&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;415&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;416&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;XMLParser&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;parser&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;XMLParser&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;417&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;parser&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getAllUserNames&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;c:\\test.xml&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;418&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;419&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;420&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//17. 把 Array 转换成 Map &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;421&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.util.Map&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;422&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;org.apache.commons.lang.ArrayUtils&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;423&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;424&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;Main&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;425&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;426&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;427&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;][&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;countries&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;United States&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;New York&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;},&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;United Kingdom&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;London&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;},&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;428&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Netherland&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Amsterdam&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;},&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Japan&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Tokyo&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;},&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;France&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Paris&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;};&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;429&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;430&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Map&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;countryCapitals&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ArrayUtils&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;toMap&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;countries&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;431&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;432&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Capital of Japan is &amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;countryCapitals&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;get&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Japan&amp;quot;&lt;/span&gt;));     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;433&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;Capital of France is &amp;quot;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;countryCapitals&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;get&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;France&amp;quot;&lt;/span&gt;));     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;434&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;435&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;436&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;437&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//18. 发送邮件 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;438&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;javax.mail.*&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;439&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;javax.mail.internet.*&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;440&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.util.*&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;441&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;442&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;postMail&lt;/span&gt;( &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;recipients&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;],&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;subject&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;message&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;from&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;MessagingException&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;443&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;444&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;boolean&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;debug&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;false&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;445&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;446&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//Set the host smtp address &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;447&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Properties&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;props&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Properties&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;448&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;props&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;put&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;mail.smtp.host&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;smtp.example.com&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;449&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;450&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// create some properties and get the default Session &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;451&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Session&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;session&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Session&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getDefaultInstance&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;props&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;452&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;session&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setDebug&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;debug&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;453&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;454&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// create a message &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;455&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Message&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;msg&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;MimeMessage&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;session&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;456&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;457&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// set the from and to address &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;458&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;InternetAddress&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;addressFrom&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;InternetAddress&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;from&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;459&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;msg&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setFrom&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;addressFrom&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;460&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;461&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;InternetAddress&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;addressTo&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;InternetAddress&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;recipients&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;];&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;462&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;recipients&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;length&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;++)&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;463&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;464&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;addressTo&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;InternetAddress&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;recipients&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;]);&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;465&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;466&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;msg&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setRecipients&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Message&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;RecipientType&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;addressTo&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;467&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;468&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// Optional : You can also set your custom headers in the Email if you Want &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;469&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;msg&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;addHeader&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;MyHeaderName&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;myHeaderValue&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;470&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;471&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// Setting the Subject and Content Type &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;472&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;msg&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setSubject&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;subject&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;473&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;msg&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;setContent&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;text/plain&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;474&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Transport&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;send&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;msg&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;475&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;476&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;477&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//19. 发送代数据的HTTP 请求 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;478&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.io.BufferedReader&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;479&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.io.InputStreamReader&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;480&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.net.URL&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;481&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;482&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;Main&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;483&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;484&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;try&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;485&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;URL&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;my_url&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;URL&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;http://cocre.com/&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;486&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedReader&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;br&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;BufferedReader&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;InputStreamReader&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;my_url&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;openStream&lt;/span&gt;()));     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;487&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;strTemp&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;488&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;while&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;!=&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;strTemp&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;br&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;readLine&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;())){&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;489&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;strTemp&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;490&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;491&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;catch&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ex&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;492&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;ex&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;printStackTrace&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;493&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;494&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;495&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;496&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;497&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//20. 改变数组的大小 &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;498&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;/** &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;499&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;* Reallocates an array with a new size, and copies the contents &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;500&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;* of the old array to the new array. &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;501&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;* @param oldArray the old array, to be reallocated. &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;502&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;* @param newSize the new array size. &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;503&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;* @return A new array with the same contents. &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;504&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;*/&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;505&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;resizeArray&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;oldArray&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newSize&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;506&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;oldSize&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;lang&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;reflect&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Array&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getLength&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;oldArray&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;507&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Class&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;elementType&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;oldArray&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getClass&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getComponentType&lt;/span&gt;();     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;508&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newArray&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;lang&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;reflect&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Array&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newInstance&lt;/span&gt;(     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;509&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;elementType&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newSize&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;510&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;preserveLength&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Math&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;min&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;oldSize&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newSize&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;511&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;preserveLength&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;)     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;512&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;arraycopy&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;oldArray&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newArray&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;preserveLength&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;513&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newArray&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;514&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;515&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;516&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// Test routine for resizeArray(). &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;517&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;main&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;518&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;a&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;};&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;519&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;a&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[])&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;resizeArray&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;5&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;520&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;4&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;521&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;5&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;522&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;length&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;++)&lt;/span&gt;&amp;#160; &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;523&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;]);&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;524&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;转：&lt;a title=&quot;http://my.oschina.net/mrlin/blog/48672&quot; href=&quot;http://my.oschina.net/mrlin/blog/48672&quot;&gt;http://my.oschina.net/mrlin/blog/48672&lt;/a&gt;&lt;/span&gt;    &lt;/div&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/628643006/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</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><description>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 [...]&lt;img src=&quot;http://www1.feedsky.com/t1/628643006/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</description><category>Java天地</category><pubDate>Tue, 17 Apr 2012 11:37:28 +0800</pubDate><author>tiger7456</author><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><guid isPermaLink="false">http://www.buguw.com/?p=312</guid><dc:creator>tiger7456</dc:creator><fs:srclink>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</fs:srclink><fs:srcfeed>http://www.buguw.com/?feed=rss2</fs:srcfeed><fs:itemid>feedsky/buguw/~8163640/628643006/6267358</fs:itemid></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><content:encoded>&lt;p&gt;委托模式是软件设计模式中的一项基本技巧。在委托模式中，有两个对象参与处理同一个请求，接受请求的对象将请求委托给另一个对象来处理。委托模式是一项基本技巧，许多其他的模式，如状态模式、策略模式、访问者模式本质上是在更特殊的场合采用了委托模式。委托模式使得我们可以用聚合来替代继承，它还使我们可以模拟mixin。    &lt;br /&gt;“委托”在C#中是一个语言级特性，而在Java语言中没有直接的对应，但是我们可以通过动态代理来实现委托！&lt;/p&gt;
&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26113/&quot;&gt;Codee#26113&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;01&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.lang.reflect.InvocationHandler&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;02&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.lang.reflect.Method&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;03&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.lang.reflect.Proxy&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;04&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;05&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;abstract&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;Delegator&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;implements&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;InvocationHandler&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;06&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//--------------------------------------------&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;07&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;08&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;obj_orgin&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//原始对象&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;09&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;obj_proxy&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//代理对象&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//--------------------------------------------&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;11&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;12&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Delegator&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;13&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;14&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;15&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Delegator&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orgin&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;16&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;createProxy&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orgin&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;17&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;18&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;19&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;20&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;createProxy&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orgin&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;21&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;obj_orgin&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orgin&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;22&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//下面语句中orgin.getClass().getClassLoader()为加载器，orgin.getClass().getInterfaces()为接口集&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;23&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;obj_proxy&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Proxy&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;newProxyInstance&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orgin&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getClass&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getClassLoader&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;(),&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orgin&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getClass&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getInterfaces&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;(),&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;this&lt;/span&gt;); &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//委托&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;24&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;obj_proxy&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;25&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;26&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;27&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;28&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;protected&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;invokeSuper&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Method&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Throwable&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;29&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;invoke&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;obj_orgin&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;30&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;31&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//--------------实现InvocationHandler接口，要求覆盖------------&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;32&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//下面实现的方法是当委托的类调用toString()方法时，操作其他方法而不是该类默认的toString()，这个类的其他方法则不会。&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;33&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;34&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;invoke&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;obj&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Method&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Throwable&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;35&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// 缺省实现：委托给obj_orgin完成对应的操作&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;36&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;equals&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;toString&amp;quot;&lt;/span&gt;)) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//对其做额外处理&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;37&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;invokeSuper&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;$Proxy&amp;quot;&lt;/span&gt;;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;38&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//注意，调用原始对象的方法，而不是代理的（obj==obj_proxy）&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;39&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;invokeSuper&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;);     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;40&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;41&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;42&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;下面的代码，则是作为一个委托的例子，实现Map的功能。&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&amp;#160;&lt;/div&gt;
&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26114/&quot;&gt;Codee#26114&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;01&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.io.IOException&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;02&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.lang.reflect.Method&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;03&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.util.Hashtable&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;04&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.util.Map&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;05&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;org.apache.commons.logging.Log&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;06&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;org.apache.commons.logging.LogFactory&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;07&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;com.bs2.core.UtilLog&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;08&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;09&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;Delegator4Map&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;extends&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Delegator&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Log&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;_log&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;LogFactory&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getLog&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Delegator4Map&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;class&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Map&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orginClass&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//原始对象&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;12&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Map&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;proxyClass&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//代理对象&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;13&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;14&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Map&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getOrgin&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;15&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orginClass&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;16&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;17&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;18&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Map&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getProxy&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;19&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;proxyClass&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;20&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;21&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;22&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Delegator4Map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Map&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orgin&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;23&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;super&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orgin&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;24&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orginClass&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;orgin&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;25&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;proxyClass&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Map&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;super&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;obj_proxy&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;26&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;27&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;28&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;invoke&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;obj&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Method&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Throwable&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;29&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getName&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;equals&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;size&amp;quot;&lt;/span&gt;)) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;//修改size处理逻辑&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;30&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Object&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;res2&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Integer&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;(-&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;31&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;调用委托的方法&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;32&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;res2&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;33&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;34&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;println&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;调用原始的方法&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;35&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;super&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;invoke&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;obj&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;36&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;37&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;38&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;39&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;[]&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;args&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;throws&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;IOException&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;40&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Delegator4Map&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;rtm&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Delegator4Map&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Hashtable&lt;/span&gt;());    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;41&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Map&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;m&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;rtm&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getProxy&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;42&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;size&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;43&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;44&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;/div&gt;
&lt;p&gt;代码编写有个这样的原则：能不用继承就不用继承，能使用委托实现的就不使用继承。两个类有明显示的层级关系时使用继承，没有明显的层级关系，仅仅是为了在一个类中使用另一个类的方法时应该使用委托。   &lt;br /&gt;根据《重构》一书称：现在有滥用继承的趋势，JDK 中 Stack 就是一个滥用继承的典型！    &lt;br /&gt;java.util.Stack 继承自 java.util.Vector，其实 Stack 与 Vector 在用途上完全是风马牛不相及的两个容器。&lt;/p&gt;
&lt;p&gt;原文地址：&lt;a title=&quot;http://my.oschina.net/u/108286/blog/33103&quot; href=&quot;http://my.oschina.net/u/108286/blog/33103&quot;&gt;http://my.oschina.net/u/108286/blog/33103&lt;/a&gt;&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/628643007/buguw/feedsky/s.gif?r=http://www.buguw.com/java%e4%b8%ad%e7%9a%84%e5%a7%94%e6%89%98%e6%a8%a1%e5%bc%8f.html&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</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><description>委托模式是软件设计模式中的一项基本技巧。在委托模式中，有两个对象参与处理同一个请求，接受请求的对象将请求委托给另一个对象来处理。委托模式是一项基本技巧，许多其他的模式，如状态模式、策略模式、访问者模式本质上是在更特殊的场合采用了委托模式。委托模式使得我们可以用聚合来替代继承，它还使我们可以模拟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&amp;#160;&amp;#160;&amp;#160;&amp;#160; 05 public abstract class Delegator implements InvocationHandler { 06&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //-------------------------------------------- 07 08&amp;#160;&amp;#160;&amp;#160;&amp;#160; protected Object obj_orgin = null; //原始对象 09&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; protected Object obj_proxy = null; //代理对象 10&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //-------------------------------------------- 11 12&amp;#160;&amp;#160;&amp;#160;&amp;#160; public Delegator() { 13&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } 14 15&amp;#160;&amp;#160;&amp;#160;&amp;#160; public Delegator(Object orgin) { 16&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [...]&lt;img src=&quot;http://www1.feedsky.com/t1/628643007/buguw/feedsky/s.gif?r=http://www.buguw.com/java%e4%b8%ad%e7%9a%84%e5%a7%94%e6%89%98%e6%a8%a1%e5%bc%8f.html&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</description><category>Java天地</category><pubDate>Tue, 17 Apr 2012 11:33:55 +0800</pubDate><author>tiger7456</author><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><guid isPermaLink="false">http://www.buguw.com/?p=310</guid><dc:creator>tiger7456</dc:creator><fs:srclink>http://www.buguw.com/java%e4%b8%ad%e7%9a%84%e5%a7%94%e6%89%98%e6%a8%a1%e5%bc%8f.html</fs:srclink><fs:srcfeed>http://www.buguw.com/?feed=rss2</fs:srcfeed><fs:itemid>feedsky/buguw/~8163640/628643007/6267358</fs:itemid></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><content:encoded>&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;项目中，需要生成一个流水账号作为数据表主键，完成后，觉得还可以，不敢独享，遂贴出以供拍砖。&lt;/p&gt;
&lt;p&gt;流水号格式为yyyyMMddXXXX，规定每天只能到9999，代码如下：&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;div style=&quot;background: rgb(253,253,253); color: black&quot;&gt;&lt;u&gt;Java语言&lt;/u&gt;: &lt;a href=&quot;http://fayaa.com/code/view/26112/&quot;&gt;Codee#26112&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgb(249,247,237); font-family: &amp;quot;Microsoft Yahei&amp;quot;,&amp;quot;Consolas&amp;quot;,&amp;quot;Lucida Console&amp;quot;,&amp;quot;Courier New&amp;quot;; color: rgb(0,0,0)&quot; class=&quot;source&quot;&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;01&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.text.DecimalFormat&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;02&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.text.SimpleDateFormat&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;03&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;java.util.Date&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;04&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;05&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;PrimaryGenerater&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;06&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;07&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;final&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SERIAL_NUMBER&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;XXXX&amp;quot;&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;// 流水号格式&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;08&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;PrimaryGenerater&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;primaryGenerater&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;09&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;PrimaryGenerater&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;12&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;13&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;/**&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;14&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * 取得PrimaryGenerater的单例实现&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * &lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;16&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * @return&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;17&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; */&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;18&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;PrimaryGenerater&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;getInstance&lt;/span&gt;() &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;19&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;primaryGenerater&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;20&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;synchronized&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;PrimaryGenerater&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;class&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;21&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;primaryGenerater&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;22&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;primaryGenerater&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;PrimaryGenerater&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;23&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;24&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;25&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;26&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;primaryGenerater&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;27&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;28&lt;/span&gt;     &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;29&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;/**&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;30&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * 生成下一个编号&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;31&lt;/span&gt; &lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; */&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;32&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;synchronized&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;generaterNextNumber&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;sno&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;33&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;String&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;id&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;34&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;date&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Date&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;35&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SimpleDateFormat&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;formatter&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SimpleDateFormat&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;yyyyMMdd&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;36&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;sno&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;37&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;id&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;formatter&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;format&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;date&lt;/span&gt;) &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;0001&amp;quot;&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;38&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;39&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;count&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;SERIAL_NUMBER&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;length&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;40&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;StringBuilder&lt;/span&gt; sb &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;StringBuilder&lt;/span&gt;();    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;41&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;0&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;count&lt;/span&gt;; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;++)&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;{&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;42&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sb&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;append&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;43&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;44&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;DecimalFormat&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;df&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;DecimalFormat&lt;/span&gt;(&lt;span style=&quot;color: rgb(163,21,21)&quot;&gt;&amp;quot;0000&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;45&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;id&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;formatter&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;format&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;date&lt;/span&gt;)    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;46&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;format&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;Integer&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;parseInt&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;sno&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;substring&lt;/span&gt;(&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;12&lt;/span&gt;)));    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;47&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;48&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;id&lt;/span&gt;;    &lt;br /&gt;&lt;span style=&quot;color: rgb(0,128,0)&quot;&gt;49&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;br /&gt;&lt;span style=&quot;color: rgb(248,16,176)&quot;&gt;50&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,0)&quot;&gt;}&lt;/span&gt;    &lt;/div&gt;
&lt;p&gt;转：&lt;a title=&quot;http://my.oschina.net/se77en/blog/41122&quot; href=&quot;http://my.oschina.net/se77en/blog/41122&quot;&gt;http://my.oschina.net/se77en/blog/41122&lt;/a&gt;&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/628643008/buguw/feedsky/s.gif?r=http://www.buguw.com/java%e7%94%9f%e6%88%90%e6%b5%81%e6%b0%b4%e5%8f%b7.html&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</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><description>&amp;#160; 项目中，需要生成一个流水账号作为数据表主键，完成后，觉得还可以，不敢独享，遂贴出以供拍砖。 流水号格式为yyyyMMddXXXX，规定每天只能到9999，代码如下： &amp;#160; &amp;#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&amp;#160;&amp;#160;&amp;#160;&amp;#160; private static final String SERIAL_NUMBER = &amp;#34;XXXX&amp;#34;; // 流水号格式 08&amp;#160;&amp;#160;&amp;#160;&amp;#160; private static PrimaryGenerater primaryGenerater = null; 09 10&amp;#160;&amp;#160;&amp;#160;&amp;#160; private PrimaryGenerater() { 11&amp;#160;&amp;#160;&amp;#160;&amp;#160; } 12 13&amp;#160;&amp;#160;&amp;#160;&amp;#160; /** 14 &amp;#160;&amp;#160;&amp;#160;&amp;#160; * 取得PrimaryGenerater的单例实现 15 [...]&lt;img src=&quot;http://www1.feedsky.com/t1/628643008/buguw/feedsky/s.gif?r=http://www.buguw.com/java%e7%94%9f%e6%88%90%e6%b5%81%e6%b0%b4%e5%8f%b7.html&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</description><category>Java天地</category><pubDate>Tue, 17 Apr 2012 11:27:14 +0800</pubDate><author>tiger7456</author><comments>http://www.buguw.com/java%e7%94%9f%e6%88%90%e6%b5%81%e6%b0%b4%e5%8f%b7.html#comments</comments><guid isPermaLink="false">http://www.buguw.com/?p=308</guid><dc:creator>tiger7456</dc:creator><fs:srclink>http://www.buguw.com/java%e7%94%9f%e6%88%90%e6%b5%81%e6%b0%b4%e5%8f%b7.html</fs:srclink><fs:srcfeed>http://www.buguw.com/?feed=rss2</fs:srcfeed><fs:itemid>feedsky/buguw/~8163640/628643008/6267358</fs:itemid></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><content:encoded>&lt;p&gt;ant执行的时候内存溢出&lt;/p&gt;
&lt;p&gt;1、在ant.bat文件中增加缓存，追加-Xms128m -Xmx256m参数：&lt;/p&gt;
&lt;p&gt;java -Xms128m -Xmx256m -classpath &amp;quot;%ANTCLASSPATH%&amp;quot; org.apache.tools.ant.Main %ANT_ARGS%&lt;/p&gt;
&lt;p&gt;2、使用eclipse运行ant时，应如下设定参数：&lt;/p&gt;
&lt;p&gt;build.xml文件右键-〉运行-〉外部工具-〉选择JRE页-〉VM参数处设定 -Xms128m -Xmx256m即可。 &lt;/p&gt;
&lt;p&gt;转至：&lt;a title=&quot;http://hi.baidu.com/fan_run/blog/item/c5d2963406952f46241f1411.html&quot; href=&quot;http://hi.baidu.com/fan_run/blog/item/c5d2963406952f46241f1411.html&quot;&gt;http://hi.baidu.com/fan_run/blog/item/c5d2963406952f46241f1411.html&lt;/a&gt;&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/628643009/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</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><description>ant执行的时候内存溢出 1、在ant.bat文件中增加缓存，追加-Xms128m -Xmx256m参数： java -Xms128m -Xmx256m -classpath &amp;#34;%ANTCLASSPATH%&amp;#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&lt;img src=&quot;http://www1.feedsky.com/t1/628643009/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</description><category>ant</category><category>Java天地</category><pubDate>Tue, 17 Apr 2012 11:17:15 +0800</pubDate><author>tiger7456</author><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><guid isPermaLink="false">http://www.buguw.com/?p=306</guid><dc:creator>tiger7456</dc:creator><fs:srclink>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</fs:srclink><fs:srcfeed>http://www.buguw.com/?feed=rss2</fs:srcfeed><fs:itemid>feedsky/buguw/~8163640/628643009/6267358</fs:itemid></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><content:encoded>&lt;p&gt;一、到官方上下载svn1.8.3，下载后的文件名叫site-1.8.3.zip&lt;/p&gt;
&lt;p&gt;二、解压，删除解压后得到的文件中除了名叫features和plugins的两个文件夹以外的其他所有文件。&lt;/p&gt;
&lt;p&gt;三、在MyEclipse10.0的安装目录中自己新建一个文件夹（我的叫MyPlugins）然后把解压并删除过文件后得到的那个SVN文件夹 复制到该目录；&lt;/p&gt;
&lt;p&gt;四、在MyEclipse10.0的安装目录下的dropins文件夹中建立一个文本文件，内容输path=你的svn路径，（我的是path=D:\\MyEclipse 10\\MyPlugins\\SVN-1.8.5），然后保存，保存后将该文件名重命名成SVN.lnk(记住扩展名是是.lnk不是网上有网友说的link).&lt;/p&gt;
&lt;p&gt;五、接下来删除 MyEclipse10.0的安装目录下configuration文件夹中的org.eclipse.update文件夹。&lt;/p&gt;
&lt;p&gt;六、重启－〉OK。&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/628643010/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</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><description>一、到官方上下载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。&lt;img src=&quot;http://www1.feedsky.com/t1/628643010/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</description><category>Java天地</category><category>svn</category><category>MyEclipse</category><pubDate>Thu, 12 Apr 2012 11:42:55 +0800</pubDate><author>tiger7456</author><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><guid isPermaLink="false">http://www.buguw.com/?p=304</guid><dc:creator>tiger7456</dc:creator><fs:srclink>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</fs:srclink><fs:srcfeed>http://www.buguw.com/?feed=rss2</fs:srcfeed><fs:itemid>feedsky/buguw/~8163640/628643010/6267358</fs:itemid></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><content:encoded>&lt;p&gt;Subclipse是一个很优秀的SVN客户端。它提供了ClientAdapter、JavaHL和SVNKit等方式于SVN服务交互。官方强烈推荐使用JavaHL的方式。JavaHL使用JNI的调用SVN的本地库。速度快，稳定可靠。&lt;/p&gt;
&lt;p&gt;但是在64位的Win7下，就比较麻烦了。默认安装好后， Subclipse的配置界面中，显示的javaHL的状态是“unavailable“，如图。这是因为系统中没有安装svn的javahl。&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;http://dl.iteye.com/upload/picture/pic/99103/0aff37d6-2575-3c6f-bd26-93c1b4830049.jpg&quot; width=&quot;263&quot; height=&quot;294&quot; /&gt; &lt;img alt=&quot;&quot; src=&quot;http://dl.iteye.com/upload/picture/pic/99101/52f714bd-a98d-33f7-8ce6-7445b7bdf6c6.jpg&quot; width=&quot;263&quot; height=&quot;294&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Eclipse 中使用Subsvn时的错误提示 &lt;/strong&gt;&lt;strong&gt;原因是SVN接口 Unavailable &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;解决办法是安装 SlikSVN，可以在&lt;a href=&quot;http://www.sliksvn.com/en/download&quot;&gt;http://www.sliksvn.com/en/download&lt;/a&gt;下载SlikSVN，不同的Subsvn要安装不同的SlikSVN。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;当前版本&lt;/em&gt;&lt;/strong&gt;&lt;strong&gt;&lt;em&gt;对应&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Subclipse Version&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;SVN/JavaHL Version&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;1.6.x&lt;/p&gt;
&lt;p&gt;1.6.x&lt;/p&gt;
&lt;p&gt;1.4.x&lt;/p&gt;
&lt;p&gt;1.5.x&lt;/p&gt;
&lt;p&gt;1.2.x&lt;/p&gt;
&lt;p&gt;1.4.x&lt;/p&gt;
&lt;p&gt;1.0.x&lt;/p&gt;
&lt;p&gt;1.4.x&lt;/p&gt;
&lt;p&gt;下载安装完SlikSVN后，配置系统环境变量，把SlikSVN安装目录下的bin目录加入到系统环境变量：&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;&quot; src=&quot;http://dl.iteye.com/upload/picture/pic/99109/77dcfe08-39b7-3183-bc74-dbdb63af4e61.jpg&quot; width=&quot;395&quot; height=&quot;406&quot; /&gt;&lt;/p&gt;
&lt;p&gt;重新启动Eclipse，你会看到Subsvn已经有JavaHL库了&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;点击查看原始大小图片&quot; alt=&quot;&quot; src=&quot;http://dl.iteye.com/upload/picture/pic/99111/0a0134d4-1127-355f-a98e-4c96f6532a32.jpg&quot; width=&quot;700&quot; height=&quot;610&quot; /&gt;&lt;/p&gt;
&lt;p&gt;转载至：&lt;a title=&quot;http://hkp.iteye.com/blog/1174499&quot; href=&quot;http://hkp.iteye.com/blog/1174499&quot;&gt;http://hkp.iteye.com/blog/1174499&lt;/a&gt;&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/628643011/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</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><description>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&lt;img src=&quot;http://www1.feedsky.com/t1/628643011/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</description><category>eclipse</category><category>svn</category><category>编程学习经验</category><pubDate>Thu, 12 Apr 2012 09:52:18 +0800</pubDate><author>tiger7456</author><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><guid isPermaLink="false">http://www.buguw.com/?p=302</guid><dc:creator>tiger7456</dc:creator><fs:srclink>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</fs:srclink><fs:srcfeed>http://www.buguw.com/?feed=rss2</fs:srcfeed><fs:itemid>feedsky/buguw/~8163640/628643011/6267358</fs:itemid></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><content:encoded>&lt;p&gt;&lt;strong&gt;什么是位运算？&lt;/strong&gt;&lt;br /&gt;
程序中的所有数在计算机内存中都是以二进制的形式储存的。位运算说穿了，就是直接对整数在内存中的二进制位进行操作。比如，and运算本来是一个逻辑运算符，但整数与整数之间也可以进行and运算。举个例子，6的二进制是110，11的二进制是1011，那么6 and 11的结果就是2，它是二进制对应位进行逻辑运算的结果（0表示False，1表示True，空位都当0处理）：&lt;br /&gt;
110&lt;br /&gt;
AND 1011&lt;br /&gt;
----------&lt;br /&gt;
0010 --&amp;gt; 2&lt;br /&gt;
由于位运算直接对内存数据进行操作，不需要转成十进制，因此处理速度非常快。当然有人会说，这个快了有什么用，计算6 and 11没有什么实际意义啊。这一系列的文章就将告诉你，位运算到底可以干什么，有些什么经典应用，以及如何用位运算优化你的程序。&lt;br /&gt;
&lt;strong&gt;Pascal和C中的位运算符号&lt;/strong&gt;&lt;br /&gt;
下面的a和b都是整数类型，则：&lt;br /&gt;
C语言   | Pascal语言&lt;br /&gt;
------- +-------------&lt;br /&gt;
a &amp;amp; b  | a and b&lt;br /&gt;
a | b    | a or b&lt;br /&gt;
a ^ b   | a xor b&lt;br /&gt;
~a      | not a&lt;br /&gt;
a &amp;lt;&amp;lt; b | a shl b&lt;br /&gt;
a &amp;gt;&amp;gt; b | a shr b&lt;br /&gt;
注意C中的逻辑运算和位运算符号是不同的。520|1314=1834，但520||1314=1，因为逻辑运算时520和1314都相当于True。同样的，!a和~a也是有区别的。&lt;br /&gt;
&lt;strong&gt;各种位运算的使用&lt;/strong&gt;&lt;br /&gt;
=== 1. and运算 ===&lt;br /&gt;
and运算通常用于二进制取位操作，例如一个数 and 1的结果就是取二进制的最末位。这可以用来判断一个整数的奇偶，二进制的最末位为0表示该数为偶数，最末位为1表示该数为奇数.&lt;br /&gt;
=== 2. or运算 ===&lt;br /&gt;
or运算通常用于二进制特定位上的无条件赋值，例如一个数or 1的结果就是把二进制最末位强行变成1。如果需要把二进制最末位变成0，对这个数or 1之后再减一就可以了，其实际意义就是把这个数强行变成最接近的偶数。&lt;br /&gt;
=== 3. xor运算 ===&lt;br /&gt;
xor运算通常用于对二进制的特定一位进行取反操作，因为异或可以这样定义：0和1异或0都不变，异或1则取反。&lt;br /&gt;
xor运算的逆运算是它本身，也就是说两次异或同一个数最后结果不变，即(a xor b) xor b = a。xor运算可以用于简单的加密，比如我想对我MM说1314520，但怕别人知道，于是双方约定拿我的生日19880516作为密钥。1314520 xor 19880516 = 20665500，我就把20665500告诉MM。MM再次计算20665500 xor 19880516的值，得到1314520，于是她就明白了我的企图。&lt;br /&gt;
下面我们看另外一个东西。定义两个符号#和@（我怎么找不到那个圈里有个叉的字符），这两个符号互为逆运算，也就是说(x # y) @ y = x。现在依次执行下面三条命令，结果是什么？&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;table width=&quot;400&quot; border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign=&quot;top&quot; width=&quot;400&quot;&gt;&lt;code&gt;x &amp;lt;- x # y&lt;br /&gt;
y &amp;lt;- x @ y&lt;br /&gt;
x &amp;lt;- x @ y&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;执行了第一句后x变成了x # y。那么第二句实质就是y &amp;lt;- x # y @ y，由于#和@互为逆运算，那么此时的y变成了原来的x。第三句中x实际上被赋值为(x # y) @ x，如果#运算具有交换律，那么赋值后x就变成最初的y了。这三句话的结果是，x和y的位置互换了。&lt;br /&gt;
加法和减法互为逆运算，并且加法满足交换律。把#换成+，把@换成-，我们可以写出一个不需要临时变量的swap过程(Pascal)。&lt;/p&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;procedure&lt;/span&gt; swap(&lt;span style=&quot;color: #0000ff;&quot;&gt;var&lt;/span&gt; a,b:longint);&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;begin&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a:=a + b;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   b:=a - b;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a:=a - b;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;end&lt;/span&gt;;&lt;/pre&gt;
&lt;p&gt;好了，刚才不是说xor的逆运算是它本身吗？于是我们就有了一个看起来非常诡异的swap过程：&lt;/p&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;procedure&lt;/span&gt; swap(&lt;span style=&quot;color: #0000ff;&quot;&gt;var&lt;/span&gt; a,b:longint);&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;begin&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a:=a &lt;span style=&quot;color: #0000ff;&quot;&gt;xor&lt;/span&gt; b;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   b:=a &lt;span style=&quot;color: #0000ff;&quot;&gt;xor&lt;/span&gt; b;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a:=a &lt;span style=&quot;color: #0000ff;&quot;&gt;xor&lt;/span&gt; b;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;end&lt;/span&gt;;&lt;/pre&gt;
&lt;p&gt;=== 4. not运算 ===&lt;/p&gt;
&lt;p&gt;not运算的定义是把内存中的0和1全部取反。使用not运算时要格外小心，你需要注意整数类型有没有符号。如果not的对象是无符号整数（不能表示负数），那么得到的值就是它与该类型上界的差，因为无符号类型的数是用$0000到$FFFF依次表示的。下面的两个程序（仅语言不同）均返回65435。&lt;/p&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;var&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a:word;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;begin&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a:=100;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a:=&lt;span style=&quot;color: #0000ff;&quot;&gt;not&lt;/span&gt; a;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   writeln(a);&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;end&lt;/span&gt;.&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; main()&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;{&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;short&lt;/span&gt; a=100;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    a = ~a;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    printf( &quot;&lt;span style=&quot;color: #8b0000;&quot;&gt;%d\n&lt;/span&gt;&quot;, a );&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; 0;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;如果not的对象是有符号的整数，情况就不一样了，稍后我们会在“整数类型的储存”小节中提到。&lt;/p&gt;
&lt;p&gt;=== 5. shl运算 ===&lt;/p&gt;
&lt;p&gt;a shl b就表示把a转为二进制后左移b位（在后面添b个0）。例如100的二进制为1100100，而110010000转成十进制是400，那么100 shl 2 = 400。可以看出，a shl b的值实际上就是a乘以2的b次方，因为在二进制数后添一个0就相当于该数乘以2。&lt;/p&gt;
&lt;p&gt;通常认为a shl 1比a * 2更快，因为前者是更底层一些的操作。因此程序中乘以2的操作请尽量用左移一位来代替。&lt;/p&gt;
&lt;p&gt;定义一些常量可能会用到shl运算。你可以方便地用1 shl 16 - 1来表示65535。很多算法和数据结构要求数据规模必须是2的幂，此时可以用shl来定义Max_N等常量。&lt;/p&gt;
&lt;p&gt;=== 6. shr运算 ===&lt;/p&gt;
&lt;p&gt;和shl相似，a shr b表示二进制右移b位（去掉末b位），相当于a除以2的b次方（取整）。我们也经常用shr 1来代替div 2，比如二分查找、堆的插入操作等等。想办法用shr代替除法运算可以使程序效率大大提高。最大公约数的二进制算法用除以2操作来代替慢得出奇的mod运算，效率可以提高60%。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;位运算的简单应用&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;有时我们的程序需要一个规模不大的Hash表来记录状态。比如，做数独时我们需要27个Hash表来统计每一行、每一列和每一个小九宫格里已经有哪些数了。此时，我们可以用27个小于2^9的整数进行记录。例如，一个只填了2和5的小九宫格就用数字18表示（二进制为000010010），而某一行的状态为511则表示这一行已经填满。需要改变状态时我们不需要把这个数转成二进制修改后再转回去，而是直接进行位操作。在搜索时，把状态表示成整数可以更好地进行判重等操作。&lt;a href=&quot;http://www.vijos.cn/Problem_Show.asp?id=1197&quot;&gt;这道题&lt;/a&gt;是在搜索中使用位运算加速的经典例子。以后我们会看到更多的例子。&lt;/p&gt;
&lt;p&gt;下面列举了一些常见的二进制位的变换操作。&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;功能                    | 示例                                | 位运算&lt;/p&gt;
&lt;p&gt;----------------------+---------------------------+--------------------&lt;/p&gt;
&lt;p&gt;去掉最后一位     | (101101-&amp;gt;10110)            | x shr 1&lt;/p&gt;
&lt;p&gt;在最后加一个0   | (101101-&amp;gt;1011010)         | x shl 1&lt;/p&gt;
&lt;p&gt;在最后加一个1   | (101101-&amp;gt;1011011)         | x shl 1+1&lt;/p&gt;
&lt;p&gt;把最后一位变成1 | (101100-&amp;gt;101101)          | x or 1&lt;/p&gt;
&lt;p&gt;把最后一位变成0 | (101101-&amp;gt;101100)          | x or 1-1&lt;/p&gt;
&lt;p&gt;最后一位取反      | (101101-&amp;gt;101100)          | x xor 1&lt;/p&gt;
&lt;p&gt;把右数第k位变成1 | (101001-&amp;gt;101101,k=3) | x or (1 shl (k-1))&lt;/p&gt;
&lt;p&gt;把右数第k位变成0 | (101101-&amp;gt;101001,k=3) | x and not (1 shl (k-1))&lt;/p&gt;
&lt;p&gt;右数第k位取反 | (101001-&amp;gt;101101,k=3) | x xor (1 shl (k-1))&lt;/p&gt;
&lt;p&gt;取末三位 | (1101101-&amp;gt;101) | x and 7&lt;/p&gt;
&lt;p&gt;取末k位 | (1101101-&amp;gt;1101,k=5) | x and (1 shl k-1)&lt;/p&gt;
&lt;p&gt;取右数第k位 | (1101101-&amp;gt;1,k=4) | x shr (k-1) and 1&lt;/p&gt;
&lt;p&gt;把末k位变成1 | (101001-&amp;gt;101111,k=4) | x or (1 shl k-1)&lt;/p&gt;
&lt;p&gt;末k位取反 | (101001-&amp;gt;100110,k=4) | x xor (1 shl k-1)&lt;/p&gt;
&lt;p&gt;把右边连续的1变成0 | (100101111-&amp;gt;100100000) | x and (x+1)&lt;/p&gt;
&lt;p&gt;把右起第一个0变成1 | (100101111-&amp;gt;100111111) | x or (x+1)&lt;/p&gt;
&lt;p&gt;把右边连续的0变成1 | (11011000-&amp;gt;11011111) | x or (x-1)&lt;/p&gt;
&lt;p&gt;取右边连续的1 | (100101111-&amp;gt;1111) | (x xor (x+1)) shr 1&lt;/p&gt;
&lt;p&gt;去掉右起第一个1的左边 | (100101000-&amp;gt;1000) | x and (x xor (x-1))&lt;/p&gt;
&lt;p&gt;最后这一个在树状数组中会用到。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pascal和C中的16进制表示&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Pascal中需要在16进制数前加$符号表示，C中需要在前面加0x来表示。这个以后我们会经常用到。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;整数类型的储存&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;我们前面所说的位运算都没有涉及负数，都假设这些运算是在unsigned/word类型（只能表示正数的整型）上进行操作。但计算机如何处理有正负符号的整数类型呢？下面两个程序都是考察16位整数的储存方式（只是语言不同）。&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;table width=&quot;400&quot; border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign=&quot;top&quot; width=&quot;400&quot;&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;var&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a,b:integer;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;begin&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a:=$0000;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   b:=$0001;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   write(a,' ',b,' ');&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a:=$FFFE;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   b:=$FFFF;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   write(a,' ',b,' ');&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   a:=$7FFF;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   b:=$8000;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;   writeln(a,' ',b);&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;end&lt;/span&gt;.&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;table width=&quot;400&quot; border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign=&quot;top&quot; width=&quot;400&quot;&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; main()&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;{&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;short&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; a, b;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    a = 0x0000;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    b = 0x0001;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    printf( &quot;&lt;span style=&quot;color: #8b0000;&quot;&gt;%d %d &lt;/span&gt;&quot;, a, b );&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    a = 0xFFFE;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    b = 0xFFFF;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    printf( &quot;&lt;span style=&quot;color: #8b0000;&quot;&gt;%d %d &lt;/span&gt;&quot;, a, b );&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    a = 0x7FFF;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    b = 0x8000;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    printf( &quot;&lt;span style=&quot;color: #8b0000;&quot;&gt;%d %d\n&lt;/span&gt;&quot;, a, b );&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; 0;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;}&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;两个程序的输出均为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。这种整数储存方式叫做“补码”。&lt;/p&gt;
&lt;p&gt;转自：&lt;a title=&quot;http://www.matrix67.com/blog/archives/263&quot; href=&quot;http://www.matrix67.com/blog/archives/263&quot;&gt;http://www.matrix67.com/blog/archives/263&lt;/a&gt;&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/628643012/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</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><description>什么是位运算？ 程序中的所有数在计算机内存中都是以二进制的形式储存的。位运算说穿了，就是直接对整数在内存中的二进制位进行操作。比如，and运算本来是一个逻辑运算符，但整数与整数之间也可以进行and运算。举个例子，6的二进制是110，11的二进制是1011，那么6 and 11的结果就是2，它是二进制对应位进行逻辑运算的结果（0表示False，1表示True，空位都当0处理）： 110 AND 1011 ---------- 0010 --&amp;#62; 2 由于位运算直接对内存数据进行操作，不需要转成十进制，因此处理速度非常快。当然有人会说，这个快了有什么用，计算6 and 11没有什么实际意义啊。这一系列的文章就将告诉你，位运算到底可以干什么，有些什么经典应用，以及如何用位运算优化你的程序。 Pascal和C中的位运算符号 下面的a和b都是整数类型，则： C语言   &amp;#124; Pascal语言 ------- +------------- a &amp;#38; b  &amp;#124; a and b a &amp;#124; b    &amp;#124; a or b a ^ b   &amp;#124; a xor b ~a      &amp;#124; not a a &amp;#60;&amp;#60; b &amp;#124; a shl b a &amp;#62;&amp;#62; [...]&lt;img src=&quot;http://www1.feedsky.com/t1/628643012/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</description><category>数据结构 &amp; 算法</category><category>位运算</category><pubDate>Fri, 06 Apr 2012 10:34:33 +0800</pubDate><author>tiger7456</author><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><guid isPermaLink="false">http://www.buguw.com/?p=298</guid><dc:creator>tiger7456</dc:creator><fs:srclink>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</fs:srclink><fs:srcfeed>http://www.buguw.com/?feed=rss2</fs:srcfeed><fs:itemid>feedsky/buguw/~8163640/628643012/6267358</fs:itemid></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><content:encoded>&lt;p&gt;这是一个很有意思的问题，也是在面试中最容易被问到的问题之一。这个问题有个正式的名字叫&lt;a href=&quot;http://en.wikipedia.org/wiki/Hamming_weight&quot;&gt;Hamming_weight&lt;/a&gt;，而且wikipedia上也提供了很好的位运算解决的方法，这个下面也会提到。&lt;/p&gt;
&lt;p&gt;解决这个问题的第一想法是一位一位的观察，判断是否为1，是则计数器加一，否则跳到下一位，于是很容易有这样的程序。&lt;/p&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; test(&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; n){&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;  &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; count=0;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;  &lt;span style=&quot;color: #0000ff;&quot;&gt;while&lt;/span&gt;(n != 0){&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;      &lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt;(n%2 ==1)&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;      count++;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;      n /= 2;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt; }&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; count;}&lt;/pre&gt;
&lt;p&gt;或者和其等价的位运算版本：&lt;/p&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; test(&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; n){&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; count=0;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;while&lt;/span&gt;(n != 0){&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;        count += n&amp;amp;1;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;        n &amp;gt;&amp;gt;= 1;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    }&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; count;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;}&lt;/pre&gt;
&lt;p&gt;这样的方法复杂度为二进制的位数，即&lt;img src=&quot;http://latex.codecogs.com/gif.latex?\log_2n&quot; alt=&quot;&quot; /&gt;，于是可是想一下，有没有只与二进制中1的位数相关的算法呢。&lt;/p&gt;
&lt;p&gt;可以考虑每次找到从最低位开始遇到的第一个1，计数，再把它清零，清零的位运算操作是与一个零，但是在有1的这一位与零的操作要同时不影响未统计过的位数和已经统计过的位数，于是可以有这样一个操作 n&amp;amp;(n-1) ，这个操作对比当前操作位高的位没有影响，对低位则完全清零。拿6（110）来做例子，第一次 110&amp;amp;101=100，这次操作成功的把从低位起第一个1消掉了，同时计数器加1，第二次100&amp;amp;011=000，同理又统计了高位的一个1，此时n已变为0，不需要再继续了，于是110中有2个1。&lt;/p&gt;
&lt;p&gt;代码如下：&lt;/p&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; test(&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; n){&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; count=0;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;while&lt;/span&gt;(n != 0){&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;        n &amp;amp;= n-1;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;        count ++;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    }&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; count;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;还有查表法：&lt;/p&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; idx[256]={0,1,1,,8}&lt;span style=&quot;color: #008000;&quot;&gt;//0~255中含1的个数&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; OneCount(&lt;span style=&quot;color: #0000ff;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; x)&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;{&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;  &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; count=0;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;  &lt;span style=&quot;color: #0000ff;&quot;&gt;for&lt;/span&gt;(; x&amp;gt;0; x&amp;gt;&amp;gt;=8)&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;     count+=idx[x&amp;amp;255];&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;  &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; count;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;}&lt;/pre&gt;
&lt;p&gt;上面算法最多只需要4次循环，用空间换取时间。&lt;/p&gt;
&lt;p&gt;这种算法的另一种形式：&lt;/p&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; idx[256]={0,1,1,..,8}&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; OneCount(&lt;span style=&quot;color: #0000ff;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; x)&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;{&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;  &lt;span style=&quot;color: #0000ff;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;char&lt;/span&gt;* p=(&lt;span style=&quot;color: #0000ff;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;char&lt;/span&gt;*)&amp;amp;x;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;  &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; idx[*p]+idx[*(p+1)]+idx[*(p+2)]+idx[*(p+3)];&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;这几个方法虽然也用到了位运算，但是并没有体现其神奇之处，下面这个版本则彰显位运算的强大能力，若不告诉这个函数的功能，一般一眼看上去是想不到这是做什么的，这也是wikipedia上给出的计算hamming_weight方法。&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; test(&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; n){&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    n = (n&amp;amp;0x55555555) + ((n&amp;gt;&amp;gt;1)&amp;amp;0x55555555);&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    n = (n&amp;amp;0x33333333) + ((n&amp;gt;&amp;gt;2)&amp;amp;0x33333333);&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    n = (n&amp;amp;0x0f0f0f0f) + ((n&amp;gt;&amp;gt;4)&amp;amp;0x0f0f0f0f);&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    n = (n&amp;amp;0x00ff00ff) + ((n&amp;gt;&amp;gt;8)&amp;amp;0x00ff00ff);&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    n = (n&amp;amp;0x0000ffff) + ((n&amp;gt;&amp;gt;16)&amp;amp;0x0000ffff);&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;    &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; n;&lt;/pre&gt;
&lt;pre style=&quot;background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;&quot;&gt;}&lt;/pre&gt;
&lt;p&gt;有循环，5个位运算语句，一次搞定。&lt;/p&gt;
&lt;p&gt;比如这个例子，143的二进制表示是10001111，这里只有8位，高位的0怎么进行与的位运算也是0，所以只考虑低位的运算，按照这个算法走一次&lt;/p&gt;
&lt;p&gt;+---+---+---+---+---+---+---+---+&lt;/p&gt;
&lt;p&gt;| 1 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | &amp;lt;---143&lt;/p&gt;
&lt;p&gt;+---+---+---+---+---+---+---+---+&lt;/p&gt;
&lt;p&gt;| 0 1 | 0 0 | 1 0 | 1 0 | &amp;lt;---第一次运算后&lt;/p&gt;
&lt;p&gt;+-------+-------+-------+-------+&lt;/p&gt;
&lt;p&gt;| 0 0 0 1 | 0 1 0 0 | &amp;lt;---第二次运算后&lt;/p&gt;
&lt;p&gt;+---------------+---------------+&lt;/p&gt;
&lt;p&gt;| 0 0 0 0 0 1 0 1 | &amp;lt;---第三次运算后，得数为5&lt;/p&gt;
&lt;p&gt;+-------------------------------+&lt;/p&gt;
&lt;p&gt;这里运用了分治的思想，先计算每对相邻的2位中有几个1，再计算每相邻的4位中有几个1，下来8位，16位，32位，因为2^5＝32，所以对于32位的机器，5条位运算语句就够了。&lt;/p&gt;
&lt;p&gt;像这里第二行第一个格子中，01就表示前两位有1个1，00表示下来的两位中没有1，其实同理。再下来01+00=0001表示前四位中有1个1，同样的10+10=0100表示低四位中有4个1，最后一步0001+0100=00000101表示整个8位中有5个1。&lt;/p&gt;
&lt;p&gt;Hamming weight原理讲解：&lt;a href=&quot;http://www.buguw.com/wp-content/uploads/2012/04/Hamming-weight.pdf&quot;&gt;Hamming weight&lt;/a&gt;&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/628643013/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</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><description>这是一个很有意思的问题，也是在面试中最容易被问到的问题之一。这个问题有个正式的名字叫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&amp;#38;1; n &amp;#62;&amp;#62;= 1; } return count; } 这样的方法复杂度为二进制的位数，即，于是可是想一下，有没有只与二进制中1的位数相关的算法呢。 可以考虑每次找到从最低位开始遇到的第一个1，计数，再把它清零，清零的位运算操作是与一个零，但是在有1的这一位与零的操作要同时不影响未统计过的位数和已经统计过的位数，于是可以有这样一个操作 n&amp;#38;(n-1) ，这个操作对比当前操作位高的位没有影响，对低位则完全清零。拿6（110）来做例子，第一次 110&amp;#38;101=100，这次操作成功的把从低位起第一个1消掉了，同时计数器加1，第二次100&amp;#38;011=000，同理又统计了高位的一个1，此时n已变为0，不需要再继续了，于是110中有2个1。 代码如下： int test(int n){ int count=0; while(n != 0){ n &amp;#38;= n-1; [...]&lt;img src=&quot;http://www1.feedsky.com/t1/628643013/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</description><category>数据结构 &amp; 算法</category><pubDate>Thu, 05 Apr 2012 11:49:28 +0800</pubDate><author>tiger7456</author><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><guid isPermaLink="false">http://www.buguw.com/?p=289</guid><dc:creator>tiger7456</dc:creator><fs:srclink>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</fs:srclink><fs:srcfeed>http://www.buguw.com/?feed=rss2</fs:srcfeed><fs:itemid>feedsky/buguw/~8163640/628643013/6267358</fs:itemid></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><content:encoded>&lt;p&gt;原文地址：&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-1&quot;&gt;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-1&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;摘要&lt;/h3&gt;
&lt;p&gt;本文以MySQL数据库为研究对象，讨论与数据库索引相关的一些话题。特别需要说明的是，MySQL支持诸多存储引擎，而各种存储引擎对索引的支持也各不相同，因此MySQL数据库支持多种索引类型，如BTree索引，哈希索引，全文索引等等。为了避免混乱，本文将只关注于BTree索引，因为这是平常使用MySQL时主要打交道的索引，至于哈希索引和全文索引本文暂不讨论。&lt;/p&gt;
&lt;p&gt;文章主要内容分为三个部分。&lt;/p&gt;
&lt;p&gt;第一部分主要从数据结构及算法理论层面讨论MySQL数据库索引的数理基础。&lt;/p&gt;
&lt;p&gt;第二部分结合MySQL数据库中MyISAM和InnoDB数据存储引擎中索引的架构实现讨论聚集索引、非聚集索引及覆盖索引等话题。&lt;/p&gt;
&lt;p&gt;第三部分根据上面的理论基础，讨论MySQL中高性能使用索引的策略。&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-1&quot;&gt;摘要&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2&quot;&gt;数据结构及算法基础&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-1&quot;&gt;索引的本质&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-2&quot;&gt;B-Tree和B+Tree&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-2-3&quot;&gt;为什么使用B-Tree（B+Tree）&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-3&quot;&gt;MySQL索引实现&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-3-1&quot;&gt;MyISAM索引实现&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-3-2&quot;&gt;InnoDB索引实现&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-4&quot;&gt;索引使用策略及优化&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-4-1&quot;&gt;示例数据库&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-4-2&quot;&gt;最左前缀原理与相关优化&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-4-3&quot;&gt;索引选择性与前缀索引&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-4-4&quot;&gt;InnoDB的主键选择与插入优化&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-5&quot;&gt;后记&lt;/a&gt;    &lt;br /&gt;&lt;a href=&quot;http://www.codinglabs.org/html/theory-of-mysql-index.html#nav-6&quot;&gt;参考文献&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;&lt;a name=&quot;nav-2&quot;&gt;&lt;/a&gt;数据结构及算法基础&lt;/h3&gt;
&lt;h4&gt;&lt;a name=&quot;nav-2-1&quot;&gt;&lt;/a&gt;索引的本质&lt;/h4&gt;
&lt;p&gt;MySQL官方对索引的定义为：&lt;strong&gt;索引（Index）是帮助MySQL高效获取数据的数据结构。&lt;/strong&gt;提取句子主干，就可以得到索引的本质：索引是数据结构。&lt;/p&gt;
&lt;p&gt;我们知道，数据库查询是数据库的最主要功能之一。我们都希望查询数据的速度能尽可能的快，因此数据库系统的设计者会从查询算法的角度进行优化。最基本的查询算法当然是&lt;a href=&quot;http://en.wikipedia.org/wiki/Linear_search&quot;&gt;顺序查找&lt;/a&gt;（linear search），这种复杂度为O(n)的算法在数据量很大时显然是糟糕的，好在计算机科学的发展提供了很多更优秀的查找算法，例如&lt;a href=&quot;http://en.wikipedia.org/wiki/Binary_search_algorithm&quot;&gt;二分查找&lt;/a&gt;（binary search）、&lt;a href=&quot;http://en.wikipedia.org/wiki/Binary_search_tree&quot;&gt;二叉树查找&lt;/a&gt;（binary tree search）等。如果稍微分析一下会发现，每种查找算法都只能应用于特定的数据结构之上，例如二分查找要求被检索数据有序，而二叉树查找只能应用于&lt;a href=&quot;http://en.wikipedia.org/wiki/Binary_search_tree&quot;&gt;二叉查找树&lt;/a&gt;上，但是数据本身的组织结构不可能完全满足各种数据结构（例如，理论上不可能同时将两列都按顺序进行组织），所以，&lt;strong&gt;在数据之外，数据库系统还维护着满足特定查找算法的数据结构，这些数据结构以某种方式引用（指向）数据，这样就可以在这些数据结构上实现高级查找算法。这种数据结构，就是索引&lt;/strong&gt;。&lt;/p&gt;
&lt;p&gt;看一个例子：&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image2.png&quot; width=&quot;588&quot; height=&quot;297&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图1&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;图1展示了一种可能的索引方式。左边是数据表，一共有两列七条记录，最左边的是数据记录的物理地址（注意逻辑上相邻的记录在磁盘上也并不是一定物理相邻的）。为了加快Col2的查找，可以维护一个右边所示的二叉查找树，每个节点分别包含索引键值和一个指向对应数据记录物理地址的指针，这样就可以运用二叉查找在O(log&lt;sub&gt;2&lt;/sub&gt;n)的复杂度内获取到相应数据。&lt;/p&gt;
&lt;p&gt;虽然这是一个货真价实的索引，但是实际的数据库系统几乎没有使用二叉查找树或其进化品种&lt;a href=&quot;http://en.wikipedia.org/wiki/Red-black_tree&quot;&gt;红黑树&lt;/a&gt;（red-black tree）实现的，原因会在下文介绍。&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;nav-2-2&quot;&gt;&lt;/a&gt;B-Tree和B+Tree&lt;/h4&gt;
&lt;p&gt;目前大部分数据库系统及文件系统都采用B-Tree或其变种B+Tree作为索引结构，在本文的下一节会结合存储器原理及计算机存取原理讨论为什么B-Tree和B+Tree在被如此广泛用于索引，这一节先单纯从数据结构角度描述它们。&lt;/p&gt;
&lt;h5&gt;B-Tree&lt;/h5&gt;
&lt;p&gt;为了描述B-Tree，首先定义一条数据记录为一个二元组[key, data]，key为记录的键值，对于不同数据记录，key是互不相同的；data为数据记录除key外的数据。那么B-Tree是满足下列条件的数据结构：&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;d为大于1的一个正整数，称为B-Tree的度。 &lt;/li&gt;
&lt;li&gt;h为一个正整数，称为B-Tree的高度。 &lt;/li&gt;
&lt;li&gt;每个非叶子节点由n-1个key和n个指针组成，其中d&amp;lt;=n&amp;lt;=2d。 &lt;/li&gt;
&lt;li&gt;每个叶子节点最少包含一个key和两个指针，最多包含2d-1个key和2d个指针，叶节点的指针均为null 。 &lt;/li&gt;
&lt;li&gt;所有叶节点具有相同的深度，等于树高h。 &lt;/li&gt;
&lt;li&gt;key和指针互相间隔，节点两端是指针。 &lt;/li&gt;
&lt;li&gt;一个节点中的key从左到右非递减排列。 &lt;/li&gt;
&lt;li&gt;所有节点组成树结构。 &lt;/li&gt;
&lt;li&gt;每个指针要么为null，要么指向另外一个节点。 &lt;/li&gt;
&lt;li&gt;如果某个指针在节点node最左边且不为null，则其指向节点的所有key小于v(key&lt;sub&gt;1&lt;/sub&gt;)，其中v(key&lt;sub&gt;1&lt;/sub&gt;)为node的第一个key的值。 &lt;/li&gt;
&lt;li&gt;如果某个指针在节点node最右边且不为null，则其指向节点的所有key大于v(key&lt;sub&gt;m&lt;/sub&gt;)，其中v(key&lt;sub&gt;m&lt;/sub&gt;)为node的最后一个key的值。 &lt;/li&gt;
&lt;li&gt;如果某个指针在节点node的左右相邻key分别是key&lt;sub&gt;i&lt;/sub&gt;和key&lt;sub&gt;i+1&lt;/sub&gt;且不为null，则其指向节点的所有key小于v(key&lt;sub&gt;i+1&lt;/sub&gt;)且大于v(key&lt;sub&gt;i&lt;/sub&gt;)。&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;图2是一个d=2的B-Tree示意图。&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image281.png&quot; width=&quot;458&quot; height=&quot;115&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图2&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;由于B-Tree的特性，在B-Tree中按key检索数据的算法非常直观：首先从根节点进行二分查找，如果找到则返回对应节点的data，否则对相应区间的指针指向的节点递归进行查找，直到找到节点或找到null指针，前者查找成功，后者查找失败。B-Tree上查找算法的伪代码如下：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;p&gt;13&lt;/p&gt;
&lt;p&gt;14&lt;/p&gt;
&lt;p&gt;&lt;code&gt;BTree_Search(node, key)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;{&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;if&lt;/code&gt;&lt;code&gt;(node == null) &lt;/code&gt;&lt;code&gt;return&lt;/code&gt; &lt;code&gt;null;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;foreach(node.key)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;{&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;if&lt;/code&gt;&lt;code&gt;(node.key[i] == key) &lt;/code&gt;&lt;code&gt;return&lt;/code&gt; &lt;code&gt;node.data[i];&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;if&lt;/code&gt;&lt;code&gt;(node.key[i] &amp;gt; key) &lt;/code&gt;&lt;code&gt;return&lt;/code&gt; &lt;code&gt;BTree_Search(point[i]-&amp;gt;node);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;return&lt;/code&gt; &lt;code&gt;BTree_Search(point[i+1]-&amp;gt;node);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;data = BTree_Search(root, my_key);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;关于B-Tree有一系列有趣的性质，例如&lt;strong&gt;一个度为d的B-Tree，设其索引N个key，则其树高h的上限为log&lt;sub&gt;d&lt;/sub&gt;((N+1)/2)，检索一个key，其查找节点个数的渐进复杂度为O(log&lt;sub&gt;d&lt;/sub&gt;N)。&lt;/strong&gt;从这点可以看出，B-Tree是一个非常有效率的索引数据结构。&lt;/p&gt;
&lt;p&gt;另外，由于插入删除新的数据记录会破坏B-Tree的性质，因此在插入删除时，需要对树进行一个分裂、合并、转移等操作以保持B-Tree性质，本文不打算完整讨论B-Tree这些内容，因为已经有许多资料详细说明了B-Tree的数学性质及插入删除算法，有兴趣的朋友可以在本文末的参考文献一栏找到相应的资料进行阅读。&lt;/p&gt;
&lt;h5&gt;B+Tree&lt;/h5&gt;
&lt;p&gt;B-Tree有许多变种，其中最常见的是B+Tree，例如MySQL就普遍使用B+Tree实现其索引结构。&lt;/p&gt;
&lt;p&gt;与B-Tree相比，B+Tree有以下不同点：&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;每个节点的指针上限为2d而不是2d+1。 &lt;/li&gt;
&lt;li&gt;内节点不存储data，只存储key；叶子节点不存储指针。&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;图3是一个简单的B+Tree示意。&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/11/image4.png&quot; width=&quot;543&quot; height=&quot;191&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图3&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;由于并不是所有节点都具有相同的域，因此B+Tree中叶节点和内节点一般大小不同。这点与B-Tree不同，虽然B-Tree中不同节点存放的key和指针可能数量不一致，但是每个节点的域和上限是一致的，所以在实现中B-Tree往往对每个节点申请同等大小的空间。&lt;/p&gt;
&lt;p&gt;一般来说，B+Tree比B-Tree更适合实现外存储索引结构，具体原因与外存储器原理及计算机存取原理有关，将在下面讨论。&lt;/p&gt;
&lt;h5&gt;带有顺序访问指针的B+Tree&lt;/h5&gt;
&lt;p&gt;一般在数据库系统或文件系统中使用的B+Tree结构都在经典B+Tree的基础上进行了优化，增加了顺序访问指针。&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image381.png&quot; width=&quot;543&quot; height=&quot;191&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图4&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;如图4所示，在B+Tree的每个叶子节点增加一个指向相邻叶子节点的指针，就形成了带有顺序访问指针的B+Tree。做这个优化的目的是为了提高区间访问的性能，例如图4中如果要查询key为从18到49的所有数据记录，当找到18后，只需顺着节点和指针顺序遍历就可以一次性访问到所有数据节点，极大提到了区间查询效率。&lt;/p&gt;
&lt;p&gt;这一节对B-Tree和B+Tree进行了一个简单的介绍，下一节结合存储器存取原理介绍为什么目前B+Tree是数据库系统实现索引的首选数据结构。&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;nav-2-3&quot;&gt;&lt;/a&gt;为什么使用B-Tree（B+Tree）&lt;/h4&gt;
&lt;p&gt;上文说过，红黑树等数据结构也可以用来实现索引，但是文件系统及数据库系统普遍采用B-/+Tree作为索引结构，这一节将结合计算机组成原理相关知识讨论B-/+Tree作为索引的理论基础。&lt;/p&gt;
&lt;p&gt;一般来说，索引本身也很大，不可能全部存储在内存中，因此索引往往以索引文件的形式存储的磁盘上。这样的话，索引查找过程中就要产生磁盘I/O消耗，相对于内存存取，I/O存取的消耗要高几个数量级，所以评价一个数据结构作为索引的优劣最重要的指标就是在查找过程中磁盘I/O操作次数的渐进复杂度。换句话说，索引的结构组织要尽量减少查找过程中磁盘I/O的存取次数。下面先介绍内存和磁盘存取原理，然后再结合这些原理分析B-/+Tree作为索引的效率。&lt;/p&gt;
&lt;h5&gt;主存存取原理&lt;/h5&gt;
&lt;p&gt;目前计算机使用的主存基本都是随机读写存储器（RAM），现代RAM的结构和存取原理比较复杂，这里本文抛却具体差别，抽象出一个十分简单的存取模型来说明RAM的工作原理。&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image4.png&quot; class=&quot;highslide-image&quot; onclick=&quot;return hs.expand(this);&quot;&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image4_thumb.png&quot; width=&quot;382&quot; height=&quot;229&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图5&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;从抽象角度看，主存是一系列的存储单元组成的矩阵，每个存储单元存储固定大小的数据。每个存储单元有唯一的地址，现代主存的编址规则比较复杂，这里将其简化成一个二维地址：通过一个行地址和一个列地址可以唯一定位到一个存储单元。图5展示了一个4 x 4的主存模型。&lt;/p&gt;
&lt;p&gt;主存的存取过程如下：&lt;/p&gt;
&lt;p&gt;当系统需要读取主存时，则将地址信号放到地址总线上传给主存，主存读到地址信号后，解析信号并定位到指定存储单元，然后将此存储单元数据放到数据总线上，供其它部件读取。&lt;/p&gt;
&lt;p&gt;写主存的过程类似，系统将要写入单元地址和数据分别放在地址总线和数据总线上，主存读取两个总线的内容，做相应的写操作。&lt;/p&gt;
&lt;p&gt;这里可以看出，主存存取的时间仅与存取次数呈线性关系，因为不存在机械操作，两次存取的数据的“距离”不会对时间有任何影响，例如，先取A0再取A1和先取A0再取D3的时间消耗是一样的。&lt;/p&gt;
&lt;h5&gt;磁盘存取原理&lt;/h5&gt;
&lt;p&gt;上文说过，索引一般以文件形式存储在磁盘上，索引检索需要磁盘I/O操作。与主存不同，磁盘I/O存在机械运动耗费，因此磁盘I/O的时间消耗是巨大的。&lt;/p&gt;
&lt;p&gt;图6是磁盘的整体结构示意图。&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image6111.png&quot; width=&quot;267&quot; height=&quot;172&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图6&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;一个磁盘由大小相同且同轴的圆形盘片组成，磁盘可以转动（各个磁盘必须同步转动）。在磁盘的一侧有磁头支架，磁头支架固定了一组磁头，每个磁头负责存取一个磁盘的内容。磁头不能转动，但是可以沿磁盘半径方向运动（实际是斜切向运动），每个磁头同一时刻也必须是同轴的，即从正上方向下看，所有磁头任何时候都是重叠的（不过目前已经有多磁头独立技术，可不受此限制）。&lt;/p&gt;
&lt;p&gt;图7是磁盘结构的示意图。&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image1111.png&quot; width=&quot;269&quot; height=&quot;246&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图7&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;盘片被划分成一系列同心环，圆心是盘片中心，每个同心环叫做一个磁道，所有半径相同的磁道组成一个柱面。磁道被沿半径线划分成一个个小的段，每个段叫做一个扇区，每个扇区是磁盘的最小存储单元。为了简单起见，我们下面假设磁盘只有一个盘片和一个磁头。&lt;/p&gt;
&lt;p&gt;当需要从磁盘读取数据时，系统会将数据逻辑地址传给磁盘，磁盘的控制电路按照寻址逻辑将逻辑地址翻译成物理地址，即确定要读的数据在哪个磁道，哪个扇区。为了读取这个扇区的数据，需要将磁头放到这个扇区上方，为了实现这一点，磁头需要移动对准相应磁道，这个过程叫做寻道，所耗费时间叫做寻道时间，然后磁盘旋转将目标扇区旋转到磁头下，这个过程耗费的时间叫做旋转时间。&lt;/p&gt;
&lt;h5&gt;局部性原理与磁盘预读&lt;/h5&gt;
&lt;p&gt;由于存储介质的特性，磁盘本身存取就比主存慢很多，再加上机械运动耗费，磁盘的存取速度往往是主存的几百分分之一，因此为了提高效率，要尽量减少磁盘I/O。为了达到这个目的，磁盘往往不是严格按需读取，而是每次都会预读，即使只需要一个字节，磁盘也会从这个位置开始，顺序向后读取一定长度的数据放入内存。这样做的理论依据是计算机科学中著名的局部性原理：&lt;/p&gt;
&lt;p&gt;当一个数据被用到时，其附近的数据也通常会马上被使用。&lt;/p&gt;
&lt;p&gt;程序运行期间所需要的数据通常比较集中。&lt;/p&gt;
&lt;p&gt;由于磁盘顺序读取的效率很高（不需要寻道时间，只需很少的旋转时间），因此对于具有局部性的程序来说，预读可以提高I/O效率。&lt;/p&gt;
&lt;p&gt;预读的长度一般为页（page）的整倍数。页是计算机管理存储器的逻辑块，硬件及操作系统往往将主存和磁盘存储区分割为连续的大小相等的块，每个存储块称为一页（在许多操作系统中，页得大小通常为4k），主存和磁盘以页为单位交换数据。当程序要读取的数据不在主存中时，会触发一个缺页异常，此时系统会向磁盘发出读盘信号，磁盘会找到数据的起始位置并向后连续读取一页或几页载入内存中，然后异常返回，程序继续运行。&lt;/p&gt;
&lt;h5&gt;B-/+Tree索引的性能分析&lt;/h5&gt;
&lt;p&gt;到这里终于可以分析B-/+Tree索引的性能了。&lt;/p&gt;
&lt;p&gt;上文说过一般使用磁盘I/O次数评价索引结构的优劣。先从B-Tree分析，根据B-Tree的定义，可知检索一次最多需要访问h个节点。数据库系统的设计者巧妙利用了磁盘预读原理，将一个节点的大小设为等于一个页，这样每个节点只需要一次I/O就可以完全载入。为了达到这个目的，在实际实现B-Tree还需要使用如下技巧：&lt;/p&gt;
&lt;p&gt;每次新建节点时，直接申请一个页的空间，这样就保证一个节点物理上也存储在一个页里，加之计算机存储分配都是按页对齐的，就实现了一个node只需一次I/O。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;B-Tree中一次检索最多需要h-1次I/O（根节点常驻内存），渐进复杂度为O(h)=O(log&lt;sub&gt;d&lt;/sub&gt;N)。&lt;/strong&gt;一般实际应用中，出度d是非常大的数字，通常超过100，因此h非常小（通常不超过3）。&lt;/p&gt;
&lt;p&gt;综上所述，用B-Tree作为索引结构效率是非常高的。&lt;/p&gt;
&lt;p&gt;而红黑树这种结构，h明显要深的多。由于逻辑上很近的节点（父子）物理上可能很远，无法利用局部性，所以红黑树的I/O渐进复杂度也为O(h)，效率明显比B-Tree差很多。&lt;/p&gt;
&lt;p&gt;上文还说过，B+Tree更适合外存索引，原因和内节点出度d有关。从上面分析可以看到，d越大索引的性能越好，而出度的上限取决于节点内key和data的大小：&lt;/p&gt;
&lt;p&gt;d&lt;sub&gt;max&lt;/sub&gt; = &lt;em&gt;&lt;strong&gt;floor&lt;/strong&gt;&lt;/em&gt;(pagesize / (keysize + datasize + pointsize)) &lt;em&gt;(pagesize – dmax &amp;gt;= pointsize)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;或&lt;/p&gt;
&lt;p&gt;d&lt;sub&gt;max&lt;/sub&gt; = &lt;em&gt;&lt;strong&gt;floor&lt;/strong&gt;&lt;/em&gt;(pagesize / (keysize + datasize + pointsize)) – 1 &lt;em&gt;(pagesize – dmax &amp;lt; pointsize)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;floor表示向下取整。由于B+Tree内节点去掉了data域，因此可以拥有更大的出度，拥有更好的性能。&lt;/p&gt;
&lt;p&gt;这一章从理论角度讨论了与索引相关的数据结构与算法问题，下一章将讨论B+Tree是如何具体实现为MySQL中索引，同时将结合MyISAM和InnDB存储引擎介绍非聚集索引和聚集索引两种不同的索引实现形式。&lt;/p&gt;
&lt;h3&gt;&lt;a name=&quot;nav-3&quot;&gt;&lt;/a&gt;MySQL索引实现&lt;/h3&gt;
&lt;p&gt;在MySQL中，索引属于存储引擎级别的概念，不同存储引擎对索引的实现方式是不同的，本文主要讨论MyISAM和InnoDB两个存储引擎的索引实现方式。&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;nav-3-1&quot;&gt;&lt;/a&gt;MyISAM索引实现&lt;/h4&gt;
&lt;p&gt;MyISAM引擎使用B+Tree作为索引结构，叶节点的data域存放的是数据记录的地址。下图是MyISAM索引的原理图：&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image11111.png&quot; width=&quot;664&quot; height=&quot;534&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图8&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;这里设表一共有三列，假设我们以Col1为主键，则图8是一个MyISAM表的主索引（Primary key）示意。可以看出MyISAM的索引文件仅仅保存数据记录的地址。&lt;strong&gt;在MyISAM中，主索引和辅助索引（Secondary key）在结构上没有任何区别，只是主索引要求key是唯一的，而辅助索引的key可以重复。&lt;/strong&gt;如果我们在Col2上建立一个辅助索引，则此索引的结构如下图所示：&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image61.png&quot; width=&quot;664&quot; height=&quot;534&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图9&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;同样也是一颗B+Tree，data域保存数据记录的地址。因此，&lt;strong&gt;MyISAM中索引检索的算法为首先按照B+Tree搜索算法搜索索引，如果指定的Key存在，则取出其data域的值，然后以data域的值为地址，读取相应数据记录。&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;MyISAM的索引方式也叫做“非聚集”的，之所以这么称呼是为了与InnoDB的聚集索引区分。&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;nav-3-2&quot;&gt;&lt;/a&gt;InnoDB索引实现&lt;/h4&gt;
&lt;p&gt;虽然InnoDB也使用B+Tree作为索引结构，但具体实现方式却与MyISAM截然不同。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;第一个重大区别是InnoDB的数据文件本身就是索引文件。&lt;/strong&gt;从上文知道，MyISAM索引文件和数据文件是分离的，索引文件仅保存数据记录的地址。而在InnoDB中，表数据文件本身就是按B+Tree组织的一个索引结构，这棵树的叶节点data域保存了完整的数据记录。这个索引的key是数据表的主键，因此InnoDB表数据文件本身就是主索引。&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image111.png&quot; width=&quot;543&quot; height=&quot;241&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图10&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;图10是InnoDB主索引（同时也是数据文件）的示意图，可以看到叶节点包含了完整的数据记录。这种索引叫做聚集索引。因为InnoDB的数据文件本身要按主键聚集，所以InnoDB要求表必须有主键（MyISAM可以没有），如果没有显式指定，则MySQL系统会自动选择一个可以唯一标识数据记录的列作为主键，如果不存在这种列，则MySQL自动为InnoDB表生成一个隐含字段作为主键，这个字段长度为6个字节，类型为长整形。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;第二个与MyISAM索引的不同是InnoDB的辅助索引data域存储相应记录主键的值而不是地址。&lt;/strong&gt;换句话说，InnoDB的所有辅助索引都引用主键作为data域。例如，图11为定义在Col3上的一个辅助索引：&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/11/image5.png&quot; width=&quot;543&quot; height=&quot;222&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图11&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;这里以英文字符的ASCII码作为比较准则。&lt;strong&gt;聚集索引这种实现方式使得按主键的搜索十分高效，但是辅助索引搜索需要检索两遍索引：首先检索辅助索引获得主键，然后用主键到主索引中检索获得记录。&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;了解不同存储引擎的索引实现方式对于正确使用和优化索引都非常有帮助，例如知道了InnoDB的索引实现后，就很容易明白为什么不建议使用过长的字段作为主键，因为所有辅助索引都引用主索引，过长的主索引会令辅助索引变得过大。再例如，用非单调的字段作为主键在InnoDB中不是个好主意，因为InnoDB数据文件本身是一颗B+Tree，非单调的主键会造成在插入新记录时数据文件为了维持B+Tree的特性而频繁的分裂调整，十分低效，而使用自增字段作为主键则是一个很好的选择。&lt;/p&gt;
&lt;p&gt;下一章将具体讨论这些与索引有关的优化策略。&lt;/p&gt;
&lt;h3&gt;&lt;a name=&quot;nav-4&quot;&gt;&lt;/a&gt;索引使用策略及优化&lt;/h3&gt;
&lt;p&gt;MySQL的优化主要分为结构优化（Scheme optimization）和查询优化（Query optimization）。本章讨论的高性能索引策略主要属于结构优化范畴。本章的内容完全基于上文的理论基础，实际上一旦理解了索引背后的机制，那么选择高性能的策略就变成了纯粹的推理，并且可以理解这些策略背后的逻辑。&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;nav-4-1&quot;&gt;&lt;/a&gt;示例数据库&lt;/h4&gt;
&lt;p&gt;为了讨论索引策略，需要一个数据量不算小的数据库作为示例。本文选用MySQL官方文档中提供的示例数据库之一：employees。这个数据库关系复杂度适中，且数据量较大。下图是这个数据库的E-R关系图（引用自MySQL官方手册）：&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image11.png&quot; width=&quot;666&quot; height=&quot;524&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图12&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;MySQL官方文档中关于此数据库的页面为&lt;a href=&quot;http://dev.mysql.com/doc/employee/en/employee.html&quot;&gt;http://dev.mysql.com/doc/employee/en/employee.html&lt;/a&gt;。里面详细介绍了此数据库，并提供了下载地址和导入方法，如果有兴趣导入此数据库到自己的MySQL可以参考文中内容。&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;nav-4-2&quot;&gt;&lt;/a&gt;最左前缀原理与相关优化&lt;/h4&gt;
&lt;p&gt;高效使用索引的首要条件是知道什么样的查询会使用到索引，这个问题和B+Tree中的“最左前缀原理”有关，下面通过例子说明最左前缀原理。&lt;/p&gt;
&lt;p&gt;这里先说一下联合索引的概念。在上文中，我们都是假设索引只引用了单个的列，实际上，MySQL中的索引可以以一定顺序引用多个列，这种索引叫做联合索引，一般的，一个联合索引是一个有序元组&amp;lt;a1, a2, …, an&amp;gt;，其中各个元素均为数据表的一列，实际上要严格定义索引需要用到关系代数，但是这里我不想讨论太多关系代数的话题，因为那样会显得很枯燥，所以这里就不再做严格定义。另外，单列索引可以看成联合索引元素数为1的特例。&lt;/p&gt;
&lt;p&gt;以employees.titles表为例，下面先查看其上都有哪些索引：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SHOW &lt;/code&gt;&lt;code&gt;INDEX&lt;/code&gt; &lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;--------+------------+----------+--------------+-------------+-----------+-------------+------+------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| &lt;/code&gt;&lt;code&gt;Table&lt;/code&gt; &lt;code&gt;| Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | &lt;/code&gt;&lt;code&gt;Null&lt;/code&gt; &lt;code&gt;| Index_type |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;--------+------------+----------+--------------+-------------+-----------+-------------+------+------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| titles | 0 | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 1 | emp_no | A | &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| | BTREE |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| titles | 0 | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 2 | title | A | &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| | BTREE |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| titles | 0 | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 3 | from_date | A | 443308 | | BTREE |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| titles | 1 | emp_no | 1 | emp_no | A | 443308 | | BTREE |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;--------+------------+----------+--------------+-------------+-----------+-------------+------+------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;从结果中可以到titles表的主索引为&amp;lt;emp_no, title, from_date&amp;gt;，还有一个辅助索引&amp;lt;emp_no&amp;gt;。为了避免多个索引使事情变复杂（MySQL的SQL优化器在多索引时行为比较复杂），这里我们将辅助索引drop掉：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ALTER&lt;/code&gt; &lt;code&gt;TABLE&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;DROP&lt;/code&gt; &lt;code&gt;INDEX&lt;/code&gt; &lt;code&gt;emp_no;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;这样就可以专心分析索引PRIMARY的行为了。&lt;/p&gt;
&lt;h5&gt;情况一：全列匹配。&lt;/h5&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no=&lt;/code&gt;&lt;code&gt;'10001'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;title=&lt;/code&gt;&lt;code&gt;'Senior Engineer'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;from_date=&lt;/code&gt;&lt;code&gt;'1986-06-26'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | const | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 59 | const,const,const | 1 | |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;很明显，当按照索引中所有列进行精确匹配（这里精确匹配指“=”或“IN”匹配）时，索引可以被用到。这里有一点需要注意，理论上索引对顺序是敏感的，但是由于MySQL的查询优化器会自动调整where子句的条件顺序以使用适合的索引，例如我们将where中的条件顺序颠倒：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;from_date=&lt;/code&gt;&lt;code&gt;'1986-06-26'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;emp_no=&lt;/code&gt;&lt;code&gt;'10001'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;title=&lt;/code&gt;&lt;code&gt;'Senior Engineer'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | const | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 59 | const,const,const | 1 | |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;效果是一样的。&lt;/p&gt;
&lt;h5&gt;情况二：最左前缀匹配。&lt;/h5&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no=&lt;/code&gt;&lt;code&gt;'10001'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+---------+---------+-------+------+-------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+---------+---------+-------+------+-------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | ref | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 4 | const | 1 | |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+---------+---------+-------+------+-------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;当查询条件精确匹配索引的左边连续一个或几个列时，如&amp;lt;emp_no&amp;gt;或&amp;lt;emp_no, title&amp;gt;，所以可以被用到，但是只能用到一部分，即条件所组成的最左前缀。上面的查询从分析结果看用到了PRIMARY索引，但是key_len为4，说明只用到了索引的第一列前缀。&lt;/p&gt;
&lt;h5&gt;情况三：查询条件用到了索引中列的精确匹配，但是中间某个条件未提供。&lt;/h5&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no=&lt;/code&gt;&lt;code&gt;'10001'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;from_date=&lt;/code&gt;&lt;code&gt;'1986-06-26'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | ref | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 4 | const | 1 | Using &lt;/code&gt;&lt;code&gt;where&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;此时索引使用情况和情况二相同，因为title未提供，所以查询只用到了索引的第一列，而后面的from_date虽然也在索引中，但是由于title不存在而无法和左前缀连接，因此需要对结果进行扫描过滤from_date（这里由于emp_no唯一，所以不存在扫描）。如果想让from_date也使用索引而不是where过滤，可以增加一个辅助索引&amp;lt;emp_no, from_date&amp;gt;，此时上面的查询会使用这个索引。除此之外，还可以使用一种称之为“隔离列”的优化方法，将emp_no与from_date之间的“坑”填上。&lt;/p&gt;
&lt;p&gt;首先我们看下title一共有几种不同的值：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;DISTINCT&lt;/code&gt;&lt;code&gt;(title) &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;--------------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| title |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;--------------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Senior Engineer |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Staff |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Engineer |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Senior Staff |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Assistant Engineer |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Technique Leader |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Manager |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;--------------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;只有7种。在这种成为“坑”的列值比较少的情况下，可以考虑用“IN”来填补这个“坑”从而形成最左前缀：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no=&lt;/code&gt;&lt;code&gt;'10001'&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;AND&lt;/code&gt; &lt;code&gt;title &lt;/code&gt;&lt;code&gt;IN&lt;/code&gt; &lt;code&gt;(&lt;/code&gt;&lt;code&gt;'Senior Engineer'&lt;/code&gt;&lt;code&gt;, &lt;/code&gt;&lt;code&gt;'Staff'&lt;/code&gt;&lt;code&gt;, &lt;/code&gt;&lt;code&gt;'Engineer'&lt;/code&gt;&lt;code&gt;, &lt;/code&gt;&lt;code&gt;'Senior Staff'&lt;/code&gt;&lt;code&gt;, &lt;/code&gt;&lt;code&gt;'Assistant Engineer'&lt;/code&gt;&lt;code&gt;, &lt;/code&gt;&lt;code&gt;'Technique Leader'&lt;/code&gt;&lt;code&gt;, &lt;/code&gt;&lt;code&gt;'Manager'&lt;/code&gt;&lt;code&gt;)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;AND&lt;/code&gt; &lt;code&gt;from_date=&lt;/code&gt;&lt;code&gt;'1986-06-26'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | range | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 59 | &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| 7 | Using &lt;/code&gt;&lt;code&gt;where&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;这次key_len为59，说明索引被用全了，但是从type和rows看出IN实际上执行了一个range查询，这里检查了7个key。看下两种查询的性能比较：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SHOW PROFILES;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----------+------------+-------------------------------------------------------------------------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Query_ID | Duration | Query |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----------+------------+-------------------------------------------------------------------------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 10 | 0.00058000 | &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no=&lt;/code&gt;&lt;code&gt;'10001'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;from_date=&lt;/code&gt;&lt;code&gt;'1986-06-26'&lt;/code&gt;&lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 11 | 0.00052500 | &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no=&lt;/code&gt;&lt;code&gt;'10001'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;title &lt;/code&gt;&lt;code&gt;IN&lt;/code&gt; &lt;code&gt;... |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----------+------------+-------------------------------------------------------------------------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;“填坑”后性能提升了一点。如果经过emp_no筛选后余下很多数据，则后者性能优势会更加明显。当然，如果title的值很多，用填坑就不合适了，必须建立辅助索引。&lt;/p&gt;
&lt;h5&gt;情况四：查询条件没有指定索引第一列。&lt;/h5&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;from_date=&lt;/code&gt;&lt;code&gt;'1986-06-26'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+------+---------+------+--------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+------+---------+------+--------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | &lt;/code&gt;&lt;code&gt;ALL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| 443308 | Using &lt;/code&gt;&lt;code&gt;where&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+------+---------+------+--------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;由于不是最左前缀，索引这样的查询显然用不到索引。&lt;/p&gt;
&lt;h5&gt;情况五：匹配某列的前缀字符串。&lt;/h5&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no=&lt;/code&gt;&lt;code&gt;'10001'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;title &lt;/code&gt;&lt;code&gt;LIKE&lt;/code&gt; &lt;code&gt;'Senior%'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | range | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 56 | &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| 1 | Using &lt;/code&gt;&lt;code&gt;where&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;此时可以用到索引，但是如果通配符不是只出现在末尾，则无法使用索引。（原文表述有误，如果通配符%不出现在开头，则可以用到索引，但根据具体情况不同可能只会用其中一个前缀）&lt;/p&gt;
&lt;h5&gt;情况六：范围查询。&lt;/h5&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no &amp;lt; &lt;/code&gt;&lt;code&gt;'10010'&lt;/code&gt; &lt;code&gt;and&lt;/code&gt; &lt;code&gt;title=&lt;/code&gt;&lt;code&gt;'Senior Engineer'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | range | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 4 | &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| 16 | Using &lt;/code&gt;&lt;code&gt;where&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;范围列可以用到索引（必须是最左前缀），但是范围列后面的列无法用到索引。同时，索引最多用于一个范围列，因此如果查询条件中有两个范围列则无法全用到索引。&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no &amp;lt; 10010&lt;/code&gt;&lt;code&gt;'&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;AND title='&lt;/code&gt;&lt;code&gt;Senior Engineer&lt;/code&gt;&lt;code&gt;'&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;AND from_date BETWEEN '&lt;/code&gt;&lt;code&gt;1986-01-01&lt;/code&gt;&lt;code&gt;' AND '&lt;/code&gt;&lt;code&gt;1986-12-31';&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | range | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 4 | &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| 16 | Using &lt;/code&gt;&lt;code&gt;where&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;可以看到索引对第二个范围索引无能为力。这里特别要说明MySQL一个有意思的地方，那就是仅用explain可能无法区分范围索引和多值匹配，因为在type中这两者都显示为range。同时，用了“between”并不意味着就是范围查询，例如下面的查询：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no &lt;/code&gt;&lt;code&gt;BETWEEN&lt;/code&gt; &lt;code&gt;'10001'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;'10010'&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;AND&lt;/code&gt; &lt;code&gt;title=&lt;/code&gt;&lt;code&gt;'Senior Engineer'&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;AND&lt;/code&gt; &lt;code&gt;from_date &lt;/code&gt;&lt;code&gt;BETWEEN&lt;/code&gt; &lt;code&gt;'1986-01-01'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;'1986-12-31'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | range | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 59 | &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| 16 | Using &lt;/code&gt;&lt;code&gt;where&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;看起来是用了两个范围查询，但作用于emp_no上的“BETWEEN”实际上相当于“IN”，也就是说emp_no实际是多值精确匹配。可以看到这个查询用到了索引全部三个列。因此在MySQL中要谨慎地区分多值匹配和范围匹配，否则会对MySQL的行为产生困惑。&lt;/p&gt;
&lt;h5&gt;情况七：查询条件中含有函数或表达式。&lt;/h5&gt;
&lt;p&gt;很不幸，如果查询条件中含有函数或表达式，则MySQL不会为这列使用索引（虽然某些在数学意义上可以使用）。例如：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no=&lt;/code&gt;&lt;code&gt;'10001'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;left&lt;/code&gt;&lt;code&gt;(title, 6)=&lt;/code&gt;&lt;code&gt;'Senior'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | ref | &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;PRIMARY&lt;/code&gt; &lt;code&gt;| 4 | const | 1 | Using &lt;/code&gt;&lt;code&gt;where&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+---------+---------+-------+------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;虽然这个查询和情况五中功能相同，但是由于使用了函数left，则无法为title列应用索引，而情况五中用LIKE则可以。再如：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;emp_no - 1=&lt;/code&gt;&lt;code&gt;'10000'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+------+---------+------+--------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+------+---------+------+--------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | titles | &lt;/code&gt;&lt;code&gt;ALL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| 443308 | Using &lt;/code&gt;&lt;code&gt;where&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+--------+------+---------------+------+---------+------+--------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;显然这个查询等价于查询emp_no为10001的函数，但是由于查询条件是一个表达式，MySQL无法为其使用索引。看来MySQL还没有智能到自动优化常量表达式的程度，因此在写查询语句时尽量避免表达式出现在查询中，而是先手工私下代数运算，转换为无表达式的查询语句。&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;nav-4-3&quot;&gt;&lt;/a&gt;索引选择性与前缀索引&lt;/h4&gt;
&lt;p&gt;既然索引可以加快查询速度，那么是不是只要是查询语句需要，就建上索引？答案是否定的。因为索引虽然加快了查询速度，但索引也是有代价的：索引文件本身要消耗存储空间，同时索引会加重插入、删除和修改记录时的负担，另外，MySQL在运行时也要消耗资源维护索引，因此索引并不是越多越好。一般两种情况下不建议建索引。&lt;/p&gt;
&lt;p&gt;第一种情况是表记录比较少，例如一两千条甚至只有几百条记录的表，没必要建索引，让查询做全表扫描就好了。至于多少条记录才算多，这个个人有个人的看法，我个人的经验是以2000作为分界线，记录数不超过 2000可以考虑不建索引，超过2000条可以酌情考虑索引。&lt;/p&gt;
&lt;p&gt;另一种不建议建索引的情况是索引的选择性较低。所谓索引的选择性（Selectivity），是指不重复的索引值（也叫基数，Cardinality）与表记录数（#T）的比值：&lt;/p&gt;
&lt;p&gt;Index Selectivity = Cardinality / #T&lt;/p&gt;
&lt;p&gt;显然选择性的取值范围为(0, 1]，选择性越高的索引价值越大，这是由B+Tree的性质决定的。例如，上文用到的employees.titles表，如果title字段经常被单独查询，是否需要建索引，我们看一下它的选择性：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;count&lt;/code&gt;&lt;code&gt;(&lt;/code&gt;&lt;code&gt;DISTINCT&lt;/code&gt;&lt;code&gt;(title))/&lt;/code&gt;&lt;code&gt;count&lt;/code&gt;&lt;code&gt;(*) &lt;/code&gt;&lt;code&gt;AS&lt;/code&gt; &lt;code&gt;Selectivity &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.titles;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Selectivity |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 0.0000 |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;title的选择性不足0.0001（精确值为0.00001579），所以实在没有什么必要为其单独建索引。&lt;/p&gt;
&lt;p&gt;有一种与索引选择性有关的索引优化策略叫做前缀索引，就是用列的前缀代替整个列作为索引key，当前缀长度合适时，可以做到既使得前缀索引的选择性接近全列索引，同时因为索引key变短而减少了索引文件的大小和维护开销。下面以employees.employees表为例介绍前缀索引的选择和使用。&lt;/p&gt;
&lt;p&gt;从图12可以看到employees表只有一个索引&amp;lt;emp_no&amp;gt;，那么如果我们想按名字搜索一个人，就只能全表扫描了：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.employees &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;first_name=&lt;/code&gt;&lt;code&gt;'Eric'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;last_name=&lt;/code&gt;&lt;code&gt;'Anido'&lt;/code&gt;&lt;code&gt;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| id | select_type | &lt;/code&gt;&lt;code&gt;table&lt;/code&gt; &lt;code&gt;| type | possible_keys | &lt;/code&gt;&lt;code&gt;key&lt;/code&gt; &lt;code&gt;| key_len | ref | &lt;/code&gt;&lt;code&gt;rows&lt;/code&gt; &lt;code&gt;| Extra |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 1 | SIMPLE | employees | &lt;/code&gt;&lt;code&gt;ALL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| &lt;/code&gt;&lt;code&gt;NULL&lt;/code&gt; &lt;code&gt;| 300024 | Using &lt;/code&gt;&lt;code&gt;where&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;如果频繁按名字搜索员工，这样显然效率很低，因此我们可以考虑建索引。有两种选择，建&amp;lt;first_name&amp;gt;或&amp;lt;first_name, last_name&amp;gt;，看下两个索引的选择性：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;p&gt;8&lt;/p&gt;
&lt;p&gt;9&lt;/p&gt;
&lt;p&gt;10&lt;/p&gt;
&lt;p&gt;11&lt;/p&gt;
&lt;p&gt;12&lt;/p&gt;
&lt;p&gt;13&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;count&lt;/code&gt;&lt;code&gt;(&lt;/code&gt;&lt;code&gt;DISTINCT&lt;/code&gt;&lt;code&gt;(first_name))/&lt;/code&gt;&lt;code&gt;count&lt;/code&gt;&lt;code&gt;(*) &lt;/code&gt;&lt;code&gt;AS&lt;/code&gt; &lt;code&gt;Selectivity &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.employees;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Selectivity |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 0.0042 |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;count&lt;/code&gt;&lt;code&gt;(&lt;/code&gt;&lt;code&gt;DISTINCT&lt;/code&gt;&lt;code&gt;(concat(first_name, last_name)))/&lt;/code&gt;&lt;code&gt;count&lt;/code&gt;&lt;code&gt;(*) &lt;/code&gt;&lt;code&gt;AS&lt;/code&gt; &lt;code&gt;Selectivity &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.employees;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Selectivity |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 0.9313 |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;first_name&amp;gt;显然选择性太低，&amp;lt;first_name, last_name&amp;gt;选择性很好，但是first_name和last_name加起来长度为30，有没有兼顾长度和选择性的办法？可以考虑用first_name和last_name的前几个字符建立索引，例如&amp;lt;first_name, left(last_name, 3)&amp;gt;，看看其选择性：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;count&lt;/code&gt;&lt;code&gt;(&lt;/code&gt;&lt;code&gt;DISTINCT&lt;/code&gt;&lt;code&gt;(concat(first_name, &lt;/code&gt;&lt;code&gt;left&lt;/code&gt;&lt;code&gt;(last_name, 3))))/&lt;/code&gt;&lt;code&gt;count&lt;/code&gt;&lt;code&gt;(*) &lt;/code&gt;&lt;code&gt;AS&lt;/code&gt; &lt;code&gt;Selectivity &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.employees;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Selectivity |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 0.7879 |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;选择性还不错，但离0.9313还是有点距离，那么把last_name前缀加到4：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;count&lt;/code&gt;&lt;code&gt;(&lt;/code&gt;&lt;code&gt;DISTINCT&lt;/code&gt;&lt;code&gt;(concat(first_name, &lt;/code&gt;&lt;code&gt;left&lt;/code&gt;&lt;code&gt;(last_name, 4))))/&lt;/code&gt;&lt;code&gt;count&lt;/code&gt;&lt;code&gt;(*) &lt;/code&gt;&lt;code&gt;AS&lt;/code&gt; &lt;code&gt;Selectivity &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.employees;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Selectivity |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 0.9007 |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;-------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;这时选择性已经很理想了，而这个索引的长度只有18，比&amp;lt;first_name, last_name&amp;gt;短了接近一半，我们把这个前缀索引 建上：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ALTER&lt;/code&gt; &lt;code&gt;TABLE&lt;/code&gt; &lt;code&gt;employees.employees&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ADD&lt;/code&gt; &lt;code&gt;INDEX&lt;/code&gt; &lt;code&gt;`first_name_last_name4` (first_name, last_name(4));&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;此时再执行一遍按名字查询，比较分析一下与建索引前的结果：&lt;/p&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SHOW PROFILES;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----------+------------+---------------------------------------------------------------------------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| Query_ID | Duration | Query |&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----------+------------+---------------------------------------------------------------------------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 87 | 0.11941700 | &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.employees &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;first_name=&lt;/code&gt;&lt;code&gt;'Eric'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;last_name=&lt;/code&gt;&lt;code&gt;'Anido'&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;| 90 | 0.00092400 | &lt;/code&gt;&lt;code&gt;SELECT&lt;/code&gt; &lt;code&gt;* &lt;/code&gt;&lt;code&gt;FROM&lt;/code&gt; &lt;code&gt;employees.employees &lt;/code&gt;&lt;code&gt;WHERE&lt;/code&gt; &lt;code&gt;first_name=&lt;/code&gt;&lt;code&gt;'Eric'&lt;/code&gt; &lt;code&gt;AND&lt;/code&gt; &lt;code&gt;last_name=&lt;/code&gt;&lt;code&gt;'Anido'&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;+&lt;/code&gt;&lt;code&gt;----------+------------+---------------------------------------------------------------------------------+&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;性能的提升是显著的，查询速度提高了120多倍。&lt;/p&gt;
&lt;p&gt;前缀索引兼顾索引大小和查询速度，但是其缺点是不能用于ORDER BY和GROUP BY操作，也不能用于Covering index（即当索引本身包含查询所需全部数据时，不再访问数据文件本身）。&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;nav-4-4&quot;&gt;&lt;/a&gt;InnoDB的主键选择与插入优化&lt;/h4&gt;
&lt;p&gt;在使用InnoDB存储引擎时，如果没有特别的需要，请永远使用一个与业务无关的自增字段作为主键。&lt;/p&gt;
&lt;p&gt;经常看到有帖子或博客讨论主键选择问题，有人建议使用业务无关的自增主键，有人觉得没有必要，完全可以使用如学号或身份证号这种唯一字段作为主键。不论支持哪种论点，大多数论据都是业务层面的。如果从数据库索引优化角度看，使用InnoDB引擎而不使用自增主键绝对是一个糟糕的主意。&lt;/p&gt;
&lt;p&gt;上文讨论过InnoDB的索引实现，InnoDB使用聚集索引，数据记录本身被存于主索引（一颗B+Tree）的叶子节点上。这就要求同一个叶子节点内（大小为一个内存页或磁盘页）的各条数据记录按主键顺序存放，因此每当有一条新的记录插入时，MySQL会根据其主键将其插入适当的节点和位置，如果页面达到装载因子（InnoDB默认为15/16），则开辟一个新的页（节点）。&lt;/p&gt;
&lt;p&gt;如果表使用自增主键，那么每次插入新的记录，记录就会顺序添加到当前索引节点的后续位置，当一页写满，就会自动开辟一个新的页。如下图所示：&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image12.png&quot; width=&quot;437&quot; height=&quot;175&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图13&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;这样就会形成一个紧凑的索引结构，近似顺序填满。由于每次插入时也不需要移动已有数据，因此效率很高，也不会增加很多开销在维护索引上。&lt;/p&gt;
&lt;p&gt;如果使用非自增主键（如果身份证号或学号等），由于每次插入主键的值近似于随机，因此每次新纪录都要被插到现有索引页得中间某个位置：&lt;/p&gt;
&lt;p&gt;&lt;img title=&quot;image&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;http://www.codinglabs.org/wp-content/uploads/2011/10/image6.png&quot; width=&quot;229&quot; height=&quot;175&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;图14&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;此时MySQL不得不为了将新记录插到合适位置而移动数据，甚至目标页面可能已经被回写到磁盘上而从缓存中清掉，此时又要从磁盘上读回来，这增加了很多开销，同时频繁的移动、分页操作造成了大量的碎片，得到了不够紧凑的索引结构，后续不得不通过OPTIMIZE TABLE来重建表并优化填充页面。&lt;/p&gt;
&lt;p&gt;因此，只要可以，请尽量在InnoDB上采用自增字段做主键。&lt;/p&gt;
&lt;h3&gt;&lt;a name=&quot;nav-5&quot;&gt;&lt;/a&gt;后记&lt;/h3&gt;
&lt;p&gt;这篇文章断断续续写了半个月，主要内容就是上面这些了。不可否认，这篇文章在一定程度上有纸上谈兵之嫌，因为我本人对MySQL的使用属于菜鸟级别，更没有太多数据库调优的经验，在这里大谈数据库索引调优有点大言不惭。就当是我个人的一篇学习笔记了。&lt;/p&gt;
&lt;p&gt;其实数据库索引调优是一项技术活，不能仅仅靠理论，因为实际情况千变万化，而且MySQL本身存在很复杂的机制，如查询优化策略和各种引擎的实现差异等都会使情况变得更加复杂。但同时这些理论是索引调优的基础，只有在明白理论的基础上，才能对调优策略进行合理推断并了解其背后的机制，然后结合实践中不断的实验和摸索，从而真正达到高效使用MySQL索引的目的。&lt;/p&gt;
&lt;p&gt;另外，MySQL索引及其优化涵盖范围非常广，本文只是涉及到其中一部分。如与排序（ORDER BY）相关的索引优化及覆盖索引（Covering index）的话题本文并未涉及，同时除B-Tree索引外MySQL还根据不同引擎支持的哈希索引、全文索引等等本文也并未涉及。如果有机会，希望再对本文未涉及的部分进行补充吧。&lt;/p&gt;
&lt;h3&gt;&lt;a name=&quot;nav-6&quot;&gt;&lt;/a&gt;参考文献&lt;/h3&gt;
&lt;p&gt;[1] Baron Scbwartz等 著，王小东等 译；高性能MySQL（High Performance MySQL）；电子工业出版社，2010&lt;/p&gt;
&lt;p&gt;[2] Michael Kofler 著，杨晓云等 译；MySQL5权威指南（The Definitive Guide to MySQL5）；人民邮电出版社，2006&lt;/p&gt;
&lt;p&gt;[3] 姜承尧 著；MySQL技术内幕-InnoDB存储引擎；机械工业出版社，2011&lt;/p&gt;
&lt;p&gt;[4] D Comer, Ubiquitous B-tree; ACM Computing Surveys (CSUR), 1979&lt;/p&gt;
&lt;p&gt;[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&lt;/p&gt;
&lt;p&gt;[6] MySQL5.1参考手册 – &lt;a href=&quot;http://dev.mysql.com/doc/refman/5.1/zh/index.html&quot;&gt;http://dev.mysql.com/doc/refman/5.1/zh/index.html&lt;/a&gt;&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/628643014/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</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><description>原文地址：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&amp;#60;=n&amp;#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 [...]&lt;img src=&quot;http://www1.feedsky.com/t1/628643014/buguw/feedsky/s.gif?r=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&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;</description><category>数据结构 &amp; 算法</category><pubDate>Fri, 30 Mar 2012 17:23:00 +0800</pubDate><author>tiger7456</author><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><guid isPermaLink="false">http://www.buguw.com/?p=287</guid><dc:creator>tiger7456</dc:creator><fs:srclink>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</fs:srclink><fs:srcfeed>http://www.buguw.com/?feed=rss2</fs:srcfeed><fs:itemid>feedsky/buguw/~8163640/628643014/6267358</fs:itemid></item></channel></rss>
