??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲国产成人久久精品99 ,国产亚洲欧美日韩亚洲中文色,亚洲国产精品不卡在线电影http://www.tkk7.com/bluesky/category/4531.html做好软gZ? #gcc -c helloworld.c -o helloworld.o //~译目标文g #gcc helloworld.o -o helloworld //~译成可执行exe #helloworld //q行exezh-cnTue, 27 Feb 2007 08:51:19 GMTTue, 27 Feb 2007 08:51:19 GMT60Delphi判断字符是否是数?/title><link>http://www.tkk7.com/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.tkk7.com/bluesky/articles/21500.html</guid><wfw:comment>http://www.tkk7.com/bluesky/comments/21500.html</wfw:comment><comments>http://www.tkk7.com/bluesky/articles/21500.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/bluesky/comments/commentRss/21500.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/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.tkk7.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">function IsNumeric(Data: string) : </SPAN><SPAN style="COLOR: #0000ff">boolean</SPAN><SPAN style="COLOR: #000000">; <BR><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top>    var i, code : integer; <BR><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top>    begin <BR><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top>    val(data, i, code); <BR><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top>     <BR><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top>    </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> code </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000"> <BR><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top>     then result :</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000"> <BR><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top>     </SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000"> result :</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">; <BR><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top>    end; <BR><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV><img src ="http://www.tkk7.com/bluesky/aggbug/21500.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/bluesky/" target="_blank">bluesky</a> 2005-11-26 13:19 <a href="http://www.tkk7.com/bluesky/articles/21500.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java.util.Datecd格式化输出和字符串{Datecdhttp://www.tkk7.com/bluesky/articles/20099.htmlblueskyblueskyWed, 16 Nov 2005 08:59:00 GMThttp://www.tkk7.com/bluesky/articles/20099.htmlhttp://www.tkk7.com/bluesky/comments/20099.htmlhttp://www.tkk7.com/bluesky/articles/20099.html#Feedback0http://www.tkk7.com/bluesky/comments/commentRss/20099.htmlhttp://www.tkk7.com/bluesky/services/trackbacks/20099.html 1 import java.text.Format;
 2 import java.text.ParseException;
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 public class DateFormatToString {
 7     protected static Format format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 8 
 9     public static void main(String[] args) {
10 
11         Date date = new Date();
12         System.out.println(format.format(date));
13 
14         try {
15             Date date1 = (Date) format.parseObject("2005-11-15 20:55:48");
16             System.out.println(date1);
17         } catch (ParseException e) {
18             // TODO Auto-generated catch block
19 
20         }
21 
22         format = new SimpleDateFormat("yyyy-MM-dd");
23         System.out.println(format.format(date));
24         format = new SimpleDateFormat("hh:mm:ss");
25         System.out.println(format.format(new Date()));
26 
27     }
28 }
输出
12005-11-15 09:02:30
2Tue Nov 15 20:55:48 CST 2005
32005-11-15
409:02:30


bluesky 2005-11-16 16:59 发表评论
]]>
对象序列化存储读取实C?/title><link>http://www.tkk7.com/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.tkk7.com/bluesky/articles/18911.html</guid><wfw:comment>http://www.tkk7.com/bluesky/comments/18911.html</wfw:comment><comments>http://www.tkk7.com/bluesky/articles/18911.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/bluesky/comments/commentRss/18911.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/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"> 1</SPAN><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.io.FileInputStream;<BR></SPAN><SPAN style="COLOR: #008080"> 2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.io.FileOutputStream;<BR></SPAN><SPAN style="COLOR: #008080"> 3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.io.ObjectInputStream;<BR></SPAN><SPAN style="COLOR: #008080"> 4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.io.ObjectOutputStream;<BR></SPAN><SPAN style="COLOR: #008080"> 5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.io.Serializable;<BR></SPAN><SPAN style="COLOR: #008080"> 6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080"> 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.tkk7.com/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.tkk7.com/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"> serialStore </SPAN><SPAN style="COLOR: #0000ff">implements</SPAN><SPAN style="COLOR: #000000"> Serializable </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.tkk7.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_213_1528_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080"> 8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> serialStore() </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> serialStore()</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </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">/** */</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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>     * 写对象到指定文g?BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>     </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> writeFile(String strFile, Object[] o) </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.tkk7.com/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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>        </SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000"> </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>            FileOutputStream fos </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> FileOutputStream(strFile);<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>            ObjectOutputStream oos </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/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.tkk7.com/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"> o.length; i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">) </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                oos.writeObject(o[i]);<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>            }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">22</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>            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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>        }</SPAN></SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000"> (Exception e) </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>            e.printStackTrace();<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>        }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">26</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> writeFile()</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">27</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </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">/** */</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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>     * L定文件的对象到对象组?BR></SPAN><SPAN style="COLOR: #008080">30</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>     </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> Object[] readFile(String strFile, </SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> objectNum) </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        Object o[] </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>        </SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000"> </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>            FileInputStream fis </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> FileInputStream(strFile);<BR></SPAN><SPAN style="COLOR: #008080">35</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>            ObjectInputStream ois </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/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.tkk7.com/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"> o.length; i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">) </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                o[i] </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> ois.readObject();<BR></SPAN><SPAN style="COLOR: #008080">38</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>            }</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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>        }</SPAN></SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000"> (Exception e) </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>            e.printStackTrace();<BR></SPAN><SPAN style="COLOR: #008080">41</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>        }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">42</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> o;<BR></SPAN><SPAN style="COLOR: #008080">43</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> readFile()</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">44</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </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">/** */</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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>     * 昄对象内容<BR></SPAN><SPAN style="COLOR: #008080">47</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>     </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> log(Object[] o) </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.tkk7.com/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.tkk7.com/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.tkk7.com/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"> o.length; i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">) </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>            System.out.println(o[i]);<BR></SPAN><SPAN style="COLOR: #008080">51</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>        }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">52</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> log()</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">53</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </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">/** */</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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>     * 昄对象内容<BR></SPAN><SPAN style="COLOR: #008080">56</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>     </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> log(Object o) </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        System.out.println(o);<BR></SPAN><SPAN style="COLOR: #008080">59</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> log()</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">60</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/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.tkk7.com/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.tkk7.com/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 arg[]) </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        Object o[] </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        o[</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">] </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        o[</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">] </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        serialStore ss </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> serialStore();<BR></SPAN><SPAN style="COLOR: #008080">66</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        String strFile </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        ss.writeFile(strFile, o);<BR></SPAN><SPAN style="COLOR: #008080">68</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        ss.log(ss.readFile(strFile, o.length));<BR></SPAN><SPAN style="COLOR: #008080">69</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</SPAN></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">main() </SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">70</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/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.tkk7.com/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.tkk7.com/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.tkk7.com/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">/** */</SPAN><SPAN id=Codehighlighter1_1530_1547_Open_Text><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000"> serialStore </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.tkk7.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">74</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV><BR><IMG height=20 src="http://www.tkk7.com/Emoticons/QQ/15.gif" width=20 border=0><img src ="http://www.tkk7.com/bluesky/aggbug/18911.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/bluesky/" target="_blank">bluesky</a> 2005-11-09 10:03 <a href="http://www.tkk7.com/bluesky/articles/18911.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java对象序列化机?/title><link>http://www.tkk7.com/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.tkk7.com/bluesky/articles/18901.html</guid><wfw:comment>http://www.tkk7.com/bluesky/comments/18901.html</wfw:comment><comments>http://www.tkk7.com/bluesky/articles/18901.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/bluesky/comments/commentRss/18901.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/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">对象序列化机制对于需要将对象的状态保存到文g中,而后能够通过d对象状态来重新构造对象,恢复E序状态,或者用套接字在网l上传送对象的E序来说Q是很有用的。通过让类实现</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">Q接口。也是_对于要实现它的类来说Q该接口不需要实CQ何方法。它主要用来通知</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">Q需要将一个对象序列化?/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">q制形式Q是对象持久化的Ҏ之一?BR><BR>《实现类的序列化--例子Vector对象压入标准》出?CN-JAVA原创Qjackliu<BR></SPAN><SPAN lang=EN style="mso-ansi-language: EN"><o:p><BR>Vector对象是一个很灉|的java数据l构Q在实际~程中,有时需要我们将一个Vector对象传递给另一个JavaE序q保持Vector的数据结构状态,q时Q我们可以将需要传递的对象实现java.io.Serializable接口Q序列化q个c,׃序列化本w就是允怸个对象在虚拟Z间传?或者经q一D늩_如在RMI?或者经q一D|_比如数据被保存C个文件中)?关于cȝ序列化请查阅相关资料Q本不在叙qͼ下面使用一个简单的E序说明如何把一个Vector对象序列化ƈ攑ֈ一个流中(Z试方便Q这里放C个文件流中,如果换成一个套接字可以把对象发送给q程h者)</o:p></SPAN></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN style="mso-ansi-language: EN"><o:p>E序1Q把一个Vector对象存储C个testvector.obj文g里(模拟server socket处理Q?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"> 1</SPAN><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.io.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080"> 2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.util.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080"> 3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> TestVector </SPAN><SPAN style="COLOR: #0000ff">implements</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_87_1155_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080"> 5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        </SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000"> Vector vect</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> Vector();<BR></SPAN><SPAN style="COLOR: #008080"> 6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>        <BR></SPAN><SPAN style="COLOR: #008080"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>        </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> add(Object 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.tkk7.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_175_221_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080"> 8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                </SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.vect.add(obj);<BR></SPAN><SPAN style="COLOR: #008080"> 9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>        }</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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>        </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                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.tkk7.com/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 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.tkk7.com/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.tkk7.com/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_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.tkk7.com/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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>                </SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000"> </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.tkk7.com/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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>                        </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"> 对象写C个文仉 </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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        FileOutputStream objfile </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>                        </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"> 创徏一个输出流 </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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        ObjectOutputStream p </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>                        </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"> 创徏一个TestVector对象 </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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        TestVector tv </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>                        </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">lVector写入几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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        p.writeObject(tv); </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 把tv写入?/SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">26</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">                        p.flush();<BR></SPAN><SPAN style="COLOR: #008080">27</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        objfile.close();    </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000"> 关闭文g对象</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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">                }</SPAN></SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000"> (Exception e) </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        e.printStackTrace();<BR></SPAN><SPAN style="COLOR: #008080">30</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>                }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">31</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>        }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">32</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/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.tkk7.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt">~译E序1Q运行后Q在当前目录生成一个testvector.obj文gQ这个文仉存放了TestVectorcȝ数据</P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><BR>E序2Q从testvector.obj文g里获取TestVector对象Q模拟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"> 1</SPAN><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.io.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080"> 2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.util.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080"> 3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_58_623_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080"> 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.tkk7.com/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.tkk7.com/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_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.tkk7.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_108_621_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>                </SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000"> </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.tkk7.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_130_523_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>                        </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"> 打开文gQ读取Vector 存放的对象数?nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080"> 8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        FileInputStream objfile </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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"> 9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        ObjectInputStream q </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> 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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>                        </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"> 获取对象原始数据 </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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        TestVector myvector </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (TestVector)q.readObject();<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        myvector.print(); <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.tkk7.com/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.tkk7.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>                }</SPAN></SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000"> (Exception e) </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.tkk7.com/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.tkk7.com/images/OutliningIndicators/InBlock.gif" align=top>                        e.printStackTrace();   <BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>                }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>        }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.tkk7.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><BR>~译E序2Q运行后Q读取testvector.obj文gQ根据内Ҏ建出原始的TestVectorcR?/P></o:p></SPAN><img src ="http://www.tkk7.com/bluesky/aggbug/18901.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/bluesky/" target="_blank">bluesky</a> 2005-11-09 09:52 <a href="http://www.tkk7.com/bluesky/articles/18901.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>三种初始化字D늚方式http://www.tkk7.com/bluesky/articles/18872.htmlblueskyblueskyTue, 08 Nov 2005 15:11:00 GMThttp://www.tkk7.com/bluesky/articles/18872.htmlhttp://www.tkk7.com/bluesky/comments/18872.htmlhttp://www.tkk7.com/bluesky/articles/18872.html#Feedback0http://www.tkk7.com/bluesky/comments/commentRss/18872.htmlhttp://www.tkk7.com/bluesky/services/trackbacks/18872.html构造函C赋?BR>2声明时赋?BR>3初始化块 initialization block。类中可以包含Q意多的代码块Q只要构造了cȝ对象Q这些代码块׃被执行?BR>
例子
People.java
 1package test;
 2
 3public class People {
 4    private String name;
 5    private int age;
 6    public int getAge() {
 7        return age;
 8    }

 9    public void setAge(int age) {
10        this.age = age;
11    }

12    public String getName() {
13        return name;
14    }

15    public void setName(String name) {
16        this.name = name;
17    }

18    
19    public People(){
20        //默认构造函?/SPAN>
21    }

22    {
23        this.setAge(12);
24        this.setName("bluesky");
25    }

26
27}

28

Test.java
1package test;
2
3public class Test {
4    public static void main(String args[]){
5        People p = new People();
6        System.out.println(p.getName());
7    }

8
9}
输出bluesky

bluesky 2005-11-08 23:11 发表评论
]]>
JavaIO利用q代d文gҎ有目录及文ghttp://www.tkk7.com/bluesky/articles/18633.htmlblueskyblueskyMon, 07 Nov 2005 10:08:00 GMThttp://www.tkk7.com/bluesky/articles/18633.htmlhttp://www.tkk7.com/bluesky/comments/18633.htmlhttp://www.tkk7.com/bluesky/articles/18633.html#Feedback0http://www.tkk7.com/bluesky/comments/commentRss/18633.htmlhttp://www.tkk7.com/bluesky/services/trackbacks/18633.html 

 1import java.io.File;
 2import java.util.ArrayList;
 3
 4public class ReadFile {
 5 public static void main(String args[]) {
 6  String dir = "D:\\xxx";
 7
 8  ArrayList a = getFiles(dir);
 9  for (int i = 0; i < a.size(); i++{
10   File file = (File) a.get(i);
11  }

12 }

13
14 public static ArrayList getFiles(String aDir) {
15  ArrayList files = new ArrayList();
16  File dirx = new File(aDir);
17  File[] dirFilesx = dirx.listFiles();
18
19  for (int k = 0; k < dirFilesx.length; k++{
20   System.out.print("|");
21   File file = dirFilesx[k];
22   String fileName = file.getName();
23   System.out.println("-" + fileName);
24   if (file.isDirectory()) {
25    String dirx1 = aDir + "\\" + fileName;
26    getFiles(dirx1);
27   }

28   files.add(file);
29  }

30  return files;
31
32 }

33
34}

35
36


bluesky 2005-11-07 18:08 发表评论
]]>
正则表达式应?/title><link>http://www.tkk7.com/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.tkk7.com/bluesky/articles/18610.html</guid><wfw:comment>http://www.tkk7.com/bluesky/comments/18610.html</wfw:comment><comments>http://www.tkk7.com/bluesky/articles/18610.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/bluesky/comments/commentRss/18610.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/bluesky/services/trackbacks/18610.html</trackback:ping><description><![CDATA[<P>正则表达式是一U功能强大但又非常灵zȝ文本处理工具。它能让你用~程的方式来描述复杂的文本模式,然后在字W串里把它找出来。一旦你扑ֈ了这U模式,你就能随心所Ʋ地处理q些文本了。虽然初看v来正则表辑ּ的语法有点让人望而生畏,但它提供了一U精l的动态语aQ我们能用一U通用的方式来解决各种字符串的问题Q包括匹配,选择Q编辑以及校验?/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 id="iooeqkc" class=original_words>B</SPAN></TD> <TD>字符<span id="sookkum" class=original_words>B</SPAN></TD></TR> <TR> <TD><span id="miugsgc" class=original_words>\xhh</SPAN></TD> <TD>16q制?SPAN class=original_words>0xhh</SPAN>所表示的字W?/TD></TR> <TR> <TD><span id="aqmacso" class=original_words>\uhhhh</SPAN></TD> <TD>16q制?SPAN class=original_words>0xhhhh</SPAN>所表示的Unicode字符</TD></TR> <TR> <TD><span id="gsmgcig" class=original_words>\t</SPAN></TD> <TD>Tab</TD></TR> <TR> <TD><span id="wsuoyoe" class=original_words>\n</SPAN></TD> <TD>换行W?/TD></TR> <TR> <TD><span id="cucisag" class=original_words>\r</SPAN></TD> <TD>回RW?/TD></TR> <TR> <TD><span id="augkesk" class=original_words>\f</SPAN></TD> <TD>换页W?/TD></TR> <TR> <TD><span id="coasuui" class=original_words>\e</SPAN></TD> <TD>Escape</TD></TR></TBODY></TABLE><BR></P> <P>正则表达式的强大体现在它能定义字W集(character class)。下面是一些最常见的字W集及其定义的方式,此外q有一些预定义的字W集Q?/P> <P> <TABLE class=narration cellSpacing=0 borderColorDark=#000000 cellPadding=2 borderColorLight=#000000 border=2> <TBODY> <TR vAlign=top> <TH colSpan=2><span id="iekguao" class=original_words>字符?/SPAN> </TH></TR> <TR vAlign=top> <TD vAlign=top><span id="qeoakii" class=original_words>.</SPAN> </TD> <TD vAlign=top>表示L一个字W?</TD></TR> <TR vAlign=top> <TD vAlign=top width=131><span id="amyycky" class=original_words>[abc]</SPAN> </TD> <TD vAlign=top width=311>表示字符<span id="myskeek" class=original_words>a</SPAN>Q?SPAN class=original_words>b</SPAN>Q?SPAN class=original_words>c</SPAN>中的L一??SPAN class=original_words>a|b|c</SPAN>相同) </TD></TR> <TR vAlign=top> <TD vAlign=top width=131><span id="myumqyo" class=original_words>[^abc]</SPAN> </TD> <TD vAlign=top width=311>?SPAN class=original_words>a</SPAN>Q?SPAN class=original_words>b</SPAN>Q?SPAN class=original_words>c</SPAN>之外的Q意一个字W?否定) </TD></TR> <TR vAlign=top> <TD vAlign=top width=131><span id="cwysomu" 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>当中的Q意一个字W?范围) </TD></TR> <TR vAlign=top> <TD vAlign=top width=131><span id="ceamwem" class=original_words>[abc[hij]]</SPAN> </TD> <TD vAlign=top width=311><span id="icgauiy" class=original_words>a,b,c,h,i,j</SPAN>中的L一个字W??SPAN class=original_words>a|b|c|h|i|j</SPAN>相同)(q) </TD></TR> <TR vAlign=top> <TD vAlign=top width=131><span id="gqkmwem" class=original_words>[a-z&&[hij]]</SPAN> </TD> <TD vAlign=top width=311><span id="ooskoum" class=original_words>h,i,j</SPAN>中的一?交集) </TD></TR> <TR vAlign=top> <TD vAlign=top width=131><span id="aeimecc" class=original_words>\s</SPAN> </TD> <TD vAlign=top width=311>I格字符(I格? tab, 换行, 换页, 回R) </TD></TR> <TR vAlign=top> <TD vAlign=top width=131><span id="ceyiksq" class=original_words>\S</SPAN> </TD> <TD vAlign=top width=311>非空格字W?<span id="awgcekc" class=original_words>[^\s]</SPAN>) </TD></TR> <TR vAlign=top> <TD vAlign=top width=131><span id="woacooc" 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 id="mwskwmk" class=original_words>\D</SPAN> </TD> <TD vAlign=top width=311>一个非数字的字W,也就?SPAN class=original_words>[^0-9]</SPAN> </TD></TR> <TR vAlign=top> <TD vAlign=top width=131><span id="iumgsgw" class=original_words>\w</SPAN> </TD> <TD vAlign=top width=311>一个单词字W?word character)Q即<span id="qumyaio" class=original_words>[a-zA-Z_0-9]</SPAN> </TD></TR> <TR vAlign=top> <TD vAlign=top width=131><span id="qcwikqw" class=original_words>\W</SPAN> </TD> <TD vAlign=top width=311>一个非单词的字W,<span id="egswwwc" class=original_words>[^\w]</SPAN> </TD></TR></TBODY></TABLE></P> <P>如果你用q其它语a的正则表辑ּQ那么你一眼就能看出反斜杠的与众不同。在其它语言里,"<span id="oykugca" class=original_words>\\</SPAN>"的意思是"我只是要在正则表辑ּ里插入一个反斜杠。没什么特别的意思?但是在Java里,"<span id="cmqseua" class=original_words>\\</SPAN>"的意思是"我要插入一个正则表辑ּ的反斜杠Q所以跟在它后面的那个字W的意思就变了?举例来说Q如果你惌CZ个或更多?单词字符"Q那么这个正则表辑ּ应该是"<span id="wgscgwu" class=original_words>\\w+</SPAN>"。如果你要插入一个反斜杠Q那得?<span id="oqiuwes" class=original_words>\\\\</SPAN>"。不q像换行Q蟩gcȝq是只用一根反斜杠Q?\n\t"?/P> <P>q里只给你讲一个例子;你应?SPAN class=original_words>JDK</SPAN>文档?SPAN class=original_words>java.util.regex.Pattern</SPAN>加到收藏多wQ这样就能很Ҏ地找到各U正则表辑ּ的模式了?/P> <P> <TABLE class=narration cellSpacing=0 borderColorDark=#000000 cellPadding=2 borderColorLight=#000000 border=2> <TBODY> <TR> <TH colSpan=2>逻辑q算W?</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>一?要匹配的l?capturing group)". 以后可以用\i来表C第i个被匚w的组?/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>边界匚wW?</TH></TR> <TR vAlign=top> <TD vAlign=top width=143><span id="kmqikaa" class=original_words>^</SPAN> </TD> <TD vAlign=top width=239>一行的开?</TD></TR> <TR vAlign=top> <TD vAlign=top width=143><span id="ceyakaw" class=original_words>$</SPAN> </TD> <TD vAlign=top width=239>一行的l尾 </TD></TR> <TR vAlign=top> <TD vAlign=top width=143><span id="wikoige" class=original_words>\b</SPAN> </TD> <TD vAlign=top width=239>一个单词的边界 </TD></TR> <TR vAlign=top> <TD vAlign=top width=143><span id="skmogeu" class=original_words>\B</SPAN> </TD> <TD vAlign=top width=239>一个非单词的边?</TD></TR> <TR vAlign=top> <TD vAlign=top width=143><span id="ysueayu" class=original_words>\G</SPAN> </TD> <TD vAlign=top width=239>前一个匹配的l束 </TD></TR></TBODY></TABLE></P><BR> <P id=header57><STRONG>数量表示W?/STRONG></P> <P>"数量表示W?quantifier)"的作用是定义模式应该匚w多少个字W?/P> <UL> <LI>Greedy(贪婪?Q?除非另有表示Q否则数量表C符都是greedy的。Greedy的表辑ּ会一直匹配下去,直到匚w不下Mؓ止?U>(如果你发现表辑ּ匚w的结果与预期的不W?</U>Q很有可能是因ؓQ你以ؓ表达式会只匹配前面几个字W,而实际上它是greedy的,因此会一直匹配下厅R? <LI>Reluctant(勉强?Q?用问可C,它会匚w最的字符。也UCؓlazy, minimal matching, non-greedy, 或ungreedy? <LI>Possessive(占有?Q?目前只有Java支持(其它语言都不支持)。它更加先进Q所以你可能q不太会用。用正则表达式匹配字W串的时候会产生很多中间状态,<U>(一般的匚w引擎会保存这U中间状态,)</U>q样匚wp|的时候就能原路返回了。占有型的表辑ּ不保存这U中间状态,因此也就不会回头重来了。它能防止正则表辑ּ的失控,同时也能提高q行的效率?</LI></UL> <TABLE class=narration cellSpacing=0 borderColorDark=#000000 cellPadding=2 borderColorLight=#000000 border=2> <TBODY> <TR vAlign=top> <TH><span id="eqicwwk" class=original_words>Greedy</SPAN> </TH> <TH><span id="skwycqg" class=original_words>Reluctant</SPAN> </TH> <TH><span id="scmysiy" class=original_words>Possessive</SPAN> </TH> <TH>匚w </TH></TR> <TR vAlign=top> <TD vAlign=top width=71><span id="cwykmka" class=original_words>X?</SPAN> </TD> <TD vAlign=top width=86><span id="eyaewem" class=original_words>X??</SPAN> </TD> <TD vAlign=top width=93><span id="oyscouc" class=original_words>X?+</SPAN> </TD> <TD vAlign=top width=231>匚w一个或零个<span id="cmykmiy" class=original_words>X</SPAN> </TD></TR> <TR vAlign=top> <TD vAlign=top width=71><span id="qcuyaoe" class=original_words>X*</SPAN> </TD> <TD vAlign=top width=86><span id="mgiuocs" class=original_words>X*?</SPAN> </TD> <TD vAlign=top width=93><span id="oikegem" class=original_words>X*+</SPAN> </TD> <TD vAlign=top width=231>匚w零或多个<span id="moaswui" class=original_words>X</SPAN> </TD></TR> <TR vAlign=top> <TD vAlign=top width=71><span id="oikeiyw" class=original_words>X+</SPAN> </TD> <TD vAlign=top width=86><span id="mwacwca" class=original_words>X+?</SPAN> </TD> <TD vAlign=top width=93><span id="woyaucy" class=original_words>X++</SPAN> </TD> <TD vAlign=top width=231>匚w一个或多个<span id="ceoimca" class=original_words>X</SPAN> </TD></TR> <TR vAlign=top> <TD vAlign=top width=71><span id="emqamiq" class=original_words>X{n}</SPAN> </TD> <TD vAlign=top width=86><span id="cugacyw" class=original_words>X{n}?</SPAN> </TD> <TD vAlign=top width=93><span id="kcwykio" class=original_words>X{n}+</SPAN> </TD> <TD vAlign=top width=231>匚w正好n?SPAN class=original_words>X</SPAN> </TD></TR> <TR vAlign=top> <TD vAlign=top width=71><span id="yqiuosm" class=original_words>X{n,}</SPAN> </TD> <TD vAlign=top width=86><span id="micuiqe" class=original_words>X{n,}?</SPAN> </TD> <TD vAlign=top width=93><span id="kuoamai" class=original_words>X{n,}+</SPAN> </TD> <TD vAlign=top width=231>匚w臛_n?SPAN class=original_words>X</SPAN> </TD></TR> <TR vAlign=top> <TD vAlign=top width=71><span id="ceqacsa" class=original_words>X{n,m}</SPAN> </TD> <TD vAlign=top width=86><span id="eyyseua" class=original_words>X{n,m}?</SPAN> </TD> <TD vAlign=top width=93><span id="wyskwes" class=original_words>X{n,m}+</SPAN> </TD> <TD vAlign=top width=231>匚w臛_n个,臛_m?SPAN class=original_words>X</SPAN> </TD></TR></TBODY></TABLE> <P>再提醒一下,要想让表辑ּ照你的意思去q行Q你应该用括h'X'括v来。比方说Q?/P> <BLOCKQUOTE><PRE>abc+</PRE></BLOCKQUOTE> <P>gq个表达式能匚w一个或若干?abc'Q但是如果你真的用它d?abcabcabc'的话Q实际上只会扑ֈ三个字符。因个表辑ּ的意思是'ab'后边跟着一个或多个'c'。要惛_配一个或多个完整?abc'Q你应该q样Q?/P> <BLOCKQUOTE><PRE>(abc)+</PRE></BLOCKQUOTE> <P>正则表达式能轻而易丑֜把你l耍了Q这是一U徏立在<span id="qqcoqqo" class=original_words>Java</SPAN>之上的新语言?BR><BR></P> <P id=header59><STRONG><span id="iieqiqw" class=original_words>Pattern</SPAN>?SPAN class=original_words>Matcher</SPAN></STRONG></P> <P>先给一个例子。下面这D늨序可以测试正则表辑ּ是否匚w字符丌Ӏ第一个参数是要匹配的字符Ԍ后面是正则表辑ּ。正则表辑ּ可以有多个。在Unix/Linux环境下,命o行下的正则表辑ּq必ȝ引号?/P> <P>当你创徏正则表达式时Q可以用q个E序来判断它是不是会按照你的要求工作?/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 < 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 < 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 id="gqmwiyo" class=original_words>Java</SPAN>的正则表辑ּ是由<span id="iamwqgw" class=original_words>java.util.regex</SPAN>?SPAN class=original_words>Pattern</SPAN>?SPAN class=original_words>Matcher</SPAN>cd现的?SPAN class=original_words>Pattern</SPAN>对象表示l编译的正则表达式。静态的<span id="cgycess" class=original_words>compile( )</SPAN>Ҏ负责表C正则表辑ּ的字W串~译?SPAN class=original_words>Pattern</SPAN>对象。正如上qCE所C的Q只要给<span id="ogqsmsq" class=original_words>Pattern</SPAN>?SPAN class=original_words>matcher( )</SPAN>Ҏ送一个字W串p获取一?SPAN class=original_words>Matcher</SPAN>对象。此外,<span id="aamgiqe" class=original_words>Pattern</SPAN>q有一个能快速判断能否在<span id="kmqiuii" class=original_words>input</SPAN>里面扑ֈ<span id="acuyqqo" class=original_words>regex</SPAN>?注意Q原文有误,漏了Ҏ?</P> <BLOCKQUOTE><PRE><FONT color=#0000ff>static</FONT> <FONT color=#0000ff>boolean</FONT> matches( regex,  input)</PRE></BLOCKQUOTE> <P>以及能返?SPAN class=original_words>String</SPAN>数组?SPAN class=original_words>split( )</SPAN>ҎQ它能用<span id="egacoes" class=original_words>regex</SPAN>把字W串分割开来?/P> <P>只要l?SPAN class=original_words>Pattern.matcher( )</SPAN>Ҏ传一个字W串p获得<span id="yiceaym" class=original_words>Matcher</SPAN>对象了。接下来p?SPAN class=original_words>Matcher</SPAN>的方法来查询匚w的结果了?/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 id="kcwqaqy" class=original_words>matches( )</SPAN>的前提是<span id="cmeysso" class=original_words>Pattern</SPAN>匚w整个字符Ԍ?SPAN class=original_words>lookingAt( )</SPAN>的意思是<span id="gamgaow" class=original_words>Pattern</SPAN>匚w字符串的开头?</P> <H4 id=header60>find( )</H4> <P><span id="cmgkusi" class=original_words>Matcher.find( )</SPAN>的功能是发现<span id="ueoqsii" class=original_words>CharSequence</SPAN>里的Q与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 id="qcwsmai" class=original_words>\\w+</SPAN>"的意思是"一个或多个单词字符"Q因此它会将字符串直接分解成单词?SPAN class=original_words>find( )</SPAN>像一个P代器Q从头到扫描一遍字W串。第二个<span id="swysuaa" class=original_words>find( )</SPAN>是带<span id="ogsciou" class=original_words>int</SPAN>参数的,正如你所看到的,它会告诉Ҏ从哪里开始找——即从参C|开始查找?/P> <P id=header61><STRONG>Groups</STRONG></P> <P>Group是指里用括号括v来的Q能被后面的表达式调用的正则表达式。Group 0 表示整个表达式,group 1表示W一个被括v来的groupQ以此类推。所以;</P> <BLOCKQUOTE><PRE>A(B(C))D</PRE></BLOCKQUOTE> <P>里面有三个groupQgroup 0?SPAN class=original_words>ABCD</SPAN>Q?group 1?SPAN class=original_words>BC</SPAN>Qgroup 2?SPAN class=original_words>C</SPAN>?/P> <P>你可以用下述<span id="oyscwek" class=original_words>Matcher</SPAN>Ҏ来用groupQ?/P> <P><span id="wgicwms" class=original_words>public int groupCount( )</SPAN>q回matcher对象中的group的数目。不包括group0?/P> <P><span id="kuwsksq" class=original_words>public String group( ) </SPAN>q回上次匚w操作(比方?SPAN class=original_words>find( )</SPAN>)的group 0(整个匚w)</P> <P><span id="askegeu" class=original_words>public String group(int i)</SPAN>q回上次匚w操作的某个group。如果匹配成功,但是没能扑ֈgroupQ则q回null?/P> <P><span id="cuoacsq" class=original_words>public int start(int group)</SPAN>q回上次匚w所扑ֈ的,group的开始位|?/P> <P><span id="gyumyem" class=original_words>public int end(int group)</SPAN>q回上次匚w所扑ֈ的,group的结束位|,最后一个字W的下标加一?/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 <= 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>q首诗是<span id="oyacemu" class=original_words><CITE>Through the Looking Glass</CITE></SPAN>的,Lewis Carroll?Jabberwocky"的第一部分。可以看到这个正则表辑ּ里有很多用括hh的groupQ它是由L多个q箋的非I字W?'<span id="kwoiusi" class=original_words>\S+</SPAN>')和Q意多个连l的I格字符('<span id="ooyaeca" class=original_words>\s+</SPAN>')所l成的,其最l目的是要捕h行的最后三个单词;'<span id="gqkegem" class=original_words>$</SPAN>'表示一行的l尾。但?<span id="iskgaow" class=original_words>$</SPAN>'通常表示整个字符串的l尾Q所以这里要明确地告诉正则表辑ּ注意换行W。这一Ҏ?<span id="wyseowe" class=original_words>(?m)</SPAN>'标志完成?模式标志会过一会讲??/P> <P id=header62><STRONG>start( )和end( )</STRONG></P> <P>如果匚w成功Q?SPAN class=original_words>start( )</SPAN>会返回此ơ匹配的开始位|,<span id="oqakemk" class=original_words>end( )</SPAN>会返回此ơ匹配的l束位置Q即最后一个字W的下标加一。如果之前的匚w不成?或者没匚w)Q那么无论是调用<span id="egiswes" class=original_words>start( )</SPAN>q是<span id="oiaeyai" class=original_words>end( )</SPAN>Q都会引发一?SPAN class=original_words>IllegalStateException</SPAN>。下面这D늨序还演示?SPAN class=original_words>matches( )</SPAN>?SPAN class=original_words>lookingAt( )</SPAN>Q?/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 < 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>注意Q只要字W串里有q个模式Q?SPAN class=original_words>find( )</SPAN>p把它l找出来Q但?SPAN class=original_words>lookingAt( )</SPAN>?SPAN class=original_words>matches( )</SPAN>Q只有在字符串与正则表达式一开始就相匹配的情况下才能返?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>成功的前提是Q字W串的开始部分与正则表达式相匚w?/P> <P id=header63><STRONG>匚w的模?Pattern flags)</STRONG></P> <P><span id="myakwwu" class=original_words>compile( )</SPAN>Ҏq有一个版本,它需要一个控制正则表辑ּ的匹配行为的参数Q?/P> <BLOCKQUOTE><PRE>Pattern Pattern.compile(String regex, <FONT color=#0000ff>int</FONT> flag)</PRE></BLOCKQUOTE> <P><span id="wauoyow" class=original_words>flag</SPAN>的取D围如下: <TABLE class=narration cellSpacing=0 borderColorDark=#000000 cellPadding=2 borderColorLight=#000000 border=2> <TBODY> <TR> <TH><span id="ueyacms" class=original_words>~译标志</SPAN> </TH> <TH><span id="kewyciy" class=original_words>效果</SPAN> </TH></TR> <TR> <TD><span id="egseyou" class=original_words>Pattern.CANON_EQ</SPAN> </TD> <TD>当且仅当两个字符?正规分解(canonical decomposition)"都完全相同的情况下,才认定匹配。比如用了这个标志之后,表达?a\u030A"会匹??"。默认情况下Q不考虑"规范相等?canonical equivalence)"?</TD></TR> <TR> <TD><span id="wqugyow" class=original_words>Pattern.CASE_INSENSITIVE<BR>(?i)</SPAN> </TD> <TD>默认情况下,大小写不明感的匹配只适用于US-ASCII字符集。这个标志能让表辑ּ忽略大小写进行匹配。要惛_Unicode字符q行大小不明感的匚wQ只要将<span id="eoicwck" class=original_words>UNICODE_CASE</SPAN>与这个标志合hp了?</TD></TR> <TR> <TD><span id="yiswwmu" class=original_words>Pattern.COMMENTS<BR>(?x)</SPAN> </TD> <TD>在这U模式下Q匹配时会忽?正则表达式里?I格字符(译者注Q不是指表达式里?\\s"Q而是指表辑ּ里的I格QtabQ回车之c?。注释从#开始,一直到q行l束。可以通过嵌入式的标志来启用Unix行模式?</TD></TR> <TR> <TD><span id="ysmyiiw" class=original_words>Pattern.DOTALL<BR>(?s)</SPAN> </TD> <TD>在这U模式下Q表辑ּ'.'可以匚wL字符Q包括表CZ行的l束W。默认情况下Q表辑ּ'.'不匹配行的结束符?</TD></TR> <TR> <TD><span id="eyacwem" class=original_words>Pattern.MULTILINE<BR>(?m)</SPAN> </TD> <TD>在这U模式下Q?^'?$'分别匚w一行的开始和l束。此外,'^'仍然匚w字符串的开始,'$'也匹配字W串的结束。默认情况下Q这两个表达式仅仅匹配字W串的开始和l束?</TD></TR> <TR> <TD><span id="eeoaksi" class=original_words>Pattern.UNICODE_CASE<BR>(?u)</SPAN> </TD> <TD>在这个模式下Q如果你q启用了<span id="gyskomc" class=original_words>CASE_INSENSITIVE</SPAN>标志Q那么它会对Unicode字符q行大小写不明感的匹配。默认情况下Q大写不明感的匚w只适用于US-ASCII字符集?</TD></TR> <TR> <TD><span id="oykoqge" class=original_words>Pattern.UNIX_LINES<BR>(?d)</SPAN> </TD> <TD>在这个模式下Q只?\n'才被认作一行的中止Qƈ且与'.'Q?^'Q以?$'q行匚w?</TD></TR></TBODY></TABLE></P> <P>在这些标志里面,<span id="saceykw" class=original_words>Pattern.CASE_INSENSITIVE</SPAN>Q?SPAN class=original_words>Pattern.MULTILINE</SPAN>Q以?SPAN class=original_words>Pattern.COMMENTS</SPAN>是最有用?其中<span id="segiucy" class=original_words>Pattern.COMMENTS</SPAN>q能帮我们把思\理清楚,q且/或者做文档)。注意,你可以用在表辑ּ里插记号的方式来启用l大多数的模式。这些记号就在上面那张表的各个标志的下面。你希望模式从哪里开始启动,在哪里插记受?/P> <P>可以?OR" ('|')q算W把q些标志合用:</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>q样创徏出来的正则表辑ּp匚w?java"Q?Java"Q?JAVA"...开头的字符串了。此外,如果字符串分好几行,那它q会Ҏ一行做匚w(匚w始于字符序列的开始,l于字符序列当中的行l束W?。注意,<span id="gaysuky" class=original_words>group( )</SPAN>Ҏ仅返回匹配的部分?/P> <P id=header64><STRONG>split( )</STRONG></P> <P>所谓分割是指将以正则表辑ּ为界Q将字符串分割成<span id="suqiukc" 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>q是一U既快又方便地将文本Ҏ一些常见的边界标志分割开来的Ҏ?/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>W二?SPAN class=original_words>split( )</SPAN>会限定分割的ơ数?/P> <P>正则表达式是如此重要Q以至于有些功能被加q了<span id="eqkcwek" class=original_words>String</SPAN>c,其中包括<span id="emgcuks" class=original_words>split( )</SPAN>(已经看到?Q?SPAN class=original_words>matches( )</SPAN>Q?SPAN class=original_words>replaceFirst( )</SPAN>以及<span id="ygkkwms" 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>正则表达式在替换文本斚w特别在行。下面就是一些方法:</P> <P><span id="gaeyyoe" class=original_words>replaceFirst(String replacement)</SPAN>字W串里,W一个与模式相匹配的子串替换?SPAN class=original_words>replacement</SPAN>?</P> <P><span id="kuoiuka" class=original_words>replaceAll(String replacement)</SPAN>Q将输入字符串里所有与模式相匹配的子串全部替换?SPAN class=original_words>replacement</SPAN>?/P> <P><span id="qacgyww" class=original_words>appendReplacement(StringBuffer sbuf, String replacement)</SPAN>?SPAN class=original_words>sbuf</SPAN>q行逐次替换Q而不是像<span id="cmykmaq" class=original_words>replaceFirst( )</SPAN>?SPAN class=original_words>replaceAll( )</SPAN>那样Q只替换W一个或全部子串。这是个非常重要的方法,因ؓ它可以调用方法来生成<span id="yysegge" class=original_words>replacement</SPAN>(<span id="wykmqge" class=original_words>replaceFirst( )</SPAN>?SPAN class=original_words>replaceAll( )</SPAN>只允许用固定的字W串来充?SPAN class=original_words>replacement</SPAN>)。有了这个方法,你就可以~程区分groupQ从而实现更强大的替换功能?/P> <P>调用?SPAN class=original_words>appendReplacement( )</SPAN>之后Qؓ了把剩余的字W串拯回去Q必调?SPAN class=original_words>appendTail(StringBuffer sbuf, String replacement)</SPAN>?</P> <P>下面我们来演CZ下怎样使用q些替换Ҏ。说明一下,q段E序所处理的字W串是它自己开头部分的注释Q是用正则表辑ּ提取出来q加以处理之后再传给替换Ҏ的?/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>我们用前面介l的<span id="kewikas" class=original_words>TextFile.read( )</SPAN>Ҏ来打开和读取文件?SPAN class=original_words>mInput</SPAN>的功能是匚w'<span id="ccyscsg" class=original_words>/*!</SPAN>' ?'<span id="skgqciq" class=original_words>!*/</SPAN>' 之间的文?注意一下分l用的括?。接下来Q我们将所有两个以上的q箋I格全都替换成一个,q且各行开头的I格全都L(Z让这个正则表辑ּ能对所有的行,而不仅仅是第一行v作用Q必d用多行模?。这两个操作都用?SPAN class=original_words>String</SPAN>?SPAN class=original_words>replaceAll( )</SPAN>(q里用它更方?。注意,׃每个替换只做一ơ,因此除了预编?SPAN class=original_words>Pattern</SPAN>之外Q程序没有额外的开销?/P> <P><span id="oegqssy" class=original_words>replaceFirst( )</SPAN>只替换第一个子丌Ӏ此外,<span id="kkmgiom" class=original_words>replaceFirst( )</SPAN>?SPAN class=original_words>replaceAll( )</SPAN>只能用常?literal)来替换,所以如果你每次替换的时候还要进行一些操作的话,它们是无能ؓ力的。碰到这U情况,你得?SPAN class=original_words>appendReplacement( )</SPAN>Q它能让你在q行替换的时候想写多代码就写多。在上面那段E序里,创徏<span id="kqceqgu" class=original_words>sbuf</SPAN>的过E就是选group做处理,也就是用正则表达式把元音字母扑և来,然后换成大写的过E。通常你得在完成全部的替换之后才调?SPAN class=original_words>appendTail( )</SPAN>Q但是如果要模仿<span id="kkwiaag" class=original_words>replaceFirst( )</SPAN>(?replace n")的效果,你也可以只替换一ơ就调用<span id="csuoiwk" class=original_words>appendTail( )</SPAN>。它会把剩下的东西全都放q?SPAN class=original_words>sbuf</SPAN>?/P> <P>你还可以?SPAN class=original_words>appendReplacement( )</SPAN>?SPAN class=original_words>replacement</SPAN>参数里用"$g"引用已捕LgroupQ其?g' 表示group的号码。不q这是ؓ一些比较简单的操作准备的,因而其效果无法与上q程序相比?/P> <P id=header66><STRONG>reset( )</STRONG></P> <P>此外Q还可以?SPAN class=original_words>reset( )</SPAN>Ҏl现有的<span id="ooqkwks" 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>如果不给参数Q?SPAN class=original_words>reset( )</SPAN>会把<span id="qqkcoeu" class=original_words>Matcher</SPAN>讑ֈ当前字符串的开始处?/P> <P id=header67><STRONG>正则表达式与Java I/O</STRONG></P> <P>到目前ؓ止,你看到的都是用正则表辑ּ处理静态字W串的例子。下面我们来演示一下怎样用正则表辑ּ扫描文gq且扑և匚w的字W串。受Unix的grep启发Q我写了?SPAN class=original_words>JGrep.java</SPAN>Q它需要两个参敎ͼ文g名,以及匚w字符串用的正则表辑ּ。它会把匚wq个正则表达式那部分内容及其所属行的行h印出来?/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 < 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>文g是用<span id="sscmqem" class=original_words>TextFile</SPAN>打开?本章的前半部分讲?。由?SPAN class=original_words>TextFile</SPAN>会把文g的各行放?SPAN class=original_words>ArrayList</SPAN>里面Q而我们又提取了一?SPAN class=original_words>ListIterator</SPAN>Q因此我们可以在文g的各行当中自q?既能向前也可以向??</P> <P>每行都会有一?SPAN class=original_words>Matcher</SPAN>Q然后用<span id="iiakmcs" class=original_words>find( )</SPAN>扫描。注意,我们?SPAN class=original_words>ListIterator.nextIndex( )</SPAN>跟踪行号?</P> <P>试参数?SPAN class=original_words>JGrep.java</SPAN>和以<span id="qgauwwu" class=original_words>[Ssct]</SPAN>开头的单词?/P> <P id=header68><STRONG>q需要StringTokenizer?</STRONG></P> <P>看到正则表达式能提供q么强大的功能,你可能会怀疑,是不是还需要原先的<span id="owgseca" class=original_words>StringTokenizer</SPAN>。JDK 1.4以前Q要惛_割字W串Q只有用<span id="kscgsgg" class=original_words>StringTokenizer</SPAN>。但现在Q有了正则表辑ּ之后Q它p做得更干净利烦了?/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>有了正则表达式,你就能用更复杂的模式字W串分割开来——要是交l?SPAN class=original_words>StringTokenizer</SPAN>的话Q事情会ȝ得多。我可以很有把握地说Q正则表辑ּ可以取代<span id="ksuoiiw" class=original_words>StringTokenizer</SPAN>?</P> <P>要想q一步学习正则表辑ּQ徏议你?CITE><span id="skeoyoe" class=original_words>Mastering Regular Expression, 2nd Edition</SPAN></CITE>Q作者Jeffrey E. F. Friedl (O’Reilly, 2002)?/P> <P id=header69><STRONG>ȝ</STRONG></P> <P>Java的I/O类库应该能满你的基本需求:你可以用它来d控制収ͼ文gQ内存,甚至是Internet。你q可以利用承来创徏新的输入和输出类型。你甚至可以利用Java会自动调用对象的<span id="iyamymk" class=original_words>toString( )</SPAN>Ҏ的特?Java仅有?自动cd转换")Q通过重新定义q个ҎQ来对要传给的对象做一个简单的扩展?/P> <P>但是Java的I/O类库及其文档还是留下了一些缺憾。比方说你打开一个文件往里面写东西,但是q个文g已经有了Q这么做会把原先的内容给覆盖?。这时要是能有一个异常就好了——有些编E语a能让你规定只能往新徏的文仉输出。看来Java是要你用<span id="aiamooe" class=original_words>File</SPAN>对象来判断文件是否存在,因ؓ如果你用<span id="ckegkyg" class=original_words>FileOutputStream</SPAN>?SPAN class=original_words>FileWriter</SPAN>的话Q文件就会被覆盖了?/P> <P>我对I/O类库的评h是比较矛盄Q它实能干很多事情Q而且做到了跨q_。但是如果你不懂decorator模式Q就会觉得这U设计太隄解了Q所以无论是对老师q是学生Q都得多q力。此外这个类库也不完_否则我也用不着d<span id="geykwck" class=original_words>TextFile</SPAN>了。此外它没有提供格式化输出的功能Q而其他语a都已l提供了q种功能?/P> <P>但是Q一旦你真正理解了decorator模式Qƈ且能开始灵z运用这个类库的时候,你就能感受到q种设计的好处了。这时多写几行代码就不了什么了?</P> <P>如果你觉得不解(本章只是做个介绍Q没惌面面俱到)Q可以去看Elliotte Rusty Harold 写的<CITE>Java I/O</CITE> (O’Reilly, 1999)。这本书讲得更深?/P><img src ="http://www.tkk7.com/bluesky/aggbug/18610.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/bluesky/" target="_blank">bluesky</a> 2005-11-07 17:06 <a href="http://www.tkk7.com/bluesky/articles/18610.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对象和对象变?/title><link>http://www.tkk7.com/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.tkk7.com/bluesky/articles/18597.html</guid><wfw:comment>http://www.tkk7.com/bluesky/comments/18597.html</wfw:comment><comments>http://www.tkk7.com/bluesky/articles/18597.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/bluesky/comments/commentRss/18597.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/bluesky/services/trackbacks/18597.html</trackback:ping><description><![CDATA[<STRONG>对象和类</STRONG> <BR>面向对象E序设计(OOP)Q类是创建对象的模板或蓝图,从类中构造一个对象,卛_Zq个cȝ一个实?instance)?<BR>面向对象中,装(encapsulation)是把数据和行为结合在一个包中,q对对象的用者隐藏数据的实现q程?<BR>对象中的数据UCؓ对象的实例字D?instance field)Q操作数据的函数和过E称为对象的Ҏ(method)。实现封装的 <BR>关键在于决不让方法直接访问其他类的实例字Dc程序应该只能通过对象的方法和对象的数据交互。OOP的另一个原则, <BR>l承?inheritance)Q得很Ҏ在java中编写自qc? <BR><BR><STRONG>对象</STRONG> <BR>要用OOPQ需要了解对象的三个主要Ҏ,分别?<BR>对象的行?behavior) 对象能做什么,或者说Q能对对象施加什么方?<BR>对象的状?state) 对对象施加方法时Q对象如何反?<BR>对象的n?identity) 对象如何与具有相同行为、状态的其他对象怺区别 <BR>cM间的关系 <BR>依赖("use-a") 在AcM的方法操作了Bcd象,那么AcM赖于Bc?<BR>聚合("has-a") Acd象包含Bcd象,q包含Ccd象等{,聚合关系意味着cA的对象包含类B的对?<BR>l承("is-a") l承关系用来表示更特D的和更一般的cM间的关系 <BR><BR><STRONG>对象和对象变?/STRONG> <BR>要用对象,使用构造器(constructor)来构造新的实例。构造器是一个专门的ҎQ构造器名称和类名相?<BR>因此Datecȝ构造器名ؓDate。要构造Date对象Q只要在构造器前面加上newq算W就行了Q如new Date() <BR>q个表达式即构造了一个新的对象,通常我们需要把对象存储在一个变量中 Date birthday = new Date(); <BR>birthday是一个对象变量,对象变量(birthday)指向新构造的对象(new Date)。对象变量ƈ不包含对象, <BR>它只是指向一个对象。在java中,M对象变量的值都是指向存储在别处的对象的一个引用。new q算W的q回 <BR>g是一个引用?img src ="http://www.tkk7.com/bluesky/aggbug/18597.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/bluesky/" target="_blank">bluesky</a> 2005-11-07 16:15 <a href="http://www.tkk7.com/bluesky/articles/18597.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>IOd文g~码是utf-8的文?/title><link>http://www.tkk7.com/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.tkk7.com/bluesky/articles/18588.html</guid><wfw:comment>http://www.tkk7.com/bluesky/comments/18588.html</wfw:comment><comments>http://www.tkk7.com/bluesky/articles/18588.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/bluesky/comments/commentRss/18588.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/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.tkk7.com/bluesky/aggbug/18588.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/bluesky/" target="_blank">bluesky</a> 2005-11-07 15:51 <a href="http://www.tkk7.com/bluesky/articles/18588.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>代码中通过代理讉K外部资源Q{_http://www.tkk7.com/bluesky/articles/18586.htmlblueskyblueskyMon, 07 Nov 2005 07:44:00 GMThttp://www.tkk7.com/bluesky/articles/18586.htmlhttp://www.tkk7.com/bluesky/comments/18586.htmlhttp://www.tkk7.com/bluesky/articles/18586.html#Feedback0http://www.tkk7.com/bluesky/comments/commentRss/18586.htmlhttp://www.tkk7.com/bluesky/services/trackbacks/18586.htmlhttp://spaces.msn.com/members/savagardn/Blog/cns!1pzu-0W6-WxMGeUbOUArh6Eg!105.entry

目前|络上最行的协议就是HTTP协议。HTTP协议有许多优点,例如它能够穿防火墙。同时HTTP也是很多其他协议的基Q例如SOAP协议是建立在HTTP协议之上的?

Java通过两种API对HTTP提供支持Q一U是servlet APIQ它覆盖了服务器端的~程问题Q另一U是java.net包,它通过HttpURLConnectioncd客户端提供了对HTTP协议的支持。但是我在用servlet API的时候曾l遇到过一些问题。本文将介绍我曾遇到q的一些问题和相应的解军_法?

基础知识

通常在企业网l中Q一个终端通过代理服务器同互联|连接v来,代理服务器负责监视网l流量和执行安全规则。在java.net API中,软g可以通过两个属性来支持代理服务器,它们分别是http.proxyHost和http.proxyPort。它们必被讑֮为相应的代理服务器和端口Q下面的代码展示了如何设定这两个属性:

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);


在上面的E序中,你需要根据实际情况设定代理服务器和端口。如果你不知道该如何讄的话Q可以询问你们公司的|络理员?

使用验证

通常公司会要求员工在q接C联网之前dC理服务器。通过dq种机制使公司可以更好的监控员工对互联网的用,例如监控员工都访问了哪些|站。HttpURLConnection通过验证cL持代理服务器验证。下面是一个如何利用HttpURLConnectionc进行验证的例子。首先需要实C个验证者:

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());
   }
}


然后Q通过Authenticator.setDefault()Ҏ注册验证者:

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);


问题

上面介绍的都是HttpURLConnectionc能够正常工作的情况。但是我在实际情况中遇到了一些网l,在这些网l中QHttpURLConnectioncL法正常工作。最后我发现关键的问题在于用了不正的DNS配置。在实际情况中,HttpURLConnectioncLL先尝试利用DNS服务器来解析地址名称。通常情况下,q种试会失败,而代理服务器会将q接重新定向。但是某些DNS服务器会q回一些不正确的响应,从而导致程序抛出UnknownHostException异常。作Z个程序员Q系l不会ؓ了你的程序更改DNS服务器的配置Q因此你需要找册个问题的Ҏ。我的方案是通过自己实现HTTP协议来解册个问题。例如一个GET命o可以用下面的代码来实玎ͼ

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 && 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();


在上面的代码中,大家可以注意C理服务器的用户名和密码的格式?

username:passwordQ?

q且E序对它们基于Base 64q行了编码。如果需要参考HTTP协议Q可以到http://www.ietf.org/rfc/rfc2616.txt?


bluesky 2005-11-07 15:44 发表评论
]]>
cd对象的概?/title><link>http://www.tkk7.com/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.tkk7.com/bluesky/articles/18583.html</guid><wfw:comment>http://www.tkk7.com/bluesky/comments/18583.html</wfw:comment><comments>http://www.tkk7.com/bluesky/articles/18583.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/bluesky/comments/commentRss/18583.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/bluesky/services/trackbacks/18583.html</trackback:ping><description><![CDATA[<P>cL把现实世界中h相同属性和行ؓ的物体抽象出来,用来构造对象实体的 模板?/P> <P>cL创徏对象的模ѝ?/P> <P>对象是类的实例?/P><img src ="http://www.tkk7.com/bluesky/aggbug/18583.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/bluesky/" target="_blank">bluesky</a> 2005-11-07 15:15 <a href="http://www.tkk7.com/bluesky/articles/18583.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> վ֩ģ壺 <a href="http://hivzx.com" target="_blank">18վѹۿ</a>| <a href="http://020iws.com" target="_blank">ۺ϶</a>| <a href="http://222mp3.com" target="_blank">պѲ</a>| <a href="http://43caopen.com" target="_blank">˳Ƶ69</a>| <a href="http://xian66.com" target="_blank">ƷƬ߻</a>| <a href="http://joob4s.com" target="_blank">޴ɫ߹ۿ</a>| <a href="http://appmofun.com" target="_blank">޲Ļ</a>| <a href="http://mt181.com" target="_blank">99߹ۿ</a>| <a href="http://6789311.com" target="_blank">Ů18ëƬؼһƵ</a>| <a href="http://znboxcdn107.com" target="_blank">99Ƶ</a>| <a href="http://www827556.com" target="_blank">þùѹۿƷ</a>| <a href="http://baicaijia666.com" target="_blank">aëƬȫ</a>| <a href="http://tc116.com" target="_blank">޴߶ר</a>| <a href="http://jdvgo.com" target="_blank">avպavav</a>| <a href="http://43sihu.com" target="_blank">ۺϼƵ</a>| <a href="http://sds54.com" target="_blank">þþƷƷް</a>| <a href="http://7272004.com" target="_blank">ɫһ</a>| <a href="http://zhnetbar.com" target="_blank">Ƶ</a>| <a href="http://www-66409b.com" target="_blank">ֻˬ͵ëƬ</a>| <a href="http://zhaosifuwang.com" target="_blank">ʵƵѹۿ </a>| <a href="http://hberay.com" target="_blank">޾ƷŮþ777777</a>| <a href="http://www8x5188x.com" target="_blank">Ʒ</a>| <a href="http://bdykdq.com" target="_blank">Ʒv߹ۿ</a>| <a href="http://by6174.com" target="_blank">һƷһAVһи</a>| <a href="http://ge2hao.com" target="_blank">鶹ƵѲ</a>| <a href="http://8k91.com" target="_blank">ҵС身߿Ѹ </a>| <a href="http://www-741.com" target="_blank">ŮƵһ</a>| <a href="http://yiuuu.com" target="_blank">337pŷ޴</a>| <a href="http://wwwby1385.com" target="_blank">þþƷƷëƬ</a>| <a href="http://mysb123.com" target="_blank">avۺɫ</a>| <a href="http://cn-taoqigui.com" target="_blank">޹˳߹ۿ69վ</a>| <a href="http://whlnrsqsie.com" target="_blank">˳վۿ</a>| <a href="http://xmjcjc.com" target="_blank">޹Ʒþһ </a>| <a href="http://91ptv.com" target="_blank">ɫaѿ</a>| <a href="http://liangdy.com" target="_blank">vպv</a>| <a href="http://whknd.com" target="_blank">ҹƬ</a>| <a href="http://kfdingrui.com" target="_blank">Avۺ辫Ʒ</a>| <a href="http://sdtuoliuta.com" target="_blank">鶹һ</a>| <a href="http://aqd1987.com" target="_blank">߹ۿ԰Ƶ</a>| <a href="http://js-jiarui.com" target="_blank">һëƬѲ</a>| <a href="http://www1688mz.com" target="_blank">߹ۿƬAVվ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>