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()删除字串头尾多余的空格?
小说推荐
- 软件工程实践者的思想(PDF格式)
- -Page 1-大 道 至 简—软件工程实践者的思想周爱民(Aimingoo 著-Page 2-序2004 年 11 月初爱民(Aimingoo)第一次把他的书稿给我,我翻看了一下,第一反应讲的是感想。这不错,在技
- 最新章:第26章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 最新章:第309章
- VB2008从入门到精通(PDF格式英文版)
- -Page 1(R)The eXperT’s Voice inBeginningVB 2008From Novice to ProfessionalChristian Gross-Page 2-Page 3-Beginning VB 2008From Novice to Professional■C
- 最新章:第214章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 最新章:第4章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 最新章:第28章
- C语言实例教程(PDF格式)
- -Page 1-前 言Visual C+是开发运行于Windows 95和Windows NT环境下的Win32应用程序的可视化编程工具中最重要的成员之一,它为软件开发人员提供了完整的编辑、编译和调试工具和建立于Win32 API(ApplicationProgramming Interface)基
- 最新章:第143章
- 神祗之眼 (正式版)第7卷(全文完)作者:百里芜虚
- 第五十一章离12月25日的圣诞节还有两天,纽约已经换上了节日的盛装,几天前的一场大雪令整个城市一片银白。人们笑逐颜开,都纷纷开始为节日做准备,商家为了在节日打开销路纷纷推出各种优惠促销手段,纽约的大街小巷全都这样热闹。依沙那被老婆打发出来买过节要用的杂货,而女儿则和凯妮一起到百货商场去买衣服去了。对
- 最新章:第23章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 最新章:第27章
- php程序设计简明教程(DOC格式)
- -Page 1-PHP 程序设计简明教程PHP 讲义 第 1 页 共 90 页-Page 2-目录序 4第一章 PHP 简介 6
- 最新章:第31章