Java±à³Ì˼ÏëµÚ4°æ[ÖÐÎÄ°æ](PDF¸ñʽ)-µÚ161²¿·Ö
°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡£ý¡¡¡¡
£ý¡¡¡¡
¡¡¡¡
public¡¡class¡¡CodePackager¡¡£û¡¡¡¡
¡¡¡¡private¡¡static¡¡final¡¡String¡¡usageString¡¡=¡¡¡¡
¡¡¡¡¡¨usage£º¡¡java¡¡CodePackager¡¡packedFileName¡¨¡¡£«¡¡¡¡
¡¡¡¡¡¨nExtracts¡¡source¡¡code¡¡files¡¡from¡¡packed¡¡n¡¨¡¡£«¡¡¡¡
¡¡¡¡¡¨version¡¡of¡¡Tjava¡£doc¡¡sources¡¡into¡¡¡¨¡¡£«¡¡¡¡
¡¡¡¡¡¨directories¡¡off¡¡current¡¡directory¡¡n¡¨¡¡£«¡¡¡¡
¡¡¡¡¡¨java¡¡CodePackager¡¡packedFileName¡¡newDirn¡¨¡¡£«¡¡¡¡
¡¡¡¡¡¨Extracts¡¡into¡¡directories¡¡off¡¡newDirn¡¨¡¡£«¡¡¡¡
¡¡¡¡¡¨java¡¡CodePackager¡¡¡p¡¡source¡£txt¡¡packedFile¡¨¡¡£«¡¡¡¡
¡¡¡¡¡¨nCreates¡¡packed¡¡version¡¡of¡¡source¡¡files¡¨¡¡£«¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡628¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡630¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¨nfrom¡¡text¡¡version¡¡of¡¡Tjava¡£doc¡¨£»¡¡¡¡
¡¡¡¡private¡¡static¡¡void¡¡usage£¨£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡System¡£err¡£println£¨usageString£©£»¡¡¡¡
¡¡¡¡¡¡¡¡System¡£exit£¨1£©£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
¡¡¡¡public¡¡static¡¡void¡¡main£¨String£§£§¡¡args£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡if£¨args¡£length¡¡==¡¡0£©¡¡usage£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡if£¨args£§0£§¡£equals£¨¡¨¡p¡¨£©£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡if£¨args¡£length¡¡£¡=¡¡3£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡usage£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡createPackedFile£¨args£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡else¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡if£¨args¡£length¡¡¡·¡¡2£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡usage£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡extractPackedFile£¨args£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡£ý¡¡¡¡
¡¡¡¡private¡¡static¡¡String¡¡currentLine£»¡¡¡¡¡¡
¡¡¡¡private¡¡static¡¡BufferedReader¡¡in£»¡¡¡¡
¡¡¡¡private¡¡static¡¡DirMap¡¡dm£»¡¡¡¡
¡¡¡¡private¡¡static¡¡void¡¡¡¡¡¡
¡¡¡¡createPackedFile£¨String£§£§¡¡args£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡dm¡¡=¡¡new¡¡DirMap£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡in¡¡=¡¡IO¡£disOpen£¨args£§1£§£©£»¡¡¡¡
¡¡¡¡¡¡¡¡try¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡while£¨£¨currentLine¡¡=¡¡in¡£readLine£¨£©£©¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¡=¡¡null£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡if£¨currentLine¡£startsWith£¨¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡SourceCodeFile¡£startMarker£©£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡dm¡£add£¨new¡¡SourceCodeFile£¨¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡currentLine£»¡¡in£©£©£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡else¡¡if£¨currentLine¡£startsWith£¨¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡SourceCodeFile¡£endMarker£©£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Pr¡£error£¨¡¨file¡¡has¡¡no¡¡start¡¡marker¡¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡//¡¡Else¡¡ignore¡¡the¡¡input¡¡line¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡catch£¨IOException¡¡e£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡Pr¡£error£¨¡¨Error¡¡reading¡¡¡¨¡¡£«¡¡args£§1£§£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡IO¡£close£¨in£©£»¡¡¡¡
¡¡¡¡¡¡¡¡dm¡£writePackedFile£¨args£§2£§£©£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
¡¡¡¡private¡¡static¡¡void¡¡¡¡¡¡
¡¡¡¡extractPackedFile£¨String£§£§¡¡args£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡if£¨args¡£length¡¡==¡¡2£©¡¡//¡¡Alternate¡¡directory¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡dm¡¡=¡¡new¡¡DirMap£¨args£§1£§£©£»¡¡¡¡
¡¡¡¡¡¡¡¡else¡¡//¡¡Current¡¡directory¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡dm¡¡=¡¡new¡¡DirMap£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡in¡¡=¡¡IO¡£disOpen£¨args£§0£§£©£»¡¡¡¡
¡¡¡¡¡¡¡¡String¡¡s¡¡=¡¡null£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡629¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡631¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡try¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡s¡¡=¡¡in¡£readLine£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡catch£¨IOException¡¡e£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡Pr¡£error£¨¡¨Cannot¡¡read¡¡from¡¡¡¨¡¡£«¡¡in£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡//¡¡Capture¡¡the¡¡separator¡¡used¡¡in¡¡the¡¡system¡¡¡¡
¡¡¡¡¡¡¡¡//¡¡that¡¡packed¡¡the¡¡file£º¡¡¡¡
¡¡¡¡¡¡¡¡if£¨s¡£indexOf£¨¡¨###Old¡¡Separator£º¡¨£©¡¡£¡=¡¡¡1¡¡£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡String¡¡oldsep¡¡=¡¡s¡£substring£¨¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¨###Old¡¡Separator£º¡¨¡£length£¨£©£©£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡oldsep¡¡=¡¡oldsep¡£substring£¨¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡0£»¡¡oldsep¡£¡¡indexOf£¨'#'£©£©£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡SourceCodeFile¡£oldsep¡¡=¡¡oldsep£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡SourceCodeFile¡¡sf¡¡=¡¡new¡¡SourceCodeFile£¨in£©£»¡¡¡¡
¡¡¡¡¡¡¡¡while£¨sf¡£hasFile£¨£©£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡dm¡£add£¨sf£©£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡sf¡¡=¡¡new¡¡SourceCodeFile£¨in£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡dm¡£write£¨£©£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
£ý¡¡///£º~¡¡¡¡
¡¡¡¡
ÎÒÃÇ×¢Òâµ½¡¡package¡¡Óï¾äÒѾ×÷ΪעÊͱêÖ¾³öÀ´ÁË¡£ÓÉÓÚÕâÊDZ¾ÕµĵÚÒ»¸ö³ÌÐò£¬ËùÒÔpackage¡¡Óï¾äÊDZØÐè¡¡
µÄ£¬ÓÃËü¸æËßCodePackager¡¡ÒѸĻ»µ½ÁíÒ»Õ¡£µ«ÊÇ°ÑËü·ÅÈë°üÀïÈ´»á³ÉΪһ¸öÎÊÌâ¡£µ±ÎÒÃÇ´´½¨Ò»¸ö°üµÄʱ¡¡
ºò£¬ÐèÒª½«½á¹û³ÌÐòͬһ¸öÌض¨µÄĿ¼½á¹¹ÁªÏµÔÚÒ»Æð£¬ÕâÒ»×ö·¨¶Ô±¾ÊéµÄ´ó¶àÊýÀý×Ó¶¼ÊÇÊÊÓõġ£µ«ÔÚÕâ¡¡
ÀCodePackager¡¡³ÌÐò±ØÐëÔÚÒ»¸öרÓõÄĿ¼Àï±àÒëºÍÔËÐУ¬ËùÒÔ¡¡package¡¡Óï¾ä×÷ΪעÊͱê¼Ç³öÈ¥¡£µ«¶Ô¡¡
CodePackager¡¡À´Ëµ£¬Ëü¡°¿´ÆðÀ´¡±ÒÀÈ»ÏóÒ»¸öÆÕͨµÄpackage¡¡Óï¾ä£¬ÒòΪ³ÌÐò»¹²»ÊÇÌرð¸´ÔÓ£¬²»ÄÜÕì²éµ½¡¡
¶àÐÐ×¢ÊÍ£¨Ã»ÓбØÒª×öµÃÕâô¸´ÔÓ£¬ÕâÀïÖ»ÒªÇó·½±ã¾ÍÐУ©¡£¡¡¡¡
Í·Á½¸öÀàÊÇ¡°Ö§³Ö£¯¹¤¾ß¡±À࣬×÷ÓÃÊÇʹ³ÌÐòÊ£ÓàµÄ²¿·ÖÔÚ±àдʱ¸ü¼ÓÁ¬¹á£¬Ò²¸ü±ãÓÚÔĶÁ¡£µÚÒ»¸öÊÇPr£¬¡¡
ËüÀàËÆANSI¡¡C¡¡µÄperror¡¡¿â£¬Á½Õ߶¼ÄÜ´òÓ¡³öÒ»Ìõ´íÎóÌáʾÏûÏ¢£¨µ«Í¬Ê±Ò²»áÍ˳ö³ÌÐò£©¡£µÚ¶þ¸öÀཫÎļþ¡¡
µÄ´´½¨¹ý³Ì·â×°ÔÚÄÚ£¬Õâ¸ö¹ý³ÌÒÑÔÚµÚ10Õ½éÉܹýÁË£»´ó¼ÒÒѾ֪µÀ£¬ÕâÑù×öºÜ¿ì¾Í»á±äµÃ·Ç³£ÀÛ׸ºÍÂé¡¡
·³¡£Îª½â¾öÕâ¸öÎÊÌ⣬µÚ¡¡10¡¡ÕÂÌṩµÄ·½°¸ÖÂÁ¦ÓÚÐÂÀàµÄ´´½¨£¬µ«Õâ¶ùµÄ¡°¾²Ì¬¡±·½·¨ÒѾʹÓùýÁË¡£ÔÚÄÇЩ¡¡
·½·¨ÖУ¬Õý³£µÄÎ¥Àý»á±»²¶»ñ£¬²¢ÏàÓ¦µØ½øÐд¦Àí¡£ÕâЩ·½·¨Ê¹Ê£ÓàµÄ´úÂëÏԵøü¼ÓÇåˬ£¬¸üÒ×ÔĶÁ¡£¡¡¡¡
°ïÖú½â¾öÎÊÌâµÄµÚÒ»¸öÀàÊÇSourceCodeFile¡¡£¨Ô´ÂëÎļþ£©£¬Ëü´ú±í±¾ÊéÒ»¸öÔ´ÂëÎļþ°üº¬µÄËùÓÐÐÅÏ¢£¨ÄÚ¡¡
ÈÝ¡¢ÎļþÃûÒÔ¼°Ä¿Â¼£©¡£Ëüͬʱ»¹°üº¬ÁËһϵÁÐString¡¡³£Êý£¬·Ö±ð´ú±íÒ»¸öÎļþµÄ¿ªÊ¼Óë½áÊø£»ÔÚ´ò°üÎļþ¡¡
ÄÚʹÓõÄÒ»¸ö±ê¼Ç£»µ±Ç°ÏµÍ³µÄ»»Ðзû£»Îļþ·¾¶·Ö¸ô·û£¨×¢ÒâÒªÓÃSystem¡£getProperty£¨£©Õì²é±¾µØ°æ±¾ÊÇ¡¡
ʲô£©£»ÒÔ¼°Ò»´ó¶Î°æȨÉùÃ÷£¬ËüÊÇ´ÓÏÂÃæÕâ¸öCopyright¡£txt¡¡ÎļþÀïÌáÈ¡³öÀ´µÄ£º¡¡¡¡
¡¡¡¡
//////////////////////////////////////////////////¡¡¡¡
//¡¡Copyright¡¡£¨c£©¡¡Bruce¡¡Eckel£»¡¡1998¡¡¡¡
//¡¡Source¡¡code¡¡file¡¡from¡¡the¡¡book¡¡¡¨Thinking¡¡in¡¡Java¡¨¡¡¡¡
//¡¡All¡¡rights¡¡reserved¡¡EXCEPT¡¡as¡¡allowed¡¡by¡¡the¡¡¡¡
//¡¡following¡¡statements£º¡¡You¡¡may¡¡freely¡¡use¡¡this¡¡file¡¡¡¡
//¡¡for¡¡your¡¡own¡¡work¡¡£¨personal¡¡or¡¡mercial£©£»¡¡¡¡
//¡¡including¡¡modifications¡¡and¡¡distribution¡¡in¡¡¡¡
//¡¡executable¡¡form¡¡only¡£¡¡Permission¡¡is¡¡granted¡¡to¡¡use¡¡¡¡
//¡¡this¡¡file¡¡in¡¡classroom¡¡situations£»¡¡including¡¡its¡¡¡¡
//¡¡use¡¡in¡¡presentation¡¡materials£»¡¡as¡¡long¡¡as¡¡the¡¡book¡¡¡¡
//¡¡¡¨Thinking¡¡in¡¡Java¡¨¡¡is¡¡cited¡¡as¡¡the¡¡source¡£¡¡¡¡¡¡
//¡¡Except¡¡in¡¡classroom¡¡situations£»¡¡you¡¡may¡¡not¡¡copy¡¡¡¡
//¡¡and¡¡distribute¡¡this¡¡code£»¡¡instead£»¡¡the¡¡sole¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡630¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡632¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
//¡¡distribution¡¡point¡¡is¡¡http£º//BruceEckel¡£¡¡¡¡¡¡
//¡¡£¨and¡¡official¡¡mirror¡¡sites£©¡¡where¡¡it¡¡is¡¡¡¡
//¡¡freely¡¡available¡£¡¡You¡¡may¡¡not¡¡remove¡¡this¡¡¡¡
//¡¡copyright¡¡and¡¡notice¡£¡¡You¡¡may¡¡not¡¡distribute¡¡¡¡
//¡¡modified¡¡versions¡¡of¡¡the¡¡source¡¡code¡¡in¡¡this¡¡¡¡
//¡¡package¡£¡¡You¡¡may¡¡not¡¡use¡¡this¡¡file¡¡in¡¡printed¡¡¡¡
//¡¡media¡¡without¡¡the¡¡express¡¡permission¡¡of¡¡the¡¡¡¡
//¡¡author¡£¡¡Bruce¡¡Eckel¡¡makes¡¡no¡¡representation¡¡about¡¡¡¡
//¡¡the¡¡suitability¡¡of¡¡this¡¡software¡¡for¡¡any¡¡purpose¡£¡¡¡¡
//¡¡It¡¡is¡¡provided¡¡¡¨as¡¡is¡¨¡¡without¡¡express¡¡or¡¡implied¡¡¡¡
//¡¡warranty¡¡of¡¡any¡¡kind£»¡¡including¡¡any¡¡implied¡¡¡¡
//¡¡warranty¡¡of¡¡merchantability£»¡¡fitness¡¡for¡¡a¡¡¡¡
//¡¡particular¡¡purpose¡¡or¡¡non¡infringement¡£¡¡The¡¡entire¡¡¡¡
//¡¡risk¡¡as¡¡to¡¡the¡¡quality¡¡and¡¡performance¡¡of¡¡the¡¡¡¡
//¡¡software¡¡is¡¡with¡¡you¡£¡¡Bruce¡¡Eckel¡¡and¡¡the¡¡¡¡
//¡¡publisher¡¡shall¡¡not¡¡be¡¡liable¡¡for¡¡any¡¡damages¡¡¡¡
//¡¡suffered¡¡by¡¡you¡¡or¡¡any¡¡third¡¡party¡¡as¡¡a¡¡result¡¡of¡¡¡¡
//¡¡using¡¡or¡¡distributing¡¡software¡£¡¡In¡¡no¡¡event¡¡will¡¡¡¡
//¡¡Bruce¡¡Eckel¡¡or¡¡the¡¡publisher¡¡be¡¡liable¡¡for¡¡any¡¡¡¡
//¡¡lost¡¡revenue£»¡¡profit£»¡¡or¡¡data£»¡¡or¡¡for¡¡direct£»¡¡¡¡
//¡¡indirect£»¡¡special£»¡¡consequential£»¡¡incidental£»¡¡or¡¡¡¡
//¡¡punitive¡¡damages£»¡¡however¡¡caused¡¡and¡¡regardless¡¡of¡¡¡¡
//¡¡the¡¡theory¡¡of¡¡liability£»¡¡arising¡¡out¡¡of¡¡the¡¡use¡¡of¡¡¡¡
//¡¡or¡¡inability¡¡to¡¡use¡¡software£»¡¡even¡¡if¡¡Bruce¡¡Eckel¡¡¡¡
//¡¡and¡¡the¡¡publisher¡¡have¡¡been¡¡advised¡¡of¡¡the¡¡¡¡
//¡¡possibility¡¡of¡¡such¡¡damages¡£¡¡Should¡¡the¡¡software¡¡¡¡
//¡¡prove¡¡defective£»¡¡you¡¡assume¡¡the¡¡cost¡¡of¡¡all¡¡¡¡
//¡¡necessary¡¡servicing£»¡¡repair£»¡¡or¡¡correction¡£¡¡If¡¡you¡¡¡¡
//¡¡think¡¡you've¡¡found¡¡an¡¡error£»¡¡please¡¡email¡¡all¡¡¡¡
//¡¡modified¡¡files¡¡with¡¡clearly¡¡mented¡¡changes¡¡to£º¡¡¡¡
//¡¡Bruce@EckelObjects¡£¡£¡¡£¨please¡¡use¡¡the¡¡same¡¡¡¡
//¡¡address¡¡for¡¡non¡code¡¡errors¡¡found¡¡in¡¡the¡¡book£©¡£¡¡¡¡
//////////////////////////////////////////////////¡¡¡¡
¡¡¡¡
´ÓÒ»¸ö´ò°üÎļþÖÐÌáÈ¡Îļþʱ£¬µ±³õËùÓÃϵͳµÄÎļþ·Ö¸ô·ûÒ²»á±ê×¢³öÀ´£¬ÒÔ±ãÓñ¾µØϵͳÊÊÓõķûºÅÌæ»»¡¡
Ëü¡£¡¡¡¡
µ±Ç°ÕµÄ×ÓĿ¼±£´æÔÚ¡¡chapter×Ö¶ÎÖУ¬Ëü³õʼ»¯³Éc02¡¡£¨´ó¼Ò¿É×¢ÒâһϵÚ2¡¡ÕµÄÁбíÕýºÃûÓаüº¬Ò»¸ö¡¡
´ò°üÓï¾ä£©¡£Ö»ÓÐÔÚµ±Ç°ÎļþÀï·¢ÏÖÒ»¸ö¡¡package¡¡£¨´ò°ü£©Óï¾äʱ£¬chapter×ֶβŻᷢÉú¸Ä±ä¡£¡¡¡¡
¡¡¡¡
1¡£¡¡¹¹½¨Ò»¸ö´ò°üÎļþ¡¡¡¡
µÚÒ»¸ö¹¹½¨Æ÷ÓÃÓÚ´Ó±¾ÊéµÄASCII¡¡Îı¾°æÀïÌáÈ¡³öÒ»¸öÎļþ¡£·¢³öµ÷ÓõĴúÂ루ÔÚÁбíÀï½ÏÉîµÄµØ·½£©»á¶Á¡¡
Èë²¢¼ì²éÿһÐУ¬Ö±µ½ÕÒµ½ÓëÒ»¸öÁбíµÄ¿ªÍ·Ïà·ûµÄΪֹ¡£ÔÚÕâ¸öʱºò£¬Ëü¾Í»áн¨Ò»¸öSourceCodeFile¡¡¶Ô¡¡
Ï󣬽«µÚÒ»ÐеÄÄÚÈÝ£¨ÒѾÓɵ÷ÓôúÂë¶ÁÈëÁË£©´«µÝ¸øËü£¬Í¬Ê±»¹Òª´«µÝBufferedReader¡¡¶ÔÏó£¬ÒÔ±ãÔÚÕâ¸ö¡¡
»º³åÇøÖÐÌáÈ¡Ô´ÂëÁбíÊ£ÓàµÄÄÚÈÝ¡£¡¡¡¡
´ÓÕâʱÆ𣬴ó¼Ò»á·¢ÏÖ¡¡String¡¡·½·¨±»Æµ·±ÔËÓá£ÎªÌáÈ¡³öÎļþÃû£¬Ðèµ÷ÓÃsubstring£¨£©µÄ¹ýÔØ°æ±¾£¬ÁîÆä´Ó¡¡
Ò»¸öÆðʼƫÒÆ¿ªÊ¼£¬Ò»Ö±¶Áµ½×Ö´®µÄĩ⣬´Ó¶øÐγÉÒ»¸ö¡°×Ó´®¡±¡£ÎªËã³öÕâ¸öÆðʼË÷Òý£¬ÏÈÒªÓÃlength£¨£©¡¡
µÃ³ö¡¡startMarker¡¡µÄ×ܳ¤£¬ÔÙÓÃtrim£¨£©É¾³ý×Ö´®Í·Î²¶àÓàµÄ¿Õ¸ñ¡£µÚÒ»ÐÐÔÚÎļþÃûºóÒ²¿ÉÄÜÓÐһЩ×Ö·û£»Ëü¡¡
ÃÇÊÇÓá¡indexOf£¨£©Õì²â³öÀ´µÄ¡£ÈôûÓз¢ÏÖÕÒµ½ÎÒÃÇÏëÑ°ÕÒµÄ×Ö·û£¬¾Í·µ»Ø¡1£»ÈôÕÒµ½ÄÇЩ×Ö·û£¬¾Í·µ»ØËü¡¡
ÃǵÚÒ»´Î³öÏÖµÄλÖá£×¢ÒâÕâÒ²ÊÇ¡¡indexOf£¨£©µÄÒ»¸ö¹ýÔØ°æ±¾£¬²ÉÓÃÒ»¸ö×Ö´®×÷Ϊ²ÎÊý£¬¶ø·ÇÒ»¸ö×Ö·û¡£¡¡¡¡
½âÎö³ö²¢±£´æºÃÎļþÃûºó£¬µÚÒ»ÐлᱻÖÃÈë×Ö´®contents¡¡ÖУ¨¸Ã×Ö´®ÓÃÓÚ±£´æÔ´ÂëÇåµ¥µÄÍêÕûÕýÎÄ£©¡£Ëæ¡¡
ºó£¬½«Ê£ÓàµÄ´úÂëÐжÁÈ룬²¢ºÏ²¢½øÈëcontents¡¡×Ö´®¡£µ±È»ÊÂÇ鲢ûÓÐÏëÏóµÄÄÇô¼òµ¥£¬ÒòΪÌض¨µÄÇé¿öÐè¡¡
¼ÓÒÔÌرðµÄ¿ØÖÆ¡£Ò»ÖÖÇé¿öÊÇ´íÎó¼ì²é£ºÈôÖ±½ÓÓöµ½Ò»¸ö¡¡startMarker¡¡£¨Æðʼ±ê¼Ç£©£¬±íÃ÷µ±Ç°²Ù×÷µÄÕâ¸ö¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡631¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡633¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
´úÂëÁбíûÓÐÉèÖÃÒ»¸ö½áÊø±ê¼Ç¡£ÕâÊôÓÚÒ»¸ö³ö´íÌõ¼þ£¬ÐèÒªÍ˳ö³ÌÐò¡£¡¡¡¡
ÁíÒ»ÖÖÌØÊâÇé¿öÓëpackage¡¡¹Ø¼ü×ÖÓйء£¾¡¹ÜJava¡¡ÊÇÒ»ÖÖ×ÔÓÉÐÎʽµÄÓïÑÔ£¬µ«Õâ¸ö³ÌÐòÒªÇó¡¡package¡¡¹Ø¼ü×Ö¡¡
±ØÐëλÓÚÐÐÊס£Èô·¢ÏÖ¡¡package¡¡¹Ø¼ü×Ö£¬¾Íͨ¹ý¼ì²éλÓÚ¿ªÍ·µÄ¿Õ¸ñÒÔ¼°Î»ÓÚĩβµÄ·ÖºÅ£¬´Ó¶øÌáÈ¡³ö°üÃû¡¡
¡¡£¨×¢ÒâÒà¿ÉÒ»´Îµ¥¶ÀµÄ²Ù×÷ʵÏÖ£¬·½·¨ÊÇʹÓùýÔصÄsubstring£¨£©£¬ÁîÆäͬʱ¼ì²éÆðʼºÍ½áÊøË÷ÒýλÖã©¡£¡¡
Ëæºó£¬½«°üÃûÖеĵãºÅÌæ»»³ÉÌض¨µÄÎļþ·Ö¸ô·û¡ª¡ªµ±È»£¬ÕâÀïÒª¼ÙÉèÎļþ·Ö¸ô·û½öÓÐÒ»¸ö×Ö·ûµÄ³¤¶È¡£¾¡¡¡
¹ÜÕâ¸ö¼ÙÉè¿ÉÄܶÔÄ¿Ç°µÄËùÓÐϵͳ¶¼ÊÇÊÊÓõģ¬µ«Ò»µ©Óöµ½ÎÊÌ⣬һ¶¨²»ÒªÍüÁ˼ì²éÒ»ÏÂÕâÀï¡£¡¡¡¡
ĬÈϲÙ×÷Êǽ«Ã¿Ò»Ðж¼Á¬½Óµ½¡¡contents¡¡Àͬʱ»¹Óл»ÐÐ×Ö·û£¬Ö±µ½Óöµ½Ò»¸ö¡¡endMarker¡¡£¨½áÊø±ê¼Ç£©Îª¡¡
Ö¹¡£¸Ã±ê¼ÇÖ¸³ö¹¹½¨Æ÷Ó¦µ±Í£Ö¹ÁË¡£ÈôÔÚ¡¡endMarker¡¡Ö®Ç°Óöµ½ÁËÎļþ½á⣬¾ÍÈÏΪ´æÔÚÒ»¸ö´íÎ󡣡¡¡¡
¡¡¡¡
2¡£¡¡´Ó´ò°üÎļþÖÐÌáÈ¡¡¡¡¡
µÚ¶þ¸ö¹¹½¨Æ÷ÓÃÓÚ½«Ô´ÂëÎļþ´Ó´ò°üÎļþÖлָ´£¨ÌáÈ¡£©³öÀ´¡£ÔÚÕâ¶ù£¬×÷Ϊµ÷ÓÃÕߵķ½·¨²»±Øµ£ÐÄ»áÌø¹ý¡¡
һЩÖмäÎı¾¡£´ò°üÎļþ°üº¬ÁËËùÓÐÔ´ÂëÎļþ£¬ËüÃÇÏ໥¼ä½ôÃܵؿ¿ÔÚÒ»Æð¡£ÐèÒª´«µÝ¸ø¸Ã¹¹½¨Æ÷µÄ½ö½öÊÇ¡¡
Ò»¸öBufferedReader£¬Ëü´ú±í×Å¡°ÐÅÏ¢Ô´¡±¡£¹¹½¨Æ÷»á´ÓÖÐÌáÈ¡³ö×Ô¼ºÐèÒªµÄÐÅÏ¢¡£µ«ÔÚÿ¸ö´úÂëÁÐ±í¿ªÊ¼¡¡
µÄµØ·½»¹ÓÐһЩÅäÖÃÐÅÏ¢£¬ËüÃǵÄÉí·ÝÊÇÓÃpackMarker¡¡£¨´ò°ü±ê¼Ç£©Ö¸³öµÄ¡£ÈôpackMarker¡¡²»´æÔÚ£¬Òâζ¡¡
×ŵ÷ÓÃÕßÊÔͼÓôíÎóµÄ·½·¨À´Ê¹ÓÃÕâ¸ö¹¹½¨Æ÷¡£¡¡¡¡
Ò»µ©·¢ÏÖpackMarker£¬¾Í»á½«Æä°þÀë³öÀ´£¬²¢ÌáÈ¡³öĿ¼Ãû£¨ÓÃÒ»¸ö'#'½á⣩ÒÔ¼°ÎļþÃû£¨Ö±µ½ÐÐÄ©£©¡£¡¡
²»¹ÜÔÚÄÄÖÖÇé¿öÏ£¬¾É·Ö¸ô·û¶¼»á±»Ìæ»»³É±¾µØÊÊÓõÄÒ»¸ö·Ö¸ô·û£¬Õ