﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-天空是蓝色的-文章分类-基础知识</title><link>http://www.blogjava.net/bluesky/category/4531.html</link><description>做好软件为中国
#gcc -c helloworld.c -o helloworld.o //编译目标文件
#gcc helloworld.o -o helloworld //编译成可执行exe
#helloworld //运行exe</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 08:51:19 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 08:51:19 GMT</pubDate><ttl>60</ttl><item><title>Delphi判断字符是否是数字</title><link>http://www.blogjava.net/bluesky/articles/21500.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Sat, 26 Nov 2005 05:19:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/21500.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/21500.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/21500.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/21500.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/21500.html</trackback:ping><description><![CDATA[<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">function&nbsp;IsNumeric(Data:&nbsp;string)&nbsp;:&nbsp;</SPAN><SPAN style="COLOR: #0000ff">boolean</SPAN><SPAN style="COLOR: #000000">;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;i,&nbsp;code&nbsp;:&nbsp;integer;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;begin&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;val(data,&nbsp;i,&nbsp;code);&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;code&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;result&nbsp;:</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;result&nbsp;:</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;end;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV><img src ="http://www.blogjava.net/bluesky/aggbug/21500.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-26 13:19 <a href="http://www.blogjava.net/bluesky/articles/21500.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java.util.Date类型格式化输出和字符串转Date类型</title><link>http://www.blogjava.net/bluesky/articles/20099.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Wed, 16 Nov 2005 08:59:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/20099.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/20099.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/20099.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/20099.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/20099.html</trackback:ping><description><![CDATA[<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN>&nbsp;<SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.text.Format;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.text.ParseException;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.text.SimpleDateFormat;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.util.Date;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;DateFormatToString&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">protected</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;Format&nbsp;format&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;SimpleDateFormat(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">yyyy-MM-dd&nbsp;hh:mm:ss</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">11</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;date&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Date();<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(format.format(date));<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">14</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;date1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(Date)&nbsp;format.parseObject(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">2005-11-15&nbsp;20:55:48</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(date1);<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(ParseException&nbsp;e)&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;TODO&nbsp;Auto-generated&nbsp;catch&nbsp;block</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">19</SPAN>&nbsp;<SPAN style="COLOR: #008000"></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">20</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">22</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;SimpleDateFormat(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">yyyy-MM-dd</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(format.format(date));<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;SimpleDateFormat(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">hh:mm:ss</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(format.format(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Date()));<BR></SPAN><SPAN style="COLOR: #008080">26</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">27</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">28</SPAN>&nbsp;<SPAN style="COLOR: #000000">}</SPAN></DIV><BR>输出 <BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">1</SPAN><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">2005</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">11</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">15</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">09</SPAN><SPAN style="COLOR: #000000">:</SPAN><SPAN style="COLOR: #000000">02</SPAN><SPAN style="COLOR: #000000">:</SPAN><SPAN style="COLOR: #000000">30</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>Tue&nbsp;Nov&nbsp;</SPAN><SPAN style="COLOR: #000000">15</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">20</SPAN><SPAN style="COLOR: #000000">:</SPAN><SPAN style="COLOR: #000000">55</SPAN><SPAN style="COLOR: #000000">:</SPAN><SPAN style="COLOR: #000000">48</SPAN><SPAN style="COLOR: #000000">&nbsp;CST&nbsp;</SPAN><SPAN style="COLOR: #000000">2005</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">2005</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">11</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">15</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">09</SPAN><SPAN style="COLOR: #000000">:</SPAN><SPAN style="COLOR: #000000">02</SPAN><SPAN style="COLOR: #000000">:</SPAN><SPAN style="COLOR: #000000">30</SPAN></DIV><img src ="http://www.blogjava.net/bluesky/aggbug/20099.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-16 16:59 <a href="http://www.blogjava.net/bluesky/articles/20099.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对象序列化存储读取实现代码</title><link>http://www.blogjava.net/bluesky/articles/18911.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Wed, 09 Nov 2005 02:03:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/18911.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/18911.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/18911.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/18911.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/18911.html</trackback:ping><description><![CDATA[<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.io.FileInputStream;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.io.FileOutputStream;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.io.ObjectInputStream;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.io.ObjectOutputStream;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.io.Serializable;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_213_1528_Open_Image onclick="this.style.display='none'; Codehighlighter1_213_1528_Open_Text.style.display='none'; Codehighlighter1_213_1528_Closed_Image.style.display='inline'; Codehighlighter1_213_1528_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_213_1528_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_213_1528_Closed_Text.style.display='none'; Codehighlighter1_213_1528_Open_Image.style.display='inline'; Codehighlighter1_213_1528_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;serialStore&nbsp;</SPAN><SPAN style="COLOR: #0000ff">implements</SPAN><SPAN style="COLOR: #000000">&nbsp;Serializable&nbsp;</SPAN><SPAN id=Codehighlighter1_213_1528_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_213_1528_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_238_241_Open_Image onclick="this.style.display='none'; Codehighlighter1_238_241_Open_Text.style.display='none'; Codehighlighter1_238_241_Closed_Image.style.display='inline'; Codehighlighter1_238_241_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_238_241_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_238_241_Closed_Text.style.display='none'; Codehighlighter1_238_241_Open_Image.style.display='inline'; Codehighlighter1_238_241_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;serialStore()&nbsp;</SPAN><SPAN id=Codehighlighter1_238_241_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_238_241_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;serialStore()</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_261_282_Open_Image onclick="this.style.display='none'; Codehighlighter1_261_282_Open_Text.style.display='none'; Codehighlighter1_261_282_Closed_Image.style.display='inline'; Codehighlighter1_261_282_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_261_282_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_261_282_Closed_Text.style.display='none'; Codehighlighter1_261_282_Open_Image.style.display='inline'; Codehighlighter1_261_282_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_261_282_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**&nbsp;*/</SPAN><SPAN id=Codehighlighter1_261_282_Open_Text><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;写对象到指定文件中<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_335_602_Open_Image onclick="this.style.display='none'; Codehighlighter1_335_602_Open_Text.style.display='none'; Codehighlighter1_335_602_Closed_Image.style.display='inline'; Codehighlighter1_335_602_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_335_602_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_335_602_Closed_Text.style.display='none'; Codehighlighter1_335_602_Open_Image.style.display='inline'; Codehighlighter1_335_602_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;writeFile(String&nbsp;strFile,&nbsp;Object[]&nbsp;o)&nbsp;</SPAN><SPAN id=Codehighlighter1_335_602_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_335_602_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_343_549_Open_Image onclick="this.style.display='none'; Codehighlighter1_343_549_Open_Text.style.display='none'; Codehighlighter1_343_549_Closed_Image.style.display='inline'; Codehighlighter1_343_549_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_343_549_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_343_549_Closed_Text.style.display='none'; Codehighlighter1_343_549_Open_Image.style.display='inline'; Codehighlighter1_343_549_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_343_549_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_343_549_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileOutputStream&nbsp;fos&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;FileOutputStream(strFile);<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectOutputStream&nbsp;oos&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ObjectOutputStream(fos);<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_497_529_Open_Image onclick="this.style.display='none'; Codehighlighter1_497_529_Open_Text.style.display='none'; Codehighlighter1_497_529_Closed_Image.style.display='inline'; Codehighlighter1_497_529_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_497_529_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_497_529_Closed_Text.style.display='none'; Codehighlighter1_497_529_Open_Image.style.display='inline'; Codehighlighter1_497_529_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;o.length;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_497_529_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_497_529_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.writeObject(o[i]);<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">22</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oos.flush();<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_571_599_Open_Image onclick="this.style.display='none'; Codehighlighter1_571_599_Open_Text.style.display='none'; Codehighlighter1_571_599_Closed_Image.style.display='inline'; Codehighlighter1_571_599_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_571_599_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_571_599_Closed_Text.style.display='none'; Codehighlighter1_571_599_Open_Image.style.display='inline'; Codehighlighter1_571_599_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(Exception&nbsp;e)&nbsp;</SPAN><SPAN id=Codehighlighter1_571_599_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_571_599_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">26</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;writeFile()</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">27</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">28</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_620_645_Open_Image onclick="this.style.display='none'; Codehighlighter1_620_645_Open_Text.style.display='none'; Codehighlighter1_620_645_Closed_Image.style.display='inline'; Codehighlighter1_620_645_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_620_645_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_620_645_Closed_Text.style.display='none'; Codehighlighter1_620_645_Open_Image.style.display='inline'; Codehighlighter1_620_645_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_620_645_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**&nbsp;*/</SPAN><SPAN id=Codehighlighter1_620_645_Open_Text><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">29</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;读指定文件的对象到对象组内<BR></SPAN><SPAN style="COLOR: #008080">30</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">31</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_704_1003_Open_Image onclick="this.style.display='none'; Codehighlighter1_704_1003_Open_Text.style.display='none'; Codehighlighter1_704_1003_Closed_Image.style.display='inline'; Codehighlighter1_704_1003_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_704_1003_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_704_1003_Closed_Text.style.display='none'; Codehighlighter1_704_1003_Open_Image.style.display='inline'; Codehighlighter1_704_1003_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;Object[]&nbsp;readFile(String&nbsp;strFile,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;objectNum)&nbsp;</SPAN><SPAN id=Codehighlighter1_704_1003_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_704_1003_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">32</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;o[]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Object[objectNum];<BR></SPAN><SPAN style="COLOR: #008080">33</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_750_938_Open_Image onclick="this.style.display='none'; Codehighlighter1_750_938_Open_Text.style.display='none'; Codehighlighter1_750_938_Closed_Image.style.display='inline'; Codehighlighter1_750_938_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_750_938_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_750_938_Closed_Text.style.display='none'; Codehighlighter1_750_938_Open_Image.style.display='inline'; Codehighlighter1_750_938_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_750_938_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_750_938_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">34</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInputStream&nbsp;fis&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;FileInputStream(strFile);<BR></SPAN><SPAN style="COLOR: #008080">35</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectInputStream&nbsp;ois&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ObjectInputStream(fis);<BR></SPAN><SPAN style="COLOR: #008080">36</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_900_934_Open_Image onclick="this.style.display='none'; Codehighlighter1_900_934_Open_Text.style.display='none'; Codehighlighter1_900_934_Closed_Image.style.display='inline'; Codehighlighter1_900_934_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_900_934_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_900_934_Closed_Text.style.display='none'; Codehighlighter1_900_934_Open_Image.style.display='inline'; Codehighlighter1_900_934_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;o.length;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_900_934_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_900_934_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">37</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o[i]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;ois.readObject();<BR></SPAN><SPAN style="COLOR: #008080">38</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">39</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_960_988_Open_Image onclick="this.style.display='none'; Codehighlighter1_960_988_Open_Text.style.display='none'; Codehighlighter1_960_988_Closed_Image.style.display='inline'; Codehighlighter1_960_988_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_960_988_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_960_988_Closed_Text.style.display='none'; Codehighlighter1_960_988_Open_Image.style.display='inline'; Codehighlighter1_960_988_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(Exception&nbsp;e)&nbsp;</SPAN><SPAN id=Codehighlighter1_960_988_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_960_988_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">40</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<BR></SPAN><SPAN style="COLOR: #008080">41</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">42</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;o;<BR></SPAN><SPAN style="COLOR: #008080">43</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;readFile()</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">44</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">45</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_1020_1038_Open_Image onclick="this.style.display='none'; Codehighlighter1_1020_1038_Open_Text.style.display='none'; Codehighlighter1_1020_1038_Closed_Image.style.display='inline'; Codehighlighter1_1020_1038_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1020_1038_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1020_1038_Closed_Text.style.display='none'; Codehighlighter1_1020_1038_Open_Image.style.display='inline'; Codehighlighter1_1020_1038_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_1020_1038_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**&nbsp;*/</SPAN><SPAN id=Codehighlighter1_1020_1038_Open_Text><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">46</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;显示对象内容<BR></SPAN><SPAN style="COLOR: #008080">47</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">48</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_1069_1144_Open_Image onclick="this.style.display='none'; Codehighlighter1_1069_1144_Open_Text.style.display='none'; Codehighlighter1_1069_1144_Closed_Image.style.display='inline'; Codehighlighter1_1069_1144_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1069_1144_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1069_1144_Closed_Text.style.display='none'; Codehighlighter1_1069_1144_Open_Image.style.display='inline'; Codehighlighter1_1069_1144_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;log(Object[]&nbsp;o)&nbsp;</SPAN><SPAN id=Codehighlighter1_1069_1144_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1069_1144_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">49</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_1108_1141_Open_Image onclick="this.style.display='none'; Codehighlighter1_1108_1141_Open_Text.style.display='none'; Codehighlighter1_1108_1141_Closed_Image.style.display='inline'; Codehighlighter1_1108_1141_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1108_1141_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1108_1141_Closed_Text.style.display='none'; Codehighlighter1_1108_1141_Open_Image.style.display='inline'; Codehighlighter1_1108_1141_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;o.length;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_1108_1141_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1108_1141_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">50</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(o[i]);<BR></SPAN><SPAN style="COLOR: #008080">51</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">52</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;log()</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">53</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">54</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_1156_1174_Open_Image onclick="this.style.display='none'; Codehighlighter1_1156_1174_Open_Text.style.display='none'; Codehighlighter1_1156_1174_Closed_Image.style.display='inline'; Codehighlighter1_1156_1174_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1156_1174_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1156_1174_Closed_Text.style.display='none'; Codehighlighter1_1156_1174_Open_Image.style.display='inline'; Codehighlighter1_1156_1174_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_1156_1174_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**&nbsp;*/</SPAN><SPAN id=Codehighlighter1_1156_1174_Open_Text><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">55</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;显示对象内容<BR></SPAN><SPAN style="COLOR: #008080">56</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">57</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_1203_1231_Open_Image onclick="this.style.display='none'; Codehighlighter1_1203_1231_Open_Text.style.display='none'; Codehighlighter1_1203_1231_Closed_Image.style.display='inline'; Codehighlighter1_1203_1231_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1203_1231_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1203_1231_Closed_Text.style.display='none'; Codehighlighter1_1203_1231_Open_Image.style.display='inline'; Codehighlighter1_1203_1231_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;log(Object&nbsp;o)&nbsp;</SPAN><SPAN id=Codehighlighter1_1203_1231_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1203_1231_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">58</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(o);<BR></SPAN><SPAN style="COLOR: #008080">59</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;log()</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">60</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">61</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_1281_1516_Open_Image onclick="this.style.display='none'; Codehighlighter1_1281_1516_Open_Text.style.display='none'; Codehighlighter1_1281_1516_Closed_Image.style.display='inline'; Codehighlighter1_1281_1516_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1281_1516_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1281_1516_Closed_Text.style.display='none'; Codehighlighter1_1281_1516_Open_Image.style.display='inline'; Codehighlighter1_1281_1516_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String&nbsp;arg[])&nbsp;</SPAN><SPAN id=Codehighlighter1_1281_1516_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1281_1516_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">62</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object&nbsp;o[]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Object[</SPAN><SPAN style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000">];<BR></SPAN><SPAN style="COLOR: #008080">63</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o[</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;String(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">objectName</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">64</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o[</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Integer(</SPAN><SPAN style="COLOR: #000000">25</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">65</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serialStore&nbsp;ss&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;serialStore();<BR></SPAN><SPAN style="COLOR: #008080">66</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;strFile&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">serial.abc</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">67</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ss.writeFile(strFile,&nbsp;o);<BR></SPAN><SPAN style="COLOR: #008080">68</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ss.log(ss.readFile(strFile,&nbsp;o.length));<BR></SPAN><SPAN style="COLOR: #008080">69</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">main()&nbsp;</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">70</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">71</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">72</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_1530_1547_Open_Image onclick="this.style.display='none'; Codehighlighter1_1530_1547_Open_Text.style.display='none'; Codehighlighter1_1530_1547_Closed_Image.style.display='inline'; Codehighlighter1_1530_1547_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_1530_1547_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1530_1547_Closed_Text.style.display='none'; Codehighlighter1_1530_1547_Open_Image.style.display='inline'; Codehighlighter1_1530_1547_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_1530_1547_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**&nbsp;*/</SPAN><SPAN id=Codehighlighter1_1530_1547_Open_Text><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000">&nbsp;serialStore&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">73</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">74</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV><BR><IMG height=20 src="http://www.blogjava.net/Emoticons/QQ/15.gif" width=20 border=0><img src ="http://www.blogjava.net/bluesky/aggbug/18911.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-09 10:03 <a href="http://www.blogjava.net/bluesky/articles/18911.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java对象序列化机制</title><link>http://www.blogjava.net/bluesky/articles/18901.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Wed, 09 Nov 2005 01:52:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/18901.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/18901.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/18901.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/18901.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/18901.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-ansi-language: EN">对象序列化机制对于需要将对象的状态保存到文件中，而后能够通过读入对象状态来重新构造对象，恢复程序状态，或者使用套接字在网络上传送对象的程序来说，是很有用的。通过让类实现</SPAN><SPAN lang=EN style="mso-ansi-language: EN">java.io.Serializable </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-ansi-language: EN">接口可以将类序列化。这个接口是一个制造者（</SPAN><SPAN lang=EN style="mso-ansi-language: EN">marker</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-ansi-language: EN">）接口。也就是说，对于要实现它的类来说，该接口不需要实现任何方法。它主要用来通知</SPAN><SPAN lang=EN style="mso-ansi-language: EN">Java</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-ansi-language: EN">虚拟机（</SPAN><SPAN lang=EN style="mso-ansi-language: EN">JVM</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-ansi-language: EN">）需要将一个对象序列化。</SPAN><SPAN lang=EN style="mso-ansi-language: EN"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-ansi-language: EN">也叫串行化，对象的域转化为紧凑的</SPAN><SPAN lang=EN style="mso-ansi-language: EN">2</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-ansi-language: EN">进制形式，是对象持久化的方法之一。<BR><BR>《实现类的序列化--例子将Vector对象压入标准流》出处 CN-JAVA原创：jackliu<BR></SPAN><SPAN lang=EN style="mso-ansi-language: EN"><o:p><BR>Vector对象是一个很灵活的java数据结构，在实际编程中，有时需要我们将一个Vector对象传递给另一个Java程序并保持Vector的数据结构状态，这时，我们可以将需要传递的对象实现java.io.Serializable接口，序列化这个类，由于序列化本身就是允许一个对象在虚拟机之间传送(或者经过一段空间，如在RMI中;或者经过一段时间，比如数据流被保存到一个文件中)。 关于类的序列化请查阅相关资料，本篇不在叙述，下面使用一个简单的程序说明如何把一个Vector对象序列化并放到一个流中（为了测试方便，这里放到一个文件流中，如果换成一个套接字就可以把对象发送给远程请求者）</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="mso-ansi-language: EN"><o:p>程序1：把一个Vector对象存储到一个testvector.obj文件里（模拟server socket处理）<BR></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.io.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.util.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_87_1155_Open_Image onclick="this.style.display='none'; Codehighlighter1_87_1155_Open_Text.style.display='none'; Codehighlighter1_87_1155_Closed_Image.style.display='inline'; Codehighlighter1_87_1155_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_87_1155_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_87_1155_Closed_Text.style.display='none'; Codehighlighter1_87_1155_Open_Image.style.display='inline'; Codehighlighter1_87_1155_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;TestVector&nbsp;</SPAN><SPAN style="COLOR: #0000ff">implements</SPAN><SPAN style="COLOR: #000000">&nbsp;java.io.Serializable</SPAN><SPAN id=Codehighlighter1_87_1155_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_87_1155_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;Vector&nbsp;vect</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Vector();<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_175_221_Open_Image onclick="this.style.display='none'; Codehighlighter1_175_221_Open_Text.style.display='none'; Codehighlighter1_175_221_Closed_Image.style.display='inline'; Codehighlighter1_175_221_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_175_221_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_175_221_Closed_Text.style.display='none'; Codehighlighter1_175_221_Open_Image.style.display='inline'; Codehighlighter1_175_221_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;add(Object&nbsp;obj)</SPAN><SPAN id=Codehighlighter1_175_221_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_175_221_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.vect.add(obj);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_250_307_Open_Image onclick="this.style.display='none'; Codehighlighter1_250_307_Open_Text.style.display='none'; Codehighlighter1_250_307_Closed_Image.style.display='inline'; Codehighlighter1_250_307_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_250_307_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_250_307_Closed_Text.style.display='none'; Codehighlighter1_250_307_Open_Image.style.display='inline'; Codehighlighter1_250_307_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;print()</SPAN><SPAN id=Codehighlighter1_250_307_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_250_307_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.vect);<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_355_1153_Open_Image onclick="this.style.display='none'; Codehighlighter1_355_1153_Open_Text.style.display='none'; Codehighlighter1_355_1153_Closed_Image.style.display='inline'; Codehighlighter1_355_1153_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_355_1153_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_355_1153_Closed_Text.style.display='none'; Codehighlighter1_355_1153_Open_Image.style.display='inline'; Codehighlighter1_355_1153_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)</SPAN><SPAN id=Codehighlighter1_355_1153_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_355_1153_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_377_1058_Open_Image onclick="this.style.display='none'; Codehighlighter1_377_1058_Open_Text.style.display='none'; Codehighlighter1_377_1058_Closed_Image.style.display='inline'; Codehighlighter1_377_1058_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_377_1058_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_377_1058_Closed_Text.style.display='none'; Codehighlighter1_377_1058_Open_Image.style.display='inline'; Codehighlighter1_377_1058_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_377_1058_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_377_1058_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_403_418_Open_Image onclick="this.style.display='none'; Codehighlighter1_403_418_Open_Text.style.display='none'; Codehighlighter1_403_418_Closed_Image.style.display='inline'; Codehighlighter1_403_418_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_403_418_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_403_418_Closed_Text.style.display='none'; Codehighlighter1_403_418_Open_Image.style.display='inline'; Codehighlighter1_403_418_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_403_418_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_403_418_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;将对象写到一个文件里&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileOutputStream&nbsp;objfile&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;FileOutputStream(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">testvector.obj</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_535_547_Open_Image onclick="this.style.display='none'; Codehighlighter1_535_547_Open_Text.style.display='none'; Codehighlighter1_535_547_Closed_Image.style.display='inline'; Codehighlighter1_535_547_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_535_547_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_535_547_Closed_Text.style.display='none'; Codehighlighter1_535_547_Open_Image.style.display='inline'; Codehighlighter1_535_547_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_535_547_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_535_547_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;创建一个输出流&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">18</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectOutputStream&nbsp;p&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ObjectOutputStream(objfile);<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_653_674_Open_Image onclick="this.style.display='none'; Codehighlighter1_653_674_Open_Text.style.display='none'; Codehighlighter1_653_674_Closed_Image.style.display='inline'; Codehighlighter1_653_674_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_653_674_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_653_674_Closed_Text.style.display='none'; Codehighlighter1_653_674_Open_Image.style.display='inline'; Codehighlighter1_653_674_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_653_674_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_653_674_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;创建一个TestVector对象&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">20</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TestVector&nbsp;tv&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;TestVector();<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_757_779_Open_Image onclick="this.style.display='none'; Codehighlighter1_757_779_Open_Text.style.display='none'; Codehighlighter1_757_779_Closed_Image.style.display='inline'; Codehighlighter1_757_779_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_757_779_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_757_779_Closed_Text.style.display='none'; Codehighlighter1_757_779_Open_Image.style.display='inline'; Codehighlighter1_757_779_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_757_779_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_757_779_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">给Vector写入几String个对象</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">22</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tv.add(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">One</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tv.add(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Two</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tv.add(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Three</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.writeObject(tv);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;把tv写入流</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">26</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.flush();<BR></SPAN><SPAN style="COLOR: #008080">27</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objfile.close();&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;关闭文件对象</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">28</SPAN><SPAN style="COLOR: #008000"><IMG id=Codehighlighter1_1080_1143_Open_Image onclick="this.style.display='none'; Codehighlighter1_1080_1143_Open_Text.style.display='none'; Codehighlighter1_1080_1143_Closed_Image.style.display='inline'; Codehighlighter1_1080_1143_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1080_1143_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1080_1143_Closed_Text.style.display='none'; Codehighlighter1_1080_1143_Open_Image.style.display='inline'; Codehighlighter1_1080_1143_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(Exception&nbsp;e)&nbsp;</SPAN><SPAN id=Codehighlighter1_1080_1143_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1080_1143_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">29</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<BR></SPAN><SPAN style="COLOR: #008080">30</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">31</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">32</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">33</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt">编译程序1，运行后，在当前目录生成一个testvector.obj文件，这个文件里存放了TestVector类的数据</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><BR>程序2：从testvector.obj文件里获取TestVector对象（模拟socke客户端）<BR></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.io.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.util.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_58_623_Open_Image onclick="this.style.display='none'; Codehighlighter1_58_623_Open_Text.style.display='none'; Codehighlighter1_58_623_Closed_Image.style.display='inline'; Codehighlighter1_58_623_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_58_623_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_58_623_Closed_Text.style.display='none'; Codehighlighter1_58_623_Open_Image.style.display='inline'; Codehighlighter1_58_623_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;ReadVectorObj</SPAN><SPAN id=Codehighlighter1_58_623_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_58_623_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_108_621_Open_Image onclick="this.style.display='none'; Codehighlighter1_108_621_Open_Text.style.display='none'; Codehighlighter1_108_621_Closed_Image.style.display='inline'; Codehighlighter1_108_621_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_108_621_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_108_621_Closed_Text.style.display='none'; Codehighlighter1_108_621_Open_Image.style.display='inline'; Codehighlighter1_108_621_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;main&nbsp;(String[]&nbsp;args)</SPAN><SPAN id=Codehighlighter1_108_621_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_108_621_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_130_523_Open_Image onclick="this.style.display='none'; Codehighlighter1_130_523_Open_Text.style.display='none'; Codehighlighter1_130_523_Closed_Image.style.display='inline'; Codehighlighter1_130_523_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_130_523_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_130_523_Closed_Text.style.display='none'; Codehighlighter1_130_523_Open_Image.style.display='inline'; Codehighlighter1_130_523_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_130_523_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_130_523_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_156_182_Open_Image onclick="this.style.display='none'; Codehighlighter1_156_182_Open_Text.style.display='none'; Codehighlighter1_156_182_Closed_Image.style.display='inline'; Codehighlighter1_156_182_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_156_182_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_156_182_Closed_Text.style.display='none'; Codehighlighter1_156_182_Open_Image.style.display='inline'; Codehighlighter1_156_182_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_156_182_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_156_182_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;打开文件，读取Vector&nbsp;存放的对象数据&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInputStream&nbsp;objfile&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;FileInputStream(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">testvector.obj</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ObjectInputStream&nbsp;q&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ObjectInputStream(objfile);<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_375_388_Open_Image onclick="this.style.display='none'; Codehighlighter1_375_388_Open_Text.style.display='none'; Codehighlighter1_375_388_Closed_Image.style.display='inline'; Codehighlighter1_375_388_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_375_388_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_375_388_Closed_Text.style.display='none'; Codehighlighter1_375_388_Open_Image.style.display='inline'; Codehighlighter1_375_388_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_375_388_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_375_388_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;获取对象原始数据&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TestVector&nbsp;myvector&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(TestVector)q.readObject();<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myvector.print();&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_545_611_Open_Image onclick="this.style.display='none'; Codehighlighter1_545_611_Open_Text.style.display='none'; Codehighlighter1_545_611_Closed_Image.style.display='inline'; Codehighlighter1_545_611_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_545_611_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_545_611_Closed_Text.style.display='none'; Codehighlighter1_545_611_Open_Image.style.display='inline'; Codehighlighter1_545_611_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(Exception&nbsp;e)&nbsp;</SPAN><SPAN id=Codehighlighter1_545_611_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_545_611_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();&nbsp;&nbsp;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><BR>编译程序2，运行后，读取testvector.obj文件，根据内容构建出原始的TestVector类。</P></o:p></SPAN><img src ="http://www.blogjava.net/bluesky/aggbug/18901.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-09 09:52 <a href="http://www.blogjava.net/bluesky/articles/18901.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>三种初始化字段的方式</title><link>http://www.blogjava.net/bluesky/articles/18872.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Tue, 08 Nov 2005 15:11:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/18872.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/18872.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/18872.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/18872.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/18872.html</trackback:ping><description><![CDATA[1<FONT color=#000000>构造函数中赋值<BR>2声明时赋值<BR>3初始化块 initialization block。类中可以包含任意多的代码块，只要构造了类的对象，这些代码块就会被执行。<BR><BR>例子<BR>People.java<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">package</SPAN><SPAN style="COLOR: #000000">&nbsp;test;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_35_360_Open_Image onclick="this.style.display='none'; Codehighlighter1_35_360_Open_Text.style.display='none'; Codehighlighter1_35_360_Closed_Image.style.display='inline'; Codehighlighter1_35_360_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_35_360_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_35_360_Closed_Text.style.display='none'; Codehighlighter1_35_360_Open_Image.style.display='inline'; Codehighlighter1_35_360_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;People&nbsp;</SPAN><SPAN id=Codehighlighter1_35_360_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_35_360_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;name;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;age;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_98_115_Open_Image onclick="this.style.display='none'; Codehighlighter1_98_115_Open_Text.style.display='none'; Codehighlighter1_98_115_Closed_Image.style.display='inline'; Codehighlighter1_98_115_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_98_115_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_98_115_Closed_Text.style.display='none'; Codehighlighter1_98_115_Open_Image.style.display='inline'; Codehighlighter1_98_115_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;getAge()&nbsp;</SPAN><SPAN id=Codehighlighter1_98_115_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_98_115_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;age;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_146_167_Open_Image onclick="this.style.display='none'; Codehighlighter1_146_167_Open_Text.style.display='none'; Codehighlighter1_146_167_Closed_Image.style.display='inline'; Codehighlighter1_146_167_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_146_167_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_146_167_Closed_Text.style.display='none'; Codehighlighter1_146_167_Open_Image.style.display='inline'; Codehighlighter1_146_167_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;setAge(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;age)&nbsp;</SPAN><SPAN id=Codehighlighter1_146_167_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_146_167_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.age&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;age;<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_194_212_Open_Image onclick="this.style.display='none'; Codehighlighter1_194_212_Open_Text.style.display='none'; Codehighlighter1_194_212_Closed_Image.style.display='inline'; Codehighlighter1_194_212_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_194_212_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_194_212_Closed_Text.style.display='none'; Codehighlighter1_194_212_Open_Image.style.display='inline'; Codehighlighter1_194_212_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;String&nbsp;getName()&nbsp;</SPAN><SPAN id=Codehighlighter1_194_212_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_194_212_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;name;<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_248_271_Open_Image onclick="this.style.display='none'; Codehighlighter1_248_271_Open_Text.style.display='none'; Codehighlighter1_248_271_Closed_Image.style.display='inline'; Codehighlighter1_248_271_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_248_271_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_248_271_Closed_Text.style.display='none'; Codehighlighter1_248_271_Open_Image.style.display='inline'; Codehighlighter1_248_271_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;setName(String&nbsp;name)&nbsp;</SPAN><SPAN id=Codehighlighter1_248_271_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_248_271_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.name&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;name;<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">18</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_291_305_Open_Image onclick="this.style.display='none'; Codehighlighter1_291_305_Open_Text.style.display='none'; Codehighlighter1_291_305_Closed_Image.style.display='inline'; Codehighlighter1_291_305_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_291_305_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_291_305_Closed_Text.style.display='none'; Codehighlighter1_291_305_Open_Image.style.display='inline'; Codehighlighter1_291_305_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;People()</SPAN><SPAN id=Codehighlighter1_291_305_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_291_305_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">默认构造函数</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">21</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">22</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_308_357_Open_Image onclick="this.style.display='none'; Codehighlighter1_308_357_Open_Text.style.display='none'; Codehighlighter1_308_357_Closed_Image.style.display='inline'; Codehighlighter1_308_357_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_308_357_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_308_357_Closed_Text.style.display='none'; Codehighlighter1_308_357_Open_Image.style.display='inline'; Codehighlighter1_308_357_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_308_357_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_308_357_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.setAge(</SPAN><SPAN style="COLOR: #000000">12</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.setName(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">bluesky</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">26</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">27</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">28</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV><BR>Test.java<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">1</SPAN><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">package</SPAN><SPAN style="COLOR: #000000">&nbsp;test;<BR></SPAN><SPAN style="COLOR: #008080">2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">3</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_33_142_Open_Image onclick="this.style.display='none'; Codehighlighter1_33_142_Open_Text.style.display='none'; Codehighlighter1_33_142_Closed_Image.style.display='inline'; Codehighlighter1_33_142_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_33_142_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_33_142_Closed_Text.style.display='none'; Codehighlighter1_33_142_Open_Image.style.display='inline'; Codehighlighter1_33_142_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;Test&nbsp;</SPAN><SPAN id=Codehighlighter1_33_142_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_33_142_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">4</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_74_139_Open_Image onclick="this.style.display='none'; Codehighlighter1_74_139_Open_Text.style.display='none'; Codehighlighter1_74_139_Closed_Image.style.display='inline'; Codehighlighter1_74_139_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_74_139_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_74_139_Closed_Text.style.display='none'; Codehighlighter1_74_139_Open_Image.style.display='inline'; Codehighlighter1_74_139_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String&nbsp;args[])</SPAN><SPAN id=Codehighlighter1_74_139_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_74_139_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;People&nbsp;p&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;People();<BR></SPAN><SPAN style="COLOR: #008080">6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(p.getName());<BR></SPAN><SPAN style="COLOR: #008080">7</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV></FONT>输出bluesky<img src ="http://www.blogjava.net/bluesky/aggbug/18872.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-08 23:11 <a href="http://www.blogjava.net/bluesky/articles/18872.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JavaIO利用迭代读取文件夹所有目录及文件</title><link>http://www.blogjava.net/bluesky/articles/18633.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Mon, 07 Nov 2005 10:08:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/18633.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/18633.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/18633.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/18633.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/18633.html</trackback:ping><description><![CDATA[<P> </P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080"> 1</SPAN><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.io.File;<BR></SPAN><SPAN style="COLOR: #008080"> 2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.util.ArrayList;<BR></SPAN><SPAN style="COLOR: #008080"> 3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080"> 4</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_72_734_Open_Image onclick="this.style.display='none'; Codehighlighter1_72_734_Open_Text.style.display='none'; Codehighlighter1_72_734_Closed_Image.style.display='inline'; Codehighlighter1_72_734_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_72_734_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_72_734_Closed_Text.style.display='none'; Codehighlighter1_72_734_Open_Image.style.display='inline'; Codehighlighter1_72_734_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> ReadFile </SPAN><SPAN id=Codehighlighter1_72_734_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_72_734_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080"> 5</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_114_252_Open_Image onclick="this.style.display='none'; Codehighlighter1_114_252_Open_Text.style.display='none'; Codehighlighter1_114_252_Closed_Image.style.display='inline'; Codehighlighter1_114_252_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_114_252_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_114_252_Closed_Text.style.display='none'; Codehighlighter1_114_252_Open_Image.style.display='inline'; Codehighlighter1_114_252_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> main(String args[]) </SPAN><SPAN id=Codehighlighter1_114_252_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_114_252_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080"> 6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>  String dir </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">D:\\xxx</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080"> 7</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080"> 8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>  ArrayList a </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> getFiles(dir);<BR></SPAN><SPAN style="COLOR: #008080"> 9</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_213_249_Open_Image onclick="this.style.display='none'; Codehighlighter1_213_249_Open_Text.style.display='none'; Codehighlighter1_213_249_Closed_Image.style.display='inline'; Codehighlighter1_213_249_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_213_249_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_213_249_Closed_Text.style.display='none'; Codehighlighter1_213_249_Open_Image.style.display='inline'; Codehighlighter1_213_249_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>  </SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> i </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">; i </SPAN><SPAN style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000"> a.size(); i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">) </SPAN><SPAN id=Codehighlighter1_213_249_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_213_249_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>   File file </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (File) a.get(i);<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>  }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top> }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_302_731_Open_Image onclick="this.style.display='none'; Codehighlighter1_302_731_Open_Text.style.display='none'; Codehighlighter1_302_731_Closed_Image.style.display='inline'; Codehighlighter1_302_731_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_302_731_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_302_731_Closed_Text.style.display='none'; Codehighlighter1_302_731_Open_Image.style.display='inline'; Codehighlighter1_302_731_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000"> ArrayList getFiles(String aDir) </SPAN><SPAN id=Codehighlighter1_302_731_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_302_731_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>  ArrayList files </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> ArrayList();<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>  File dirx </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> File(aDir);<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>  File[] dirFilesx </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> dirx.listFiles();<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">19</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_456_711_Open_Image onclick="this.style.display='none'; Codehighlighter1_456_711_Open_Text.style.display='none'; Codehighlighter1_456_711_Closed_Image.style.display='inline'; Codehighlighter1_456_711_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_456_711_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_456_711_Closed_Text.style.display='none'; Codehighlighter1_456_711_Open_Image.style.display='inline'; Codehighlighter1_456_711_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>  </SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> k </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">; k </SPAN><SPAN style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000"> dirFilesx.length; k</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">) </SPAN><SPAN id=Codehighlighter1_456_711_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_456_711_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>   System.out.print(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">|</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>   File file </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> dirFilesx[k];<BR></SPAN><SPAN style="COLOR: #008080">22</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>   String fileName </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> file.getName();<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>   System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> fileName);<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_616_687_Open_Image onclick="this.style.display='none'; Codehighlighter1_616_687_Open_Text.style.display='none'; Codehighlighter1_616_687_Closed_Image.style.display='inline'; Codehighlighter1_616_687_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_616_687_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_616_687_Closed_Text.style.display='none'; Codehighlighter1_616_687_Open_Image.style.display='inline'; Codehighlighter1_616_687_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>   </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (file.isDirectory()) </SPAN><SPAN id=Codehighlighter1_616_687_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_616_687_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>    String dirx1 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> aDir </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">\\</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> fileName;<BR></SPAN><SPAN style="COLOR: #008080">26</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>    getFiles(dirx1);<BR></SPAN><SPAN style="COLOR: #008080">27</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>   }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">28</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>   files.add(file);<BR></SPAN><SPAN style="COLOR: #008080">29</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>  }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">30</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>  </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> files;<BR></SPAN><SPAN style="COLOR: #008080">31</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">32</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top> }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">33</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">34</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">35</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">36</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV><img src ="http://www.blogjava.net/bluesky/aggbug/18633.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-07 18:08 <a href="http://www.blogjava.net/bluesky/articles/18633.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>正则表达式应用</title><link>http://www.blogjava.net/bluesky/articles/18610.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Mon, 07 Nov 2005 09:06:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/18610.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/18610.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/18610.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/18610.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/18610.html</trackback:ping><description><![CDATA[<P>正则表达式是一种功能强大但又非常灵活的文本处理工具。它能让你用编程的方式来描述复杂的文本模式，然后在字符串里把它找出来。一旦你找到了这种模式，你就能随心所欲地处理这些文本了。虽然初看起来正则表达式的语法有点让人望而生畏，但它提供了一种精练的动态语言，使我们能用一种通用的方式来解决各种字符串的问题，包括匹配，选择，编辑以及校验。</P>
<P id=header56><STRONG>创建正则表达式</STRONG></P>
<P>
<TABLE class=narration cellSpacing=0 borderColorDark=#000000 cellPadding=2 borderColorLight=#000000 border=2>
<TBODY>
<TR>
<TH colSpan=2>字符</TH></TR>
<TR>
<TD><SPAN class=original_words>B</SPAN></TD>
<TD>字符<SPAN class=original_words>B</SPAN></TD></TR>
<TR>
<TD><SPAN class=original_words>\xhh</SPAN></TD>
<TD>16进制值<SPAN class=original_words>0xhh</SPAN>所表示的字符</TD></TR>
<TR>
<TD><SPAN class=original_words>\uhhhh</SPAN></TD>
<TD>16进制值<SPAN class=original_words>0xhhhh</SPAN>所表示的Unicode字符</TD></TR>
<TR>
<TD><SPAN class=original_words>\t</SPAN></TD>
<TD>Tab</TD></TR>
<TR>
<TD><SPAN class=original_words>\n</SPAN></TD>
<TD>换行符</TD></TR>
<TR>
<TD><SPAN class=original_words>\r</SPAN></TD>
<TD>回车符</TD></TR>
<TR>
<TD><SPAN class=original_words>\f</SPAN></TD>
<TD>换页符</TD></TR>
<TR>
<TD><SPAN class=original_words>\e</SPAN></TD>
<TD>Escape</TD></TR></TBODY></TABLE><BR></P>
<P>正则表达式的强大体现在它能定义字符集(character class)。下面是一些最常见的字符集及其定义的方式，此外还有一些预定义的字符集：</P>
<P>
<TABLE class=narration cellSpacing=0 borderColorDark=#000000 cellPadding=2 borderColorLight=#000000 border=2>
<TBODY>
<TR vAlign=top>
<TH colSpan=2><SPAN class=original_words>字符集</SPAN> </TH></TR>
<TR vAlign=top>
<TD vAlign=top><SPAN class=original_words>.</SPAN> </TD>
<TD vAlign=top>表示任意一个字符 </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>[abc]</SPAN> </TD>
<TD vAlign=top width=311>表示字符<SPAN class=original_words>a</SPAN>，<SPAN class=original_words>b</SPAN>，<SPAN class=original_words>c</SPAN>中的任意一个(与<SPAN class=original_words>a|b|c</SPAN>相同) </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>[^abc]</SPAN> </TD>
<TD vAlign=top width=311>除<SPAN class=original_words>a</SPAN>，<SPAN class=original_words>b</SPAN>，<SPAN class=original_words>c</SPAN>之外的任意一个字符(否定) </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>[a-zA-Z]</SPAN> </TD>
<TD vAlign=top width=311>从<SPAN class=original_words>a</SPAN>到<SPAN class=original_words>z</SPAN>或<SPAN class=original_words>A</SPAN>到<SPAN class=original_words>Z</SPAN>当中的任意一个字符(范围) </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>[abc[hij]]</SPAN> </TD>
<TD vAlign=top width=311><SPAN class=original_words>a,b,c,h,i,j</SPAN>中的任意一个字符(与<SPAN class=original_words>a|b|c|h|i|j</SPAN>相同)(并集) </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>[a-z&amp;&amp;[hij]]</SPAN> </TD>
<TD vAlign=top width=311><SPAN class=original_words>h,i,j</SPAN>中的一个(交集) </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>\s</SPAN> </TD>
<TD vAlign=top width=311>空格字符(空格键, tab, 换行, 换页, 回车) </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>\S</SPAN> </TD>
<TD vAlign=top width=311>非空格字符(<SPAN class=original_words>[^\s]</SPAN>) </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>\d</SPAN> </TD>
<TD vAlign=top width=311>一个数字，也就是<SPAN class=original_words>[0-9]</SPAN> </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>\D</SPAN> </TD>
<TD vAlign=top width=311>一个非数字的字符，也就是<SPAN class=original_words>[^0-9]</SPAN> </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>\w</SPAN> </TD>
<TD vAlign=top width=311>一个单词字符(word character)，即<SPAN class=original_words>[a-zA-Z_0-9]</SPAN> </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=131><SPAN class=original_words>\W</SPAN> </TD>
<TD vAlign=top width=311>一个非单词的字符，<SPAN class=original_words>[^\w]</SPAN> </TD></TR></TBODY></TABLE></P>
<P>如果你用过其它语言的正则表达式，那么你一眼就能看出反斜杠的与众不同。在其它语言里，"<SPAN class=original_words>\\</SPAN>"的意思是"我只是要在正则表达式里插入一个反斜杠。没什么特别的意思。"但是在Java里，"<SPAN class=original_words>\\</SPAN>"的意思是"我要插入一个正则表达式的反斜杠，所以跟在它后面的那个字符的意思就变了。"举例来说，如果你想表示一个或更多的"单词字符"，那么这个正则表达式就应该是"<SPAN class=original_words>\\w+</SPAN>"。如果你要插入一个反斜杠，那就得用"<SPAN class=original_words>\\\\</SPAN>"。不过像换行，跳格之类的还是只用一根反斜杠："\n\t"。</P>
<P>这里只给你讲一个例子；你应该<SPAN class=original_words>JDK</SPAN>文档的<SPAN class=original_words>java.util.regex.Pattern</SPAN>加到收藏夹里，这样就能很容易地找到各种正则表达式的模式了。</P>
<P>
<TABLE class=narration cellSpacing=0 borderColorDark=#000000 cellPadding=2 borderColorLight=#000000 border=2>
<TBODY>
<TR>
<TH colSpan=2>逻辑运算符 </TH></TR>
<TR>
<TD>XY</TD>
<TD>X 后面跟着 Y</TD></TR>
<TR>
<TD>X|Y</TD>
<TD>X或Y</TD></TR>
<TR>
<TD>(X)</TD>
<TD>一个"要匹配的组(capturing group)". 以后可以用\i来表示第i个被匹配的组。</TD></TR></TBODY></TABLE></P>
<P>
<TABLE class=narration cellSpacing=0 borderColorDark=#000000 cellPadding=2 borderColorLight=#000000 border=2>
<TBODY>
<TR vAlign=top>
<TH vAlign=top width=383 colSpan=2>边界匹配符 </TH></TR>
<TR vAlign=top>
<TD vAlign=top width=143><SPAN class=original_words>^</SPAN> </TD>
<TD vAlign=top width=239>一行的开始 </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=143><SPAN class=original_words>$</SPAN> </TD>
<TD vAlign=top width=239>一行的结尾 </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=143><SPAN class=original_words>\b</SPAN> </TD>
<TD vAlign=top width=239>一个单词的边界 </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=143><SPAN class=original_words>\B</SPAN> </TD>
<TD vAlign=top width=239>一个非单词的边界 </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=143><SPAN class=original_words>\G</SPAN> </TD>
<TD vAlign=top width=239>前一个匹配的结束 </TD></TR></TBODY></TABLE></P><BR>
<P id=header57><STRONG>数量表示符</STRONG></P>
<P>"数量表示符(quantifier)"的作用是定义模式应该匹配多少个字符。</P>
<UL>
<LI>Greedy(贪婪的)： 除非另有表示，否则数量表示符都是greedy的。Greedy的表达式会一直匹配下去，直到匹配不下去为止。<U>(如果你发现表达式匹配的结果与预期的不符)</U>，很有可能是因为，你以为表达式会只匹配前面几个字符，而实际上它是greedy的，因此会一直匹配下去。 
<LI>Reluctant(勉强的)： 用问号表示，它会匹配最少的字符。也称为lazy, minimal matching, non-greedy, 或ungreedy。 
<LI>Possessive(占有的)： 目前只有Java支持(其它语言都不支持)。它更加先进，所以你可能还不太会用。用正则表达式匹配字符串的时候会产生很多中间状态，<U>(一般的匹配引擎会保存这种中间状态，)</U>这样匹配失败的时候就能原路返回了。占有型的表达式不保存这种中间状态，因此也就不会回头重来了。它能防止正则表达式的失控，同时也能提高运行的效率。 </LI></UL>
<TABLE class=narration cellSpacing=0 borderColorDark=#000000 cellPadding=2 borderColorLight=#000000 border=2>
<TBODY>
<TR vAlign=top>
<TH><SPAN class=original_words>Greedy</SPAN> </TH>
<TH><SPAN class=original_words>Reluctant</SPAN> </TH>
<TH><SPAN class=original_words>Possessive</SPAN> </TH>
<TH>匹配 </TH></TR>
<TR vAlign=top>
<TD vAlign=top width=71><SPAN class=original_words>X?</SPAN> </TD>
<TD vAlign=top width=86><SPAN class=original_words>X??</SPAN> </TD>
<TD vAlign=top width=93><SPAN class=original_words>X?+</SPAN> </TD>
<TD vAlign=top width=231>匹配一个或零个<SPAN class=original_words>X</SPAN> </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=71><SPAN class=original_words>X*</SPAN> </TD>
<TD vAlign=top width=86><SPAN class=original_words>X*?</SPAN> </TD>
<TD vAlign=top width=93><SPAN class=original_words>X*+</SPAN> </TD>
<TD vAlign=top width=231>匹配零或多个<SPAN class=original_words>X</SPAN> </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=71><SPAN class=original_words>X+</SPAN> </TD>
<TD vAlign=top width=86><SPAN class=original_words>X+?</SPAN> </TD>
<TD vAlign=top width=93><SPAN class=original_words>X++</SPAN> </TD>
<TD vAlign=top width=231>匹配一个或多个<SPAN class=original_words>X</SPAN> </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=71><SPAN class=original_words>X{n}</SPAN> </TD>
<TD vAlign=top width=86><SPAN class=original_words>X{n}?</SPAN> </TD>
<TD vAlign=top width=93><SPAN class=original_words>X{n}+</SPAN> </TD>
<TD vAlign=top width=231>匹配正好n个<SPAN class=original_words>X</SPAN> </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=71><SPAN class=original_words>X{n,}</SPAN> </TD>
<TD vAlign=top width=86><SPAN class=original_words>X{n,}?</SPAN> </TD>
<TD vAlign=top width=93><SPAN class=original_words>X{n,}+</SPAN> </TD>
<TD vAlign=top width=231>匹配至少n个<SPAN class=original_words>X</SPAN> </TD></TR>
<TR vAlign=top>
<TD vAlign=top width=71><SPAN class=original_words>X{n,m}</SPAN> </TD>
<TD vAlign=top width=86><SPAN class=original_words>X{n,m}?</SPAN> </TD>
<TD vAlign=top width=93><SPAN class=original_words>X{n,m}+</SPAN> </TD>
<TD vAlign=top width=231>匹配至少n个，至多m个<SPAN class=original_words>X</SPAN> </TD></TR></TBODY></TABLE>
<P>再提醒一下，要想让表达式照你的意思去运行，你应该用括号把'X'括起来。比方说：</P>
<BLOCKQUOTE><PRE>abc+</PRE></BLOCKQUOTE>
<P>似乎这个表达式能匹配一个或若干个'abc'，但是如果你真的用它去匹配'abcabcabc'的话，实际上只会找到三个字符。因为这个表达式的意思是'ab'后边跟着一个或多个'c'。要想匹配一个或多个完整的'abc'，你应该这样：</P>
<BLOCKQUOTE><PRE>(abc)+</PRE></BLOCKQUOTE>
<P>正则表达式能轻而易举地把你给耍了；这是一种建立在<SPAN class=original_words>Java</SPAN>之上的新语言。<BR><BR></P>
<P id=header59><STRONG><SPAN class=original_words>Pattern</SPAN>和<SPAN class=original_words>Matcher</SPAN></STRONG></P>
<P>先给一个例子。下面这段程序可以测试正则表达式是否匹配字符串。第一个参数是要匹配的字符串，后面是正则表达式。正则表达式可以有多个。在Unix/Linux环境下，命令行下的正则表达式还必须用引号。</P>
<P>当你创建正则表达式时，可以用这个程序来判断它是不是会按照你的要求工作。</P>
<P>
<TABLE class=sourcecode>
<TBODY>
<TR>
<TD><PRE><FONT color=#009900>//: c12:TestRegularExpression.java</FONT>
<FONT color=#009900>// Allows you to easly try out regular expressions.</FONT>
<FONT color=#009900>// {Args: abcabcabcdefabc "abc+" "(abc)+" "(abc){2,}" }</FONT>
<FONT color=#0000ff>import</FONT> java.util.regex.*;
<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> TestRegularExpression {
  <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) {
    <FONT color=#0000ff>if</FONT>(args.length &lt; 2) {
      System.out.println(<FONT color=#004488>"Usage:\n"</FONT> +
        <FONT color=#004488>"java TestRegularExpression "</FONT> +
        <FONT color=#004488>"characterSequence regularExpression+"</FONT>);
      System.exit(0);
    }
    System.out.println(<FONT color=#004488>"Input: \"</FONT><FONT color=#004488>" + args[0] + "</FONT>\<FONT color=#004488>""</FONT>);
    <FONT color=#0000ff>for</FONT>(<FONT color=#0000ff>int</FONT> i = 1; i &lt; args.length; i++) {
      System.out.println(
        <FONT color=#004488>"Regular expression: \"</FONT><FONT color=#004488>" + args[i] + "</FONT>\<FONT color=#004488>""</FONT>);
      Pattern p = Pattern.compile(args[i]);
      Matcher m = p.matcher(args[0]);
      <FONT color=#0000ff>while</FONT>(m.find()) {
        System.out.println(<FONT color=#004488>"Match \"</FONT>" + m.group() +
          <FONT color=#004488>"\"</FONT> at positions " +
          m.start() + <FONT color=#004488>"-"</FONT> + (m.end() - 1));
      }
    }
  }
} <FONT color=#009900>///:~</FONT></PRE></TD></TR></TBODY></TABLE></P>
<P><SPAN class=original_words>Java</SPAN>的正则表达式是由<SPAN class=original_words>java.util.regex</SPAN>的<SPAN class=original_words>Pattern</SPAN>和<SPAN class=original_words>Matcher</SPAN>类实现的。<SPAN class=original_words>Pattern</SPAN>对象表示经编译的正则表达式。静态的<SPAN class=original_words>compile( )</SPAN>方法负责将表示正则表达式的字符串编译成<SPAN class=original_words>Pattern</SPAN>对象。正如上述例程所示的，只要给<SPAN class=original_words>Pattern</SPAN>的<SPAN class=original_words>matcher( )</SPAN>方法送一个字符串就能获取一个<SPAN class=original_words>Matcher</SPAN>对象。此外，<SPAN class=original_words>Pattern</SPAN>还有一个能快速判断能否在<SPAN class=original_words>input</SPAN>里面找到<SPAN class=original_words>regex</SPAN>的(注意，原文有误，漏了方法名)</P>
<BLOCKQUOTE><PRE><FONT color=#0000ff>static</FONT> <FONT color=#0000ff>boolean</FONT> matches(&nbsp;regex, &nbsp;input)</PRE></BLOCKQUOTE>
<P>以及能返回<SPAN class=original_words>String</SPAN>数组的<SPAN class=original_words>split( )</SPAN>方法，它能用<SPAN class=original_words>regex</SPAN>把字符串分割开来。</P>
<P>只要给<SPAN class=original_words>Pattern.matcher( )</SPAN>方法传一个字符串就能获得<SPAN class=original_words>Matcher</SPAN>对象了。接下来就能用<SPAN class=original_words>Matcher</SPAN>的方法来查询匹配的结果了。</P>
<BLOCKQUOTE><PRE><FONT color=#0000ff>boolean</FONT> matches()
<FONT color=#0000ff>boolean</FONT> lookingAt()
<FONT color=#0000ff>boolean</FONT> find()
<FONT color=#0000ff>boolean</FONT> find(<FONT color=#0000ff>int</FONT> start)</PRE></BLOCKQUOTE>
<P><SPAN class=original_words>matches( )</SPAN>的前提是<SPAN class=original_words>Pattern</SPAN>匹配整个字符串，而<SPAN class=original_words>lookingAt( )</SPAN>的意思是<SPAN class=original_words>Pattern</SPAN>匹配字符串的开头。 </P>
<H4 id=header60>find( )</H4>
<P><SPAN class=original_words>Matcher.find( )</SPAN>的功能是发现<SPAN class=original_words>CharSequence</SPAN>里的，与pattern相匹配的多个字符序列。例如：</P>
<P>
<TABLE class=sourcecode>
<TBODY>
<TR>
<TD><PRE><FONT color=#009900>//: c12:FindDemo.java</FONT>
<FONT color=#0000ff>import</FONT> java.util.regex.*;
<FONT color=#0000ff>import</FONT> com.bruceeckel.simpletest.*;
<FONT color=#0000ff>import</FONT> java.util.*;
<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> FindDemo {
  <FONT color=#0000ff>private</FONT> <FONT color=#0000ff>static</FONT> Test monitor = <FONT color=#0000ff>new</FONT> Test();
  <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) {
    Matcher m = Pattern.compile(<FONT color=#004488>"\\w+"</FONT>)
      .matcher(<FONT color=#004488>"Evening is full of the linnet's wings"</FONT>);
    <FONT color=#0000ff>while</FONT>(m.find())
      System.out.println(m.group());
    <FONT color=#0000ff>int</FONT> i = 0;
    <FONT color=#0000ff>while</FONT>(m.find(i)) {
      System.out.print(m.group() + <FONT color=#004488>" "</FONT>);
      i++;
    }
    monitor.expect(<FONT color=#0000ff>new</FONT> String[] {
      <FONT color=#004488>"Evening"</FONT>,
      <FONT color=#004488>"is"</FONT>,
      <FONT color=#004488>"full"</FONT>,
      <FONT color=#004488>"of"</FONT>,
      <FONT color=#004488>"the"</FONT>,
      <FONT color=#004488>"linnet"</FONT>,
      <FONT color=#004488>"s"</FONT>,
      <FONT color=#004488>"wings"</FONT>,
      <FONT color=#004488>"Evening vening ening ning ing ng g is is s full "</FONT> +
      <FONT color=#004488>"full ull ll l of of f the the he e linnet linnet "</FONT> +
      <FONT color=#004488>"innet nnet net et t s s wings wings ings ngs gs s "</FONT>
    });
  }
} <FONT color=#009900>///:~</FONT></PRE></TD></TR></TBODY></TABLE></P>
<P>"<SPAN class=original_words>\\w+</SPAN>"的意思是"一个或多个单词字符"，因此它会将字符串直接分解成单词。<SPAN class=original_words>find( )</SPAN>像一个迭代器，从头到尾扫描一遍字符串。第二个<SPAN class=original_words>find( )</SPAN>是带<SPAN class=original_words>int</SPAN>参数的，正如你所看到的，它会告诉方法从哪里开始找——即从参数位置开始查找。</P>
<P id=header61><STRONG>Groups</STRONG></P>
<P>Group是指里用括号括起来的，能被后面的表达式调用的正则表达式。Group 0 表示整个表达式，group 1表示第一个被括起来的group，以此类推。所以；</P>
<BLOCKQUOTE><PRE>A(B(C))D</PRE></BLOCKQUOTE>
<P>里面有三个group：group 0是<SPAN class=original_words>ABCD</SPAN>， group 1是<SPAN class=original_words>BC</SPAN>，group 2是<SPAN class=original_words>C</SPAN>。</P>
<P>你可以用下述<SPAN class=original_words>Matcher</SPAN>方法来使用group：</P>
<P><SPAN class=original_words>public int groupCount( )</SPAN>返回matcher对象中的group的数目。不包括group0。</P>
<P><SPAN class=original_words>public String group( ) </SPAN>返回上次匹配操作(比方说<SPAN class=original_words>find( )</SPAN>)的group 0(整个匹配)</P>
<P><SPAN class=original_words>public String group(int i)</SPAN>返回上次匹配操作的某个group。如果匹配成功，但是没能找到group，则返回null。</P>
<P><SPAN class=original_words>public int start(int group)</SPAN>返回上次匹配所找到的，group的开始位置。</P>
<P><SPAN class=original_words>public int end(int group)</SPAN>返回上次匹配所找到的，group的结束位置，最后一个字符的下标加一。</P>
<P>下面我们举一些group的例子：</P>
<P>
<TABLE class=sourcecode>
<TBODY>
<TR>
<TD><PRE><FONT color=#009900>//: c12:Groups.java</FONT>
<FONT color=#0000ff>import</FONT> java.util.regex.*;
<FONT color=#0000ff>import</FONT> com.bruceeckel.simpletest.*;
<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> Groups {
  <FONT color=#0000ff>private</FONT> <FONT color=#0000ff>static</FONT> Test monitor = <FONT color=#0000ff>new</FONT> Test();
  <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>final</FONT> String poem =
    <FONT color=#004488>"Twas brillig, and the slithy toves\n"</FONT> +
    <FONT color=#004488>"Did gyre and gimble in the wabe.\n"</FONT> +
    <FONT color=#004488>"All mimsy were the borogoves,\n"</FONT> +
    <FONT color=#004488>"And the mome raths outgrabe.\n\n"</FONT> +
    <FONT color=#004488>"Beware the Jabberwock, my son,\n"</FONT> +
    <FONT color=#004488>"The jaws that bite, the claws that catch.\n"</FONT> +
    <FONT color=#004488>"Beware the Jubjub bird, and shun\n"</FONT> +
    <FONT color=#004488>"The frumious Bandersnatch."</FONT>;
  <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) {
    Matcher m =
      Pattern.compile(<FONT color=#004488>"(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$"</FONT>)
        .matcher(poem);
    <FONT color=#0000ff>while</FONT>(m.find()) {
      <FONT color=#0000ff>for</FONT>(<FONT color=#0000ff>int</FONT> j = 0; j &lt;= m.groupCount(); j++)
        System.out.print(<FONT color=#004488>"["</FONT> + m.group(j) + <FONT color=#004488>"]"</FONT>);
      System.out.println();
    }
    monitor.expect(<FONT color=#0000ff>new</FONT> String[]{
      <FONT color=#004488>"[the slithy toves]"</FONT> +
      <FONT color=#004488>"[the][slithy toves][slithy][toves]"</FONT>,
      <FONT color=#004488>"[in the wabe.][in][the wabe.][the][wabe.]"</FONT>,
      <FONT color=#004488>"[were the borogoves,]"</FONT> +
      <FONT color=#004488>"[were][the borogoves,][the][borogoves,]"</FONT>,
      <FONT color=#004488>"[mome raths outgrabe.]"</FONT> +
      <FONT color=#004488>"[mome][raths outgrabe.][raths][outgrabe.]"</FONT>,
      <FONT color=#004488>"[Jabberwock, my son,]"</FONT> +
      <FONT color=#004488>"[Jabberwock,][my son,][my][son,]"</FONT>,
      <FONT color=#004488>"[claws that catch.]"</FONT> +
      <FONT color=#004488>"[claws][that catch.][that][catch.]"</FONT>,
      <FONT color=#004488>"[bird, and shun][bird,][and shun][and][shun]"</FONT>,
      <FONT color=#004488>"[The frumious Bandersnatch.][The]"</FONT> +
      <FONT color=#004488>"[frumious Bandersnatch.][frumious][Bandersnatch.]"</FONT>
    });
  }
} <FONT color=#009900>///:~</FONT></PRE></TD></TR></TBODY></TABLE></P>
<P>这首诗是<SPAN class=original_words><CITE>Through the Looking Glass</CITE></SPAN>的，Lewis Carroll的"Jabberwocky"的第一部分。可以看到这个正则表达式里有很多用括号括起来的group，它是由任意多个连续的非空字符('<SPAN class=original_words>\S+</SPAN>')和任意多个连续的空格字符('<SPAN class=original_words>\s+</SPAN>')所组成的，其最终目的是要捕获每行的最后三个单词；'<SPAN class=original_words>$</SPAN>'表示一行的结尾。但是'<SPAN class=original_words>$</SPAN>'通常表示整个字符串的结尾，所以这里要明确地告诉正则表达式注意换行符。这一点是由'<SPAN class=original_words>(?m)</SPAN>'标志完成的(模式标志会过一会讲解)。</P>
<P id=header62><STRONG>start( )和end( )</STRONG></P>
<P>如果匹配成功，<SPAN class=original_words>start( )</SPAN>会返回此次匹配的开始位置，<SPAN class=original_words>end( )</SPAN>会返回此次匹配的结束位置，即最后一个字符的下标加一。如果之前的匹配不成功(或者没匹配)，那么无论是调用<SPAN class=original_words>start( )</SPAN>还是<SPAN class=original_words>end( )</SPAN>，都会引发一个<SPAN class=original_words>IllegalStateException</SPAN>。下面这段程序还演示了<SPAN class=original_words>matches( )</SPAN>和<SPAN class=original_words>lookingAt( )</SPAN>：</P>
<P>
<TABLE class=sourcecode>
<TBODY>
<TR>
<TD><PRE><FONT color=#009900>//: c12:StartEnd.java</FONT>
<FONT color=#0000ff>import</FONT> java.util.regex.*;
<FONT color=#0000ff>import</FONT> com.bruceeckel.simpletest.*;
<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> StartEnd {
  <FONT color=#0000ff>private</FONT> <FONT color=#0000ff>static</FONT> Test monitor = <FONT color=#0000ff>new</FONT> Test();
  <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) {
    String[] input = <FONT color=#0000ff>new</FONT> String[] {
      <FONT color=#004488>"Java has regular expressions in 1.4"</FONT>,
      <FONT color=#004488>"regular expressions now expressing in Java"</FONT>,
      <FONT color=#004488>"Java represses oracular expressions"</FONT>
    };
    Pattern
      p1 = Pattern.compile(<FONT color=#004488>"re\\w*"</FONT>),
      p2 = Pattern.compile(<FONT color=#004488>"Java.*"</FONT>);
    <FONT color=#0000ff>for</FONT>(<FONT color=#0000ff>int</FONT> i = 0; i &lt; input.length; i++) {
      System.out.println(<FONT color=#004488>"input "</FONT> + i + <FONT color=#004488>": "</FONT> + input[i]);
      Matcher
        m1 = p1.matcher(input[i]),
        m2 = p2.matcher(input[i]);
      <FONT color=#0000ff>while</FONT>(m1.find())
        System.out.println(<FONT color=#004488>"m1.find() '"</FONT> + m1.group() +
          <FONT color=#004488>"' start = "</FONT>+ m1.start() + <FONT color=#004488>" end = "</FONT> + m1.end());
      <FONT color=#0000ff>while</FONT>(m2.find())
        System.out.println(<FONT color=#004488>"m2.find() '"</FONT> + m2.group() +
          <FONT color=#004488>"' start = "</FONT>+ m2.start() + <FONT color=#004488>" end = "</FONT> + m2.end());
      <FONT color=#0000ff>if</FONT>(m1.lookingAt()) <FONT color=#009900>// No reset() necessary</FONT>
        System.out.println(<FONT color=#004488>"m1.lookingAt() start = "</FONT>
          + m1.start() + <FONT color=#004488>" end = "</FONT> + m1.end());
      <FONT color=#0000ff>if</FONT>(m2.lookingAt())
        System.out.println(<FONT color=#004488>"m2.lookingAt() start = "</FONT>
          + m2.start() + <FONT color=#004488>" end = "</FONT> + m2.end());
      <FONT color=#0000ff>if</FONT>(m1.matches()) <FONT color=#009900>// No reset() necessary</FONT>
        System.out.println(<FONT color=#004488>"m1.matches() start = "</FONT>
          + m1.start() + <FONT color=#004488>" end = "</FONT> + m1.end());
      <FONT color=#0000ff>if</FONT>(m2.matches())
        System.out.println(<FONT color=#004488>"m2.matches() start = "</FONT>
          + m2.start() + <FONT color=#004488>" end = "</FONT> + m2.end());
    }
    monitor.expect(<FONT color=#0000ff>new</FONT> String[] {
      <FONT color=#004488>"input 0: Java has regular expressions in 1.4"</FONT>,
      <FONT color=#004488>"m1.find() 'regular' start = 9 end = 16"</FONT>,
      <FONT color=#004488>"m1.find() 'ressions' start = 20 end = 28"</FONT>,
      <FONT color=#004488>"m2.find() 'Java has regular expressions in 1.4'"</FONT> +
      <FONT color=#004488>" start = 0 end = 35"</FONT>,
      <FONT color=#004488>"m2.lookingAt() start = 0 end = 35"</FONT>,
      <FONT color=#004488>"m2.matches() start = 0 end = 35"</FONT>,
      <FONT color=#004488>"input 1: regular expressions now "</FONT> +
      <FONT color=#004488>"expressing in Java"</FONT>,
      <FONT color=#004488>"m1.find() 'regular' start = 0 end = 7"</FONT>,
      <FONT color=#004488>"m1.find() 'ressions' start = 11 end = 19"</FONT>,
      <FONT color=#004488>"m1.find() 'ressing' start = 27 end = 34"</FONT>,
      <FONT color=#004488>"m2.find() 'Java' start = 38 end = 42"</FONT>,
      <FONT color=#004488>"m1.lookingAt() start = 0 end = 7"</FONT>,
      <FONT color=#004488>"input 2: Java represses oracular expressions"</FONT>,
      <FONT color=#004488>"m1.find() 'represses' start = 5 end = 14"</FONT>,
      <FONT color=#004488>"m1.find() 'ressions' start = 27 end = 35"</FONT>,
      <FONT color=#004488>"m2.find() 'Java represses oracular expressions' "</FONT> +
      <FONT color=#004488>"start = 0 end = 35"</FONT>,
      <FONT color=#004488>"m2.lookingAt() start = 0 end = 35"</FONT>,
      <FONT color=#004488>"m2.matches() start = 0 end = 35"</FONT>
    });
  }
} <FONT color=#009900>///:~</FONT></PRE></TD></TR></TBODY></TABLE></P>
<P>注意，只要字符串里有这个模式，<SPAN class=original_words>find( )</SPAN>就能把它给找出来，但是<SPAN class=original_words>lookingAt( )</SPAN>和<SPAN class=original_words>matches( )</SPAN>，只有在字符串与正则表达式一开始就相匹配的情况下才能返回<SPAN class=original_words>true</SPAN>。<SPAN class=original_words>matches( )</SPAN>成功的前提是正则表达式与字符串完全匹配，而<SPAN class=original_words>lookingAt( )</SPAN><A id=ref67 href="mk:@MSITStore:C:\Documents%20and%20Settings\wangkai\桌面\TIJ3_cn.chm::/chap12/nocomment.html#comment67"><SUP>[67]</SUP></A>成功的前提是，字符串的开始部分与正则表达式相匹配。</P>
<P id=header63><STRONG>匹配的模式(Pattern flags)</STRONG></P>
<P><SPAN class=original_words>compile( )</SPAN>方法还有一个版本，它需要一个控制正则表达式的匹配行为的参数：</P>
<BLOCKQUOTE><PRE>Pattern Pattern.compile(String regex, <FONT color=#0000ff>int</FONT> flag)</PRE></BLOCKQUOTE>
<P><SPAN class=original_words>flag</SPAN>的取值范围如下： 
<TABLE class=narration cellSpacing=0 borderColorDark=#000000 cellPadding=2 borderColorLight=#000000 border=2>
<TBODY>
<TR>
<TH><SPAN class=original_words>编译标志</SPAN> </TH>
<TH><SPAN class=original_words>效果</SPAN> </TH></TR>
<TR>
<TD><SPAN class=original_words>Pattern.CANON_EQ</SPAN> </TD>
<TD>当且仅当两个字符的"正规分解(canonical decomposition)"都完全相同的情况下，才认定匹配。比如用了这个标志之后，表达式"a\u030A"会匹配"?"。默认情况下，不考虑"规范相等性(canonical equivalence)"。 </TD></TR>
<TR>
<TD><SPAN class=original_words>Pattern.CASE_INSENSITIVE<BR>(?i)</SPAN> </TD>
<TD>默认情况下，大小写不明感的匹配只适用于US-ASCII字符集。这个标志能让表达式忽略大小写进行匹配。要想对Unicode字符进行大小不明感的匹配，只要将<SPAN class=original_words>UNICODE_CASE</SPAN>与这个标志合起来就行了。 </TD></TR>
<TR>
<TD><SPAN class=original_words>Pattern.COMMENTS<BR>(?x)</SPAN> </TD>
<TD>在这种模式下，匹配时会忽略(正则表达式里的)空格字符(译者注：不是指表达式里的"\\s"，而是指表达式里的空格，tab，回车之类)。注释从#开始，一直到这行结束。可以通过嵌入式的标志来启用Unix行模式。 </TD></TR>
<TR>
<TD><SPAN class=original_words>Pattern.DOTALL<BR>(?s)</SPAN> </TD>
<TD>在这种模式下，表达式'.'可以匹配任意字符，包括表示一行的结束符。默认情况下，表达式'.'不匹配行的结束符。 </TD></TR>
<TR>
<TD><SPAN class=original_words>Pattern.MULTILINE<BR>(?m)</SPAN> </TD>
<TD>在这种模式下，'^'和'$'分别匹配一行的开始和结束。此外，'^'仍然匹配字符串的开始，'$'也匹配字符串的结束。默认情况下，这两个表达式仅仅匹配字符串的开始和结束。 </TD></TR>
<TR>
<TD><SPAN class=original_words>Pattern.UNICODE_CASE<BR>(?u)</SPAN> </TD>
<TD>在这个模式下，如果你还启用了<SPAN class=original_words>CASE_INSENSITIVE</SPAN>标志，那么它会对Unicode字符进行大小写不明感的匹配。默认情况下，大小写不明感的匹配只适用于US-ASCII字符集。 </TD></TR>
<TR>
<TD><SPAN class=original_words>Pattern.UNIX_LINES<BR>(?d)</SPAN> </TD>
<TD>在这个模式下，只有'\n'才被认作一行的中止，并且与'.'，'^'，以及'$'进行匹配。 </TD></TR></TBODY></TABLE></P>
<P>在这些标志里面，<SPAN class=original_words>Pattern.CASE_INSENSITIVE</SPAN>，<SPAN class=original_words>Pattern.MULTILINE</SPAN>，以及<SPAN class=original_words>Pattern.COMMENTS</SPAN>是最有用的(其中<SPAN class=original_words>Pattern.COMMENTS</SPAN>还能帮我们把思路理清楚，并且/或者做文档)。注意，你可以用在表达式里插记号的方式来启用绝大多数的模式。这些记号就在上面那张表的各个标志的下面。你希望模式从哪里开始启动，就在哪里插记号。</P>
<P>可以用"OR" ('|')运算符把这些标志合使用：</P>
<P>
<TABLE class=sourcecode>
<TBODY>
<TR>
<TD><PRE><FONT color=#009900>//: c12:ReFlags.java</FONT>
<FONT color=#0000ff>import</FONT> java.util.regex.*;
<FONT color=#0000ff>import</FONT> com.bruceeckel.simpletest.*;
<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> ReFlags {
  <FONT color=#0000ff>private</FONT> <FONT color=#0000ff>static</FONT> Test monitor = <FONT color=#0000ff>new</FONT> Test();
  <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) {
    Pattern p =  Pattern.compile(<FONT color=#004488>"^java"</FONT>,
      Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    Matcher m = p.matcher(
      <FONT color=#004488>"java has regex\nJava has regex\n"</FONT> +
      <FONT color=#004488>"JAVA has pretty good regular expressions\n"</FONT> +
      <FONT color=#004488>"Regular expressions are in Java"</FONT>);
    <FONT color=#0000ff>while</FONT>(m.find())
      System.out.println(m.group());
    monitor.expect(<FONT color=#0000ff>new</FONT> String[] {
      <FONT color=#004488>"java"</FONT>,
      <FONT color=#004488>"Java"</FONT>,
      <FONT color=#004488>"JAVA"</FONT>
    });
  }
} <FONT color=#009900>///:~</FONT></PRE></TD></TR></TBODY></TABLE></P>
<P>这样创建出来的正则表达式就能匹配以"java"，"Java"，"JAVA"...开头的字符串了。此外，如果字符串分好几行，那它还会对每一行做匹配(匹配始于字符序列的开始，终于字符序列当中的行结束符)。注意，<SPAN class=original_words>group( )</SPAN>方法仅返回匹配的部分。</P>
<P id=header64><STRONG>split( )</STRONG></P>
<P>所谓分割是指将以正则表达式为界，将字符串分割成<SPAN class=original_words>String</SPAN>数组。</P>
<BLOCKQUOTE><PRE>String[] split(CharSequence charseq)
String[] split(CharSequence charseq, <FONT color=#0000ff>int</FONT> limit)</PRE></BLOCKQUOTE>
<P>这是一种既快又方便地将文本根据一些常见的边界标志分割开来的方法。</P>
<P>
<TABLE class=code>
<TBODY>
<TR>
<TD><PRE><FONT color=#009900>//: c12:SplitDemo.java</FONT>
<FONT color=#0000ff>import</FONT> java.util.regex.*;
<FONT color=#0000ff>import</FONT> com.bruceeckel.simpletest.*;
<FONT color=#0000ff>import</FONT> java.util.*;
<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> SplitDemo {
  <FONT color=#0000ff>private</FONT> <FONT color=#0000ff>static</FONT> Test monitor = <FONT color=#0000ff>new</FONT> Test();
  <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) {
    String input =
      <FONT color=#004488>"This!!unusual use!!of exclamation!!points"</FONT>;
    System.out.println(Arrays.asList(
      Pattern.compile(<FONT color=#004488>"!!"</FONT>).split(input)));
    <FONT color=#009900>// Only do the first three:</FONT>
    System.out.println(Arrays.asList(
      Pattern.compile(<FONT color=#004488>"!!"</FONT>).split(input, 3)));
    System.out.println(Arrays.asList(
      <FONT color=#004488>"Aha! String has a split() built in!"</FONT>.split(<FONT color=#004488>" "</FONT>)));
    monitor.expect(<FONT color=#0000ff>new</FONT> String[] {
      <FONT color=#004488>"[This, unusual use, of exclamation, points]"</FONT>,
      <FONT color=#004488>"[This, unusual use, of exclamation!!points]"</FONT>,
      <FONT color=#004488>"[Aha!, String, has, a, split(), built, in!]"</FONT>
    });
  }
} <FONT color=#009900>///:~</FONT></PRE></TD></TR></TBODY></TABLE></P>
<P>第二个<SPAN class=original_words>split( )</SPAN>会限定分割的次数。</P>
<P>正则表达式是如此重要，以至于有些功能被加进了<SPAN class=original_words>String</SPAN>类，其中包括<SPAN class=original_words>split( )</SPAN>(已经看到了)，<SPAN class=original_words>matches( )</SPAN>，<SPAN class=original_words>replaceFirst( )</SPAN>以及<SPAN class=original_words>replaceAll( )</SPAN>。这些方法的功能同<SPAN class=original_words>Pattern</SPAN>和<SPAN class=original_words>Matcher</SPAN>的相同。 </P>
<P id=header65><STRONG>替换操作</STRONG></P>
<P>正则表达式在替换文本方面特别在行。下面就是一些方法：</P>
<P><SPAN class=original_words>replaceFirst(String replacement)</SPAN>将字符串里，第一个与模式相匹配的子串替换成<SPAN class=original_words>replacement</SPAN>。 </P>
<P><SPAN class=original_words>replaceAll(String replacement)</SPAN>，将输入字符串里所有与模式相匹配的子串全部替换成<SPAN class=original_words>replacement</SPAN>。</P>
<P><SPAN class=original_words>appendReplacement(StringBuffer sbuf, String replacement)</SPAN>对<SPAN class=original_words>sbuf</SPAN>进行逐次替换，而不是像<SPAN class=original_words>replaceFirst( )</SPAN>或<SPAN class=original_words>replaceAll( )</SPAN>那样，只替换第一个或全部子串。这是个非常重要的方法，因为它可以调用方法来生成<SPAN class=original_words>replacement</SPAN>(<SPAN class=original_words>replaceFirst( )</SPAN>和<SPAN class=original_words>replaceAll( )</SPAN>只允许用固定的字符串来充当<SPAN class=original_words>replacement</SPAN>)。有了这个方法，你就可以编程区分group，从而实现更强大的替换功能。</P>
<P>调用完<SPAN class=original_words>appendReplacement( )</SPAN>之后，为了把剩余的字符串拷贝回去，必须调用<SPAN class=original_words>appendTail(StringBuffer sbuf, String replacement)</SPAN>。 </P>
<P>下面我们来演示一下怎样使用这些替换方法。说明一下，这段程序所处理的字符串是它自己开头部分的注释，是用正则表达式提取出来并加以处理之后再传给替换方法的。</P>
<P>
<TABLE class=sourcecode>
<TBODY>
<TR>
<TD><PRE><FONT color=#009900>//: c12:TheReplacements.java</FONT>
<FONT color=#0000ff>import</FONT> java.util.regex.*;
<FONT color=#0000ff>import</FONT> java.io.*;
<FONT color=#0000ff>import</FONT> com.bruceeckel.util.*;
<FONT color=#0000ff>import</FONT> com.bruceeckel.simpletest.*;
<FONT color=#009900>/*! Here's a block of text to use as input to
    the regular expression matcher. Note that we'll
    first extract the block of text by looking for
    the special delimiters, then process the
    extracted block. !*/</FONT>
<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> TheReplacements {
  <FONT color=#0000ff>private</FONT> <FONT color=#0000ff>static</FONT> Test monitor = <FONT color=#0000ff>new</FONT> Test();
  <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) <FONT color=#0000ff>throws</FONT> Exception {
    String s = TextFile.read(<FONT color=#004488>"TheReplacements.java"</FONT>);
    <FONT color=#009900>// Match the specially-commented block of text above:</FONT>
    Matcher mInput =
      Pattern.compile(<FONT color=#004488>"</FONT><FONT color=#004488>/\\*!(.*)!\\*</FONT><FONT color=#004488>/"</FONT>, Pattern.DOTALL)
        .matcher(s);
    <FONT color=#0000ff>if</FONT>(mInput.find())
      s = mInput.group(1); <FONT color=#009900>// Captured by parentheses</FONT>
    <FONT color=#009900>// Replace two or more spaces with a single space:</FONT>
    s = s.replaceAll(<FONT color=#004488>" {2,}"</FONT>, <FONT color=#004488>" "</FONT>);
    <FONT color=#009900>// Replace one or more spaces at the beginning of each</FONT>
    <FONT color=#009900>// line with no spaces. Must enable MULTILINE mode:</FONT>
    s = s.replaceAll(<FONT color=#004488>"(?m)^ +"</FONT>, <FONT color=#004488>""</FONT>);
    System.out.println(s);
    s = s.replaceFirst(<FONT color=#004488>"[aeiou]"</FONT>, <FONT color=#004488>"(VOWEL1)"</FONT>);
    StringBuffer sbuf = <FONT color=#0000ff>new</FONT> StringBuffer();
    Pattern p = Pattern.compile(<FONT color=#004488>"[aeiou]"</FONT>);
    Matcher m = p.matcher(s);
    <FONT color=#009900>// Process the find information as you</FONT>
    <FONT color=#009900>// perform the replacements:</FONT>
    <FONT color=#0000ff>while</FONT>(m.find())
      m.appendReplacement(sbuf, m.group().toUpperCase());
    <FONT color=#009900>// Put in the remainder of the text:</FONT>
    m.appendTail(sbuf);
    System.out.println(sbuf);
    monitor.expect(<FONT color=#0000ff>new</FONT> String[]{
      <FONT color=#004488>"Here's a block of text to use as input to"</FONT>,
      <FONT color=#004488>"the regular expression matcher. Note that we'll"</FONT>,
      <FONT color=#004488>"first extract the block of text by looking for"</FONT>,
      <FONT color=#004488>"the special delimiters, then process the"</FONT>,
      <FONT color=#004488>"extracted block. "</FONT>,
      <FONT color=#004488>"H(VOWEL1)rE's A blOck Of tExt tO UsE As InpUt tO"</FONT>,
      <FONT color=#004488>"thE rEgUlAr ExprEssIOn mAtchEr. NOtE thAt wE'll"</FONT>,
      <FONT color=#004488>"fIrst ExtrAct thE blOck Of tExt by lOOkIng fOr"</FONT>,
      <FONT color=#004488>"thE spEcIAl dElImItErs, thEn prOcEss thE"</FONT>,
      <FONT color=#004488>"ExtrActEd blOck. "</FONT>
    });
  }
} <FONT color=#009900>///:~</FONT></PRE></TD></TR></TBODY></TABLE></P>
<P>我们用前面介绍的<SPAN class=original_words>TextFile.read( )</SPAN>方法来打开和读取文件。<SPAN class=original_words>mInput</SPAN>的功能是匹配'<SPAN class=original_words>/*!</SPAN>' 和 '<SPAN class=original_words>!*/</SPAN>' 之间的文本(注意一下分组用的括号)。接下来，我们将所有两个以上的连续空格全都替换成一个，并且将各行开头的空格全都去掉(为了让这个正则表达式能对所有的行，而不仅仅是第一行起作用，必须启用多行模式)。这两个操作都用了<SPAN class=original_words>String</SPAN>的<SPAN class=original_words>replaceAll( )</SPAN>(这里用它更方便)。注意，由于每个替换只做一次，因此除了预编译<SPAN class=original_words>Pattern</SPAN>之外，程序没有额外的开销。</P>
<P><SPAN class=original_words>replaceFirst( )</SPAN>只替换第一个子串。此外，<SPAN class=original_words>replaceFirst( )</SPAN>和<SPAN class=original_words>replaceAll( )</SPAN>只能用常量(literal)来替换，所以如果你每次替换的时候还要进行一些操作的话，它们是无能为力的。碰到这种情况，你得用<SPAN class=original_words>appendReplacement( )</SPAN>，它能让你在进行替换的时候想写多少代码就写多少。在上面那段程序里，创建<SPAN class=original_words>sbuf</SPAN>的过程就是选group做处理，也就是用正则表达式把元音字母找出来，然后换成大写的过程。通常你得在完成全部的替换之后才调用<SPAN class=original_words>appendTail( )</SPAN>，但是如果要模仿<SPAN class=original_words>replaceFirst( )</SPAN>(或"replace n")的效果，你也可以只替换一次就调用<SPAN class=original_words>appendTail( )</SPAN>。它会把剩下的东西全都放进<SPAN class=original_words>sbuf</SPAN>。</P>
<P>你还可以在<SPAN class=original_words>appendReplacement( )</SPAN>的<SPAN class=original_words>replacement</SPAN>参数里用"$g"引用已捕获的group，其中'g' 表示group的号码。不过这是为一些比较简单的操作准备的，因而其效果无法与上述程序相比。</P>
<P id=header66><STRONG>reset( )</STRONG></P>
<P>此外，还可以用<SPAN class=original_words>reset( )</SPAN>方法给现有的<SPAN class=original_words>Matcher</SPAN>对象配上个新的<SPAN class=original_words>CharSequence</SPAN>。</P>
<P>
<TABLE class=code>
<TBODY>
<TR>
<TD><PRE><FONT color=#009900>//: c12:Resetting.java</FONT>
<FONT color=#0000ff>import</FONT> java.util.regex.*;
<FONT color=#0000ff>import</FONT> java.io.*;
<FONT color=#0000ff>import</FONT> com.bruceeckel.simpletest.*;
<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> Resetting {
  <FONT color=#0000ff>private</FONT> <FONT color=#0000ff>static</FONT> Test monitor = <FONT color=#0000ff>new</FONT> Test();
  <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) <FONT color=#0000ff>throws</FONT> Exception {
    Matcher m = Pattern.compile(<FONT color=#004488>"[frb][aiu][gx]"</FONT>)
      .matcher(<FONT color=#004488>"fix the rug with bags"</FONT>);
    <FONT color=#0000ff>while</FONT>(m.find())
      System.out.println(m.group());
    m.reset(<FONT color=#004488>"fix the rig with rags"</FONT>);
    <FONT color=#0000ff>while</FONT>(m.find())
      System.out.println(m.group());
    monitor.expect(<FONT color=#0000ff>new</FONT> String[]{
      <FONT color=#004488>"fix"</FONT>,
      <FONT color=#004488>"rug"</FONT>,
      <FONT color=#004488>"bag"</FONT>,
      <FONT color=#004488>"fix"</FONT>,
      <FONT color=#004488>"rig"</FONT>,
      <FONT color=#004488>"rag"</FONT>
    });
  }
} <FONT color=#009900>///:~</FONT></PRE></TD></TR></TBODY></TABLE></P>
<P>如果不给参数，<SPAN class=original_words>reset( )</SPAN>会把<SPAN class=original_words>Matcher</SPAN>设到当前字符串的开始处。</P>
<P id=header67><STRONG>正则表达式与Java I/O</STRONG></P>
<P>到目前为止，你看到的都是用正则表达式处理静态字符串的例子。下面我们来演示一下怎样用正则表达式扫描文件并且找出匹配的字符串。受Unix的grep启发，我写了个<SPAN class=original_words>JGrep.java</SPAN>，它需要两个参数：文件名，以及匹配字符串用的正则表达式。它会把匹配这个正则表达式那部分内容及其所属行的行号打印出来。</P>
<P>
<TABLE class=code>
<TBODY>
<TR>
<TD><PRE><FONT color=#009900>//: c12:JGrep.java</FONT>
<FONT color=#009900>// A very simple version of the "grep" program.</FONT>
<FONT color=#009900>// {Args: JGrep.java "\\b[Ssct]\\w+"}</FONT>
<FONT color=#0000ff>import</FONT> java.io.*;
<FONT color=#0000ff>import</FONT> java.util.regex.*;
<FONT color=#0000ff>import</FONT> java.util.*;
<FONT color=#0000ff>import</FONT> com.bruceeckel.util.*;
<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> JGrep {
  <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) <FONT color=#0000ff>throws</FONT> Exception {
    <FONT color=#0000ff>if</FONT>(args.length &lt; 2) {
      System.out.println(<FONT color=#004488>"Usage: java JGrep file regex"</FONT>);
      System.exit(0);
    }
    Pattern p = Pattern.compile(args[1]);
    <FONT color=#009900>// Iterate through the lines of the input file:</FONT>
    ListIterator it = <FONT color=#0000ff>new</FONT> TextFile(args[0]).listIterator();
    <FONT color=#0000ff>while</FONT>(it.hasNext()) {
      Matcher m = p.matcher((String)it.next());
      <FONT color=#0000ff>while</FONT>(m.find())
        System.out.println(it.nextIndex() + <FONT color=#004488>": "</FONT> +
          m.group() + <FONT color=#004488>": "</FONT> + m.start());
    }
  }
} <FONT color=#009900>///:~</FONT></PRE></TD></TR></TBODY></TABLE></P>
<P>文件是用<SPAN class=original_words>TextFile</SPAN>打开的(本章的前半部分讲的)。由于<SPAN class=original_words>TextFile</SPAN>会把文件的各行放在<SPAN class=original_words>ArrayList</SPAN>里面，而我们又提取了一个<SPAN class=original_words>ListIterator</SPAN>，因此我们可以在文件的各行当中自由移动(既能向前也可以向后)。 </P>
<P>每行都会有一个<SPAN class=original_words>Matcher</SPAN>，然后用<SPAN class=original_words>find( )</SPAN>扫描。注意，我们用<SPAN class=original_words>ListIterator.nextIndex( )</SPAN>跟踪行号。 </P>
<P>测试参数是<SPAN class=original_words>JGrep.java</SPAN>和以<SPAN class=original_words>[Ssct]</SPAN>开头的单词。</P>
<P id=header68><STRONG>还需要StringTokenizer吗?</STRONG></P>
<P>看到正则表达式能提供这么强大的功能，你可能会怀疑，是不是还需要原先的<SPAN class=original_words>StringTokenizer</SPAN>。JDK 1.4以前，要想分割字符串，只有用<SPAN class=original_words>StringTokenizer</SPAN>。但现在，有了正则表达式之后，它就能做得更干净利索了。</P>
<P>
<TABLE class=sourcecode>
<TBODY>
<TR>
<TD><PRE><FONT color=#009900>//: c12:ReplacingStringTokenizer.java</FONT>
<FONT color=#0000ff>import</FONT> java.util.regex.*;
<FONT color=#0000ff>import</FONT> com.bruceeckel.simpletest.*;
<FONT color=#0000ff>import</FONT> java.util.*;
<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> ReplacingStringTokenizer {
  <FONT color=#0000ff>private</FONT> <FONT color=#0000ff>static</FONT> Test monitor = <FONT color=#0000ff>new</FONT> Test();
  <FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) {
    String input = <FONT color=#004488>"But I'm not dead yet! I feel happy!"</FONT>;
    StringTokenizer stoke = <FONT color=#0000ff>new</FONT> StringTokenizer(input);
    <FONT color=#0000ff>while</FONT>(stoke.hasMoreElements())
      System.out.println(stoke.nextToken());
    System.out.println(Arrays.asList(input.split(<FONT color=#004488>" "</FONT>)));
    monitor.expect(<FONT color=#0000ff>new</FONT> String[] {
      <FONT color=#004488>"But"</FONT>,
      <FONT color=#004488>"I'm"</FONT>,
      <FONT color=#004488>"not"</FONT>,
      <FONT color=#004488>"dead"</FONT>,
      <FONT color=#004488>"yet!"</FONT>,
      <FONT color=#004488>"I"</FONT>,
      <FONT color=#004488>"feel"</FONT>,
      <FONT color=#004488>"happy!"</FONT>,
      <FONT color=#004488>"[But, I'm, not, dead, yet!, I, feel, happy!]"</FONT>
    });
  }
} <FONT color=#009900>///:~</FONT></PRE></TD></TR></TBODY></TABLE></P>
<P>有了正则表达式，你就能用更复杂的模式将字符串分割开来——要是交给<SPAN class=original_words>StringTokenizer</SPAN>的话，事情会麻烦得多。我可以很有把握地说，正则表达式可以取代<SPAN class=original_words>StringTokenizer</SPAN>。 </P>
<P>要想进一步学习正则表达式，建议你看<CITE><SPAN class=original_words>Mastering Regular Expression, 2nd Edition</SPAN></CITE>，作者Jeffrey E. F. Friedl (O’Reilly, 2002)。</P>
<P id=header69><STRONG>总结</STRONG></P>
<P>Java的I/O流类库应该能满足你的基本需求：你可以用它来读写控制台，文件，内存，甚至是Internet。你还可以利用继承来创建新的输入和输出类型。你甚至可以利用Java会自动调用对象的<SPAN class=original_words>toString( )</SPAN>方法的特点(Java仅有的"自动类型转换")，通过重新定义这个方法，来对要传给流的对象做一个简单的扩展。</P>
<P>但是Java的I/O流类库及其文档还是留下了一些缺憾。比方说你打开一个文件往里面写东西，但是这个文件已经有了，这么做会把原先的内容给覆盖了 。这时要是能有一个异常就好了——有些编程语言能让你规定只能往新建的文件里输出。看来Java是要你用<SPAN class=original_words>File</SPAN>对象来判断文件是否存在，因为如果你用<SPAN class=original_words>FileOutputStream</SPAN>或<SPAN class=original_words>FileWriter</SPAN>的话，文件就会被覆盖了。</P>
<P>我对I/O流类库的评价是比较矛盾的；它确实能干很多事情，而且做到了跨平台。但是如果你不懂decorator模式，就会觉得这种设计太难理解了，所以无论是对老师还是学生，都得多花精力。此外这个类库也不完整，否则我也用不着去写<SPAN class=original_words>TextFile</SPAN>了。此外它没有提供格式化输出的功能，而其他语言都已经提供了这种功能。</P>
<P>但是，一旦你真正理解了decorator模式，并且能开始灵活运用这个类库的时候，你就能感受到这种设计的好处了。这时多写几行代码就算不了什么了。 </P>
<P>如果你觉得不解渴(本章只是做个介绍，没想要面面俱到)，可以去看Elliotte Rusty Harold 写的<CITE>Java I/O</CITE> (O’Reilly, 1999)。这本书讲得更深。</P><img src ="http://www.blogjava.net/bluesky/aggbug/18610.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-07 17:06 <a href="http://www.blogjava.net/bluesky/articles/18610.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对象和对象变量</title><link>http://www.blogjava.net/bluesky/articles/18597.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Mon, 07 Nov 2005 08:15:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/18597.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/18597.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/18597.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/18597.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/18597.html</trackback:ping><description><![CDATA[<STRONG>对象和类</STRONG> <BR>面向对象程序设计(OOP)，类是创建对象的模板或蓝图，从类中构造一个对象，即创建了这个类的一个实例(instance)。 <BR>面向对象中，封装(encapsulation)是把数据和行为结合在一个包中，并对对象的使用者隐藏数据的实现过程。 <BR>对象中的数据称为对象的实例字段(instance field)，操作数据的函数和过程称为对象的方法(method)。实现封装的 <BR>关键在于决不让方法直接访问其他类的实例字段。程序应该只能通过对象的方法和对象的数据交互。OOP的另一个原则， <BR>继承性(inheritance)，使得很容易在java中编写自己的类. <BR><BR><STRONG>对象</STRONG> <BR>要使用OOP，需要了解对象的三个主要特性，分别是 <BR>对象的行为(behavior) 对象能做什么，或者说，能对对象施加什么方法 <BR>对象的状态(state) 对对象施加方法时，对象如何反应 <BR>对象的身份(identity) 对象如何与具有相同行为、状态的其他对象相互区别 <BR>类之间的关系 <BR>依赖("use-a") 在A类中的方法操作了B类对象，那么A类依赖于B类 <BR>聚合("has-a") A类对象包含B类对象，还包含C类对象等等，聚合关系意味着类A的对象包含类B的对象 <BR>继承("is-a") 继承关系用来表示更特殊的和更一般的类之间的关系 <BR><BR><STRONG>对象和对象变量</STRONG> <BR>要使用对象，使用构造器(constructor)来构造新的实例。构造器是一个专门的方法，构造器名称和类名相同 <BR>因此Date类的构造器名为Date。要构造Date对象，只要在构造器前面加上new运算符就行了，如new Date() <BR>这个表达式即构造了一个新的对象，通常我们需要把对象存储在一个变量中 Date birthday = new Date(); <BR>birthday是一个对象变量，对象变量(birthday)指向新构造的对象(new Date)。对象变量并不包含对象， <BR>它只是指向一个对象。在java中，任何对象变量的值都是指向存储在别处的对象的一个引用。new 运算符的返回 <BR>值也是一个引用。<img src ="http://www.blogjava.net/bluesky/aggbug/18597.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-07 16:15 <a href="http://www.blogjava.net/bluesky/articles/18597.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>IO读取文件编码是utf-8的文件</title><link>http://www.blogjava.net/bluesky/articles/18588.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Mon, 07 Nov 2005 07:51:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/18588.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/18588.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/18588.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/18588.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/18588.html</trackback:ping><description><![CDATA[<P>import java.io.BufferedReader;<BR>import java.io.File;<BR>import java.io.FileInputStream;<BR>import java.io.FileReader;<BR>import java.io.IOException;<BR>import java.io.InputStreamReader;</P>
<P>public class ReadFile {<BR>       public static void main(String[] args) throws IOException{<BR>              File file = new File("D:\\test.txt");<BR>              FileReader fileReader = new FileReader(file);<BR>              String sx = fileReader.getEncoding();<BR>              FileInputStream fileInputStream = new FileInputStream(file);<BR>              InputStreamReader inputStramReader = new InputStreamReader(fileInputStream, "utf-8");<BR>              BufferedReader bufferedReader = new BufferedReader(inputStramReader);<BR>              String fileContent = "";<BR>              String line = null;<BR>              while ((line = bufferedReader.readLine()) != null) {<BR>                     fileContent += line + "\n";<BR>              }<BR>       }</P>
<P>}<BR></P><img src ="http://www.blogjava.net/bluesky/aggbug/18588.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-07 15:51 <a href="http://www.blogjava.net/bluesky/articles/18588.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>代码中通过代理访问外部资源（转贴）</title><link>http://www.blogjava.net/bluesky/articles/18586.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Mon, 07 Nov 2005 07:44:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/18586.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/18586.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/18586.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/18586.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/18586.html</trackback:ping><description><![CDATA[<A href="http://spaces.msn.com/members/savagardn/Blog/cns!1pzu-0W6-WxMGeUbOUArh6Eg!105.entry">http://spaces.msn.com/members/savagardn/Blog/cns!1pzu-0W6-WxMGeUbOUArh6Eg!105.entry</A><BR><BR>
<DIV><SPAN><FONT size=+0>目前网络上最流行的协议就是HTTP协议。HTTP协议有许多优点，例如它能够穿越防火墙。同时HTTP也是很多其他协议的基础，例如SOAP协议就是建立在HTTP协议之上的。 <BR><BR>Java通过两种API对HTTP提供支持，一种是servlet API，它覆盖了服务器端的编程问题；另一种是java.net包，它通过HttpURLConnection类在客户端提供了对HTTP协议的支持。但是我在使用servlet API的时候曾经遇到过一些问题。本文将介绍我曾遇到过的一些问题和相应的解决办法。 <BR><BR><FONT color=#000099><STRONG>基础知识</STRONG></FONT><BR><BR>通常在企业网络中，一个终端通过代理服务器同互联网连接起来，代理服务器负责监视网络流量和执行安全规则。在java.net API中，软件可以通过两个属性来支持代理服务器，它们分别是http.proxyHost和http.proxyPort。它们必须被设定为相应的代理服务器和端口，下面的代码展示了如何设定这两个属性： <BR><BR>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD bgColor=#e6e6e6><PRE>String url = "http://www.digitalcq.com/",
   proxy = "proxy.digitalcq.com",
   port = "8080";
URL server = new URL(url);
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);
HttpURLConnection connection = (
    HttpURLConnection)server.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
readResponse(in);</PRE></TD></TR></TBODY></TABLE><BR><BR>在上面的程序中，你需要根据实际情况设定代理服务器和端口。如果你不知道该如何设置的话，可以询问你们公司的网络管理员。 <BR><BR><FONT color=#000099><STRONG>使用验证</STRONG></FONT><BR><BR>通常公司会要求员工在连接到互联网之前登录到代理服务器。通过登录这种机制使公司可以更好的监控员工对互联网的使用，例如监控员工都访问了哪些网站。HttpURLConnection通过验证类支持代理服务器验证。下面是一个如何利用HttpURLConnection类进行验证的例子。首先需要实现一个验证者： <BR><BR>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD bgColor=#e6e6e6><PRE>public class SimpleAuthenticator
   extends Authenticator
{
   private String username,
                  password;
    public SimpleAuthenticator(String username,String password)
   {
      this.username = username;
      this.password = password;
   }
    protected PasswordAuthentication getPasswordAuthentication()
   {
      return new PasswordAuthentication(
             username,password.toCharArray());
   }
}</PRE></TD></TR></TBODY></TABLE><BR><BR>然后，通过Authenticator.setDefault()方法注册验证者： <BR><BR>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD bgColor=#e6e6e6><PRE>String url = "http://www.digitalcq.com/",
       proxy = "proxy.digitalcq.com",
       port = "8080",
       username = "usr",
       password = "pwd";
Authenticator.setDefault(new SimpleAuthenticator(
       username,password));
URL server = new URL(url);
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);
HttpURLConnection connection = (
    HttpURLConnection)server.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
readResponse(in);</PRE></TD></TR></TBODY></TABLE><BR><BR><FONT color=#000099><STRONG>问题</STRONG></FONT><BR><BR>上面介绍的都是HttpURLConnection类能够正常工作的情况。但是我在实际情况中遇到了一些网络，在这些网络中，HttpURLConnection类无法正常工作。最后我发现关键的问题在于使用了不正确的DNS配置。在实际情况中，HttpURLConnection类总是先尝试利用DNS服务器来解析地址名称。通常情况下，这种尝试会失败，而代理服务器会将连接重新定向。但是某些DNS服务器会返回一些不正确的响应，从而导致程序抛出UnknownHostException异常。作为一个程序员，系统不会为了你的程序更改DNS服务器的配置，因此你需要找出解决这个问题的方法。我的方案是通过自己实现HTTP协议来解决这个问题。例如一个GET命令可以用下面的代码来实现： <BR><BR>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=550 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD bgColor=#e6e6e6><PRE>String url = "http://www.digitalcq.com/",
       proxy = "proxy.digitalcq.com",
       port = "8080",
       authentication = "usr:pwd";
URL server = new URL(url);
Socket socket = new Socket(proxy,port);
Writer writer = new OutputStreamWriter(socket.getOutputStream(),
                                       "US-ASCII");
writer.write("GET " + server.toExternalForm() + " HTTP/1.0\r\n");
writer.write("Host: " + server.getHost() + "\r\n");
writer.write("Proxy-Authorization: Basic "
             + new sun.misc.BASE64Encoder().encode(
               authentication.getBytes())
             + "\r\n\r\n");
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(
                            socket.getInputStream(),"US-ASCII"));
String line = reader.readLine();
if(line != null &amp;&amp; line.startsWith("HTTP/"))
{
   int sp = line.indexOf(' ');
   String status = line.substring(sp + 1,sp + 4);
   if(status.equals("200"))
   {
      while(line.length() != 0)
         line = reader.readLine();
      readResponse(reader);
   }
   else
      throw new FileNotFoundException("Host reports error " +
                                       status);
}
else
   throw new IOException("Bad protocol");
reader.close();
writer.close();
socket.close();</PRE></TD></TR></TBODY></TABLE><BR><BR>在上面的代码中，大家可以注意到代理服务器的用户名和密码的格式是: <BR><BR>username:password， <BR><BR>并且程序对它们基于Base 64进行了编码。如果需要参考HTTP协议，可以到<A href="http://www.ietf.org/rfc/rfc2616.txt" target=_blank><U><FONT color=#0000ff>http://www.ietf.org/rfc/rfc2616.txt</FONT></U></A>。 <BR></FONT></SPAN></DIV><img src ="http://www.blogjava.net/bluesky/aggbug/18586.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-07 15:44 <a href="http://www.blogjava.net/bluesky/articles/18586.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>类和对象的概念</title><link>http://www.blogjava.net/bluesky/articles/18583.html</link><dc:creator>bluesky</dc:creator><author>bluesky</author><pubDate>Mon, 07 Nov 2005 07:15:00 GMT</pubDate><guid>http://www.blogjava.net/bluesky/articles/18583.html</guid><wfw:comment>http://www.blogjava.net/bluesky/comments/18583.html</wfw:comment><comments>http://www.blogjava.net/bluesky/articles/18583.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/bluesky/comments/commentRss/18583.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/bluesky/services/trackbacks/18583.html</trackback:ping><description><![CDATA[<P>类是把现实世界中具有相同属性和行为的物体抽象出来，用来构造对象实体的 模板。</P>
<P>类是创建对象的模板。</P>
<P>对象是类的实例。</P><img src ="http://www.blogjava.net/bluesky/aggbug/18583.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/bluesky/" target="_blank">bluesky</a> 2005-11-07 15:15 <a href="http://www.blogjava.net/bluesky/articles/18583.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>