友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
狗狗书籍 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

Java编程思想第4版[中文版](PDF格式)-第268章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!



    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 语句已经作为注释标志出来了。由于这是本章的第一个程序,所以package 语句是必需 

的,用它告诉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()删除字串头尾多余的空格�
返回目录 上一页 下一页 回到顶部 赞(1) 踩(1)
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!