📄 tij0185.html
字号:
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < v.size(); i++) {
SourceCodeFile f =
(SourceCodeFile)v.elementAt(i);
f.writePacked(packed);
}
}
IO.close(packed);
}
<font color="#009900">// Write all the files in their directories:</font>
<font color="#0000ff">public</font> <font color="#0000ff">void</font> write() {
Enumeration e = t.keys();
<font color="#0000ff">while</font>(e.hasMoreElements()) {
String dir = (String)e.nextElement();
Vector v = (Vector)t.get(dir);
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < v.size(); i++) {
SourceCodeFile f =
(SourceCodeFile)v.elementAt(i);
f.writeFile(rootpath);
}
<font color="#009900">// Add file indicating file quantity</font>
<font color="#009900">// written to this directory as a check:</font>
IO.close(IO.dosOpen(
<font color="#0000ff">new</font> File(<font color="#0000ff">new</font> File(rootpath, dir),
Integer.toString(v.size())+".files")));
}
}
}
<font color="#0000ff">public</font> <font color="#0000ff">class</font> CodePackager {
<font color="#0000ff">private</font> <font color="#0000ff">static</font> <font color="#0000ff">final</font> 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 newDir\n" +
"Extracts into directories off newDir\n" +
"java CodePackager -p source.txt packedFile" +
"\nCreates packed version of source files" +
"\nfrom text version of Tjava.doc";
<font color="#0000ff">private</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> usage() {
System.err.println(usageString);
System.exit(1);
}
<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 == 0) usage();
<font color="#0000ff">if</font>(args[0].equals("-p")) {
<font color="#0000ff">if</font>(args.length != 3)
usage();
createPackedFile(args);
}
<font color="#0000ff">else</font> {
<font color="#0000ff">if</font>(args.length > 2)
usage();
extractPackedFile(args);
}
}
<font color="#0000ff">private</font> <font color="#0000ff">static</font> String currentLine;
<font color="#0000ff">private</font> <font color="#0000ff">static</font> BufferedReader in;
<font color="#0000ff">private</font> <font color="#0000ff">static</font> DirMap dm;
<font color="#0000ff">private</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font>
createPackedFile(String[] args) {
dm = <font color="#0000ff">new</font> DirMap();
in = IO.disOpen(args[1]);
<font color="#0000ff">try</font> {
<font color="#0000ff">while</font>((currentLine = in.readLine())
!= <font color="#0000ff">null</font>) {
<font color="#0000ff">if</font>(currentLine.startsWith(
SourceCodeFile.startMarker)) {
dm.add(<font color="#0000ff">new</font> SourceCodeFile(
currentLine, in));
}
<font color="#0000ff">else</font> <font color="#0000ff">if</font>(currentLine.startsWith(
SourceCodeFile.endMarker))
Pr.error("file has no start marker");
<font color="#009900">// Else ignore the input line</font>
}
} <font color="#0000ff">catch</font>(IOException e) {
Pr.error("Error reading " + args[1]);
}
IO.close(in);
dm.writePackedFile(args[2]);
}
<font color="#0000ff">private</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font>
extractPackedFile(String[] args) {
<font color="#0000ff">if</font>(args.length == 2) <font color="#009900">// Alternate directory</font>
dm = <font color="#0000ff">new</font> DirMap(args[1]);
<font color="#0000ff">else</font> <font color="#009900">// Current directory</font>
dm = <font color="#0000ff">new</font> DirMap();
in = IO.disOpen(args[0]);
String s = <font color="#0000ff">null</font>;
<font color="#0000ff">try</font> {
s = in.readLine();
} <font color="#0000ff">catch</font>(IOException e) {
Pr.error("Cannot read from " + in);
}
<font color="#009900">// Capture the separator used in the system</font>
<font color="#009900">// that packed the file:</font>
<font color="#0000ff">if</font>(s.indexOf("###Old Separator:") != -1 ) {
String oldsep = s.substring(
"###Old Separator:".length());
oldsep = oldsep.substring(
0, oldsep. indexOf('#'));
SourceCodeFile.oldsep = oldsep;
}
SourceCodeFile sf = <font color="#0000ff">new</font> SourceCodeFile(in);
<font color="#0000ff">while</font>(sf.hasFile()) {
dm.add(sf);
sf = <font color="#0000ff">new</font> SourceCodeFile(in);
}
dm.write();
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You’ll
first notice the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement that is commented out. Since this is the first program in the
chapter, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">statement
is necessary to tell
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CodePackager
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">that
the chapter has changed, but putting it in a package would be a problem. When
you create a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you tie the resulting program to a particular directory structure, which is
fine for most of the examples in this book. Here, however, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CodePackager</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
program must be compiled and run from an arbitrary directory, so the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement is commented out. It will still
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>look</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
like an ordinary
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CodePackager</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
though, since the program isn’t sophisticated enough to detect multi-line
comments. (It has no need for such sophistication, a fact that comes in handy
here.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first two classes are support/utility classes designed to make the rest of the
program more consistent to write and easier to read. The first,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Pr</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
is similar to the ANSI C library
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>perror</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
since it prints an error message (but also exits the program). The second class
encapsulates the creation of files, a process that was shown in Chapter 10 as
one that rapidly becomes verbose and annoying. In Chapter 10, the proposed
solution created new classes, but here
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">method
calls are used. Within those methods the appropriate exceptions are caught and
dealt with. These methods make the rest of the code much cleaner to read.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first class that helps solve the problem is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SourceCodeFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which represents all the information (including the contents, file name, and
directory) for one source code file in the book. It also contains a set of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
constants representing the markers that start and end a file, a marker used
inside the packed file, the current system’s end-of-line separator and
file path separator (notice the use of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.getProperty( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to get the local version), and a copyright notice, which is extracted from the
following file
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Copyright.txt</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//////////////////////////////////////////////////</font>
<font color="#009900">// Copyright (c) Bruce Eckel, 1998</font>
<font color="#009900">// Source code file from the book "Thinking in Java"</font>
<font color="#009900">// All rights reserved EXCEPT as allowed by the</font>
<font color="#009900">// following statements: You may freely use this file</font>
<font color="#009900">// for your own work (personal or commercial),</font>
<font color="#009900">// including modifications and distribution in</font>
<font color="#009900">// executable form only. Permission is granted to use</font>
<font color="#009900">// this file in classroom situations, including its</font>
<font color="#009900">// use in presentation materials, as long as the book</font>
<font color="#009900">// "Thinking in Java" is cited as the source. </font>
<font color="#009900">// Except in classroom situations, you may not copy</font>
<font color="#009900">// and distribute this code; instead, the sole</font>
<font color="#009900">// distribution point is http://www.BruceEckel.com </font>
<font color="#009900">// (and official mirror sites) where it is</font>
<font color="#009900">// freely available. You may not remove this</font>
<font color="#009900">// copyright and notice. You may not distribute</font>
<font color="#009900">// modified versions of the source code in this</font>
<font color="#009900">// package. You may not use this file in printed</font>
<font color="#009900">// media without the express permission of the</font>
<font color="#009900">// author. Bruce Eckel makes no representation about</font>
<font color="#009900">// the suitability of this software for any purpose.</font>
<font color="#009900">// It is provided "as is" without express or implied</font>
<font color="#009900">// warranty of any kind, including any implied</font>
<font color="#009900">// warranty of merchantability, fitness for a</font>
<font color="#009900">// particular purpose or non-infringement. The entire</font>
<font color="#009900">// risk as to the quality and performance of the</font>
<font color="#009900">// software is with you. Bruce Eckel and the</font>
<font color="#009900">// publisher shall not be liable for any damages</font>
<font color="#009900">// suffered by you or any third party as a result of</font>
<font color="#009900">// using or distributing software. In no event will</font>
<font color="#009900">// Bruce Eckel or the publisher be liable for any</font>
<font color="#009900">// lost revenue, profit, or data, or for direct,</font>
<font color="#009900">// indirect, special, consequential, incidental, or</font>
<font color="#009900">// punitive damages, however caused and regardless of</font>
<font color="#009900">// the theory of liability, arising out of the use of</font>
<font color="#009900">// or inability to use software, even if Bruce Eckel</font>
<font color="#009900">// and the publisher have been advised of the</font>
<font color="#009900">// possibility of such damages. Should the software</font>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -