📄 chapter17.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source:
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:Tjava14.rtf
Application Directory:c:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:02/04/2000
Translation Time:23:26:21
Translation Platform:Win32
Number of Output files:27
This File:Chapter17.html
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>17: Projects</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
<a href="http://www.MindView.net">
<img src="mindview-head.gif" alt="MindView Inc." BORDER = "0"></a>
<CENTER>
<FONT FACE="Verdana" size = "-1">
[ <a href="README-HTML.txt">Viewing Hints</a> ]
[ <a href="http://www.mindview.net/TIJ2/index.html">2nd Edition</a> ]
[ <a href="http://www.mindview.net/MailingList.html">Free Newsletter</a> ] <br>
[ <a href="http://www.mindview.net/Training.html">Seminars</a> ]
[ <a href="http://www.mindview.net/javaCD2.html">Seminars on CD ROM</a> ]
[ <a href="http://www.mindview.net/CPPServices/#ConsultingServices">Consulting</a> ]
</FONT>
<H2><FONT FACE="Verdana">
Thinking in Java, 1st edition</FONT></H2>
<H3><FONT FACE="Verdana">©1998 by Bruce Eckel</FONT></H3>
<FONT FACE="Verdana" size = "-1">
[ <a href="Chapter16.html">Previous Chapter</a> ]
[ <a href="SimpleContents.html">Short TOC</a> ]
[ <a href="Contents.html">Table of Contents</a> ]
[ <a href="DocIndex.html">Index</a> ]
[ <a href="AppendixA.html">Next Chapter</a> ]
</FONT>
</CENTER>
</P></DIV><A NAME="_Toc407441461"></A><A NAME="_Toc408018809"></A><A NAME="Heading570"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
17: Projects</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana" SIZE=4>This chapter includes a
set of projects that build on the material presented in this book or otherwise
didn’t fit in earlier chapters.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Most of these projects are
significantly more complex than the examples in the rest of the book, and they
often demonstrate new techniques and uses of class
libraries.</FONT><A NAME="_Toc375545502"></A><A NAME="_Toc408018810"></A><BR></P></DIV>
<A NAME="Heading571"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Text processing<BR><A NAME="Index2999"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you come from a C or C++
background, you might be skeptical at first of Java’s power when it comes
to handling text. Indeed, one drawback is that execution speed is slower and
that could hinder some of your efforts. However, the tools (in particular the
<A NAME="Index3000"></A><B>String</B> class) are quite powerful, as the examples
in this section show (and performance improvements have been promised for
Java).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As you’ll see, these examples
were created to solve problems that arose in the creation of this book. However,
they are not restricted to that and the solutions they offer can easily be
adapted to other situations. In addition, they show the power of Java in an area
that has not previously been emphasized in this
book.</FONT><A NAME="_Toc375545503"></A><A NAME="_Toc408018811"></A><BR></P></DIV>
<A NAME="Heading572"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Extracting code
listings<BR><A NAME="Index3001"></A><A NAME="Index3002"></A><A NAME="Index3003"></A><A NAME="Index3004"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You’ve no doubt noticed that
each complete code listing (not code fragment) in this book begins and ends with
special comment tag marks ‘<B>//:</B>’ and
‘<B>///:~</B>’. This meta-information is included so that the code
can be automatically extracted from the book into compilable source-code files.
In my previous book, I had a system that allowed me to automatically incorporate
tested code files into the book. In this book, however, I discovered that it was
often easier to paste the code into the book once it was initially tested and,
since it’s hard to get right the first time, to perform edits to the code
within the book. But how to extract it and test the code? This program is the
answer, and it could come in handy when you set out to solve a text processing
problem. It also demonstrates many of the <B>String</B> class
features.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">I first save the entire book in
ASCII text format into a separate file. The <B>CodePackager</B> program has two
modes (which you can see described in <B>usageString</B>): if you use the
<B>-p</B> flag, it expects to see an input file containing the ASCII text from
the book. It will go through this file and use the comment tag marks to extract
the code, and it uses the file name on the first line to determine the name of
the file. In addition, it looks for the <B>package</B> statement in case it
needs to put the file into a special directory (chosen via the path indicated by
the <B>package</B> statement).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But that’s not all. It also
watches for the change in chapters by keeping track of the package names. Since
all packages for each chapter begin with <B>c02</B>, <B>c03</B>, <B>c04</B>,
etc. to indicate the chapter where they belong<B> </B>(except for those
beginning with <B>com</B>, which are ignored for the purpose of keeping track of
chapters), as long as the first listing in each chapter contains a
<B>package</B> statement with the chapter number, the <B>CodePackager</B>
program can keep track of when the chapter changed and put all the subsequent
files in the new chapter subdirectory.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As each file is extracted, it is
placed into a <B>SourceCodeFile</B> object that is then placed into a
collection. (This process will be more thoroughly described later.) These
<B>SourceCodeFile</B> objects could simply be stored in files, but that brings
us to the second use for this project. If you invoke <B>CodePackager</B>
<I>without</I> the <B>-p</B> flag it expects a “packed” file as
input, which it will then extract into separate files. So the <B>-p</B> flag
means that the extracted files will be found “packed” into this
single file.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Why bother with the packed file?
Because different computer platforms have different ways of storing text
information in files. A big issue is the end-of-line character or characters,
but other issues can also exist. However, Java has a special type of IO stream
– the <A NAME="Index3005"></A><A NAME="Index3006"></A><B>DataOutputStream
</B>– which promises that, regardless of what machine the data is coming
from, the storage of that data will be in a form that can be correctly retrieved
by any other machine by using a
<A NAME="Index3007"></A><A NAME="Index3008"></A><B>DataInputStream</B>. That is,
Java handles all of the <A NAME="Index3009"></A>platform-specific details, which
is a large part of the promise of Java. So the <B>-p</B> flag stores everything
into a single file in a universal format. You download this file and the Java
program from the Web, and when you run <B>CodePackager</B> on this file
<I>without</I> the <B>-p</B> flag the files will all be extracted to appropriate
places on your system. (You can specify an alternate subdirectory; otherwise the
subdirectories will just be created in the current directory.) To ensure that no
system-specific formats remain, <B>File</B> objects are used everywhere a path
or a file is described. In addition, there’s a sanity check: an empty file
is placed in each subdirectory; the name of that file indicates how many files
you should find in that subdirectory.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here is the code, which will be
described in detail at the end of the listing:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: CodePackager.java</font>
<font color=#009900>// "Packs" and "unpacks" the code in "Thinking </font>
<font color=#009900>// in Java" for cross-platform distribution.</font>
<font color=#009900>/* Commented so CodePackager sees it and starts
a new chapter directory, but so you don't
have to worry about the directory where this
program lives:
package c17;
*/</font>
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>import</font> java.io.*;
<font color=#0000ff>class</font> Pr {
<font color=#0000ff>static</font> <font color=#0000ff>void</font> error(String e) {
System.err.println(<font color=#004488>"ERROR: "</font> + e);
System.exit(1);
}
}
<font color=#0000ff>class</font> IO {
<font color=#0000ff>static</font> BufferedReader disOpen(File f) {
BufferedReader in = <font color=#0000ff>null</font>;
<font color=#0000ff>try</font> {
in = <font color=#0000ff>new</font> BufferedReader(
<font color=#0000ff>new</font> FileReader(f));
} <font color=#0000ff>catch</font>(IOException e) {
Pr.error(<font color=#004488>"could not open "</font> + f);
}
<font color=#0000ff>return</font> in;
}
<font color=#0000ff>static</font> BufferedReader disOpen(String fname) {
<font color=#0000ff>return</font> disOpen(<font color=#0000ff>new</font> File(fname));
}
<font color=#0000ff>static</font> DataOutputStream dosOpen(File f) {
DataOutputStream in = <font color=#0000ff>null</font>;
<font color=#0000ff>try</font> {
in = <font color=#0000ff>new</font> DataOutputStream(
<font color=#0000ff>new</font> BufferedOutputStream(
<font color=#0000ff>new</font> FileOutputStream(f)));
} <font color=#0000ff>catch</font>(IOException e) {
Pr.error(<font color=#004488>"could not open "</font> + f);
}
<font color=#0000ff>return</font> in;
}
<font color=#0000ff>static</font> DataOutputStream dosOpen(String fname) {
<font color=#0000ff>return</font> dosOpen(<font color=#0000ff>new</font> File(fname));
}
<font color=#0000ff>static</font> PrintWriter psOpen(File f) {
PrintWriter in = <font color=#0000ff>null</font>;
<font color=#0000ff>try</font> {
in = <font color=#0000ff>new</font> PrintWriter(
<font color=#0000ff>new</font> BufferedWriter(
<font color=#0000ff>new</font> FileWriter(f)));
} <font color=#0000ff>catch</font>(IOException e) {
Pr.error(<font color=#004488>"could not open "</font> + f);
}
<font color=#0000ff>return</font> in;
}
<font color=#0000ff>static</font> PrintWriter psOpen(String fname) {
<font color=#0000ff>return</font> psOpen(<font color=#0000ff>new</font> File(fname));
}
<font color=#0000ff>static</font> <font color=#0000ff>void</font> close(Writer os) {
<font color=#0000ff>try</font> {
os.close();
} <font color=#0000ff>catch</font>(IOException e) {
Pr.error(<font color=#004488>"closing "</font> + os);
}
}
<font color=#0000ff>static</font> <font color=#0000ff>void</font> close(DataOutputStream os) {
<font color=#0000ff>try</font> {
os.close();
} <font color=#0000ff>catch</font>(IOException e) {
Pr.error(<font color=#004488>"closing "</font> + os);
}
}
<font color=#0000ff>static</font> <font color=#0000ff>void</font> close(Reader os) {
<font color=#0000ff>try</font> {
os.close();
} <font color=#0000ff>catch</font>(IOException e) {
Pr.error(<font color=#004488>"closing "</font> + os);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -