⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0185.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>add(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method is where quite a bit of dense action occurs. First, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>directory(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is extracted from the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SourceCodeFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you want to add, and then the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is examined to see if it contains that key already. If not, a new 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is added to the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and associated with that key. At this point, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is there, one way or another, and it is extracted so the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SourceCodeFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can be added. Because <A NAME="Index3042"></A><A NAME="Index3043"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
can be easily combined with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
like this, the power of both is amplified.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Writing
a packed file involves opening the file to write (as a <A NAME="Index3044"></A><A NAME="Index3045"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataOutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
so the data is universally recoverable) and writing the header information
about the old separator on the first line. Next, an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Enumeration</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keys is produced and stepped through to select each directory and to fetch the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
associated with that directory so each 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SourceCodeFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in that 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can be written to the packed file.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Writing
the Java source files to their directories in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>write(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
almost identical to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>writePackedFile(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
since both methods simply call the appropriate method in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SourceCodeFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Here, however, the root path is passed into 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SourceCodeFile.writeFile(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and when all the files have been written the additional file with the name
containing the number of files is also written.
</FONT><P></DIV>
<A NAME="Heading578"></A><H4 ALIGN=LEFT>
The
main program
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
previously described classes are used within 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CodePackager</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
First you see the usage string that gets printed whenever the end user invokes
the program incorrectly, along with the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>usage(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method that calls it and exits the program. All 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
does is determine whether you want to create a packed file or extract from one,
then it ensures the arguments are correct and calls the appropriate method.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
a packed file is created, it&#8217;s assumed to be made in the current
directory, so the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DirMap</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is created using the default constructor. After the file is opened each line is
read and examined for particular conditions:
</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	If
the line starts with the starting marker for a source code listing, a new 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SourceCodeFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is created. The constructor reads in the rest of the source listing. The
handle that results is directly added to the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DirMap</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	If
the line starts with the end marker for a source code listing, something has
gone wrong, since end markers should be found only by the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SourceCodeFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
constructor.
</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
extracting a packed file, the extraction can be into the current directory or
into an alternate directory, so the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DirMap</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is created accordingly. The file is opened and the first line is read.
The old file path separator information is extracted from this line. Then the
input is used to create the first 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SourceCodeFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object, which is added to the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DirMap</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
New 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SourceCodeFile</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects are created and added as long as they contain a file. (The last one
created will simply return when it runs out of input and then 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>hasFile(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will return false.)
</FONT><a name="_Toc375545504"></a><a name="_Toc408018812"></a><P></DIV>
<A NAME="Heading579"></A><H3 ALIGN=LEFT>
Checking
capitalization style
<P><A NAME="Index3046"></A><A NAME="Index3047"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Although
the previous example can come in handy as a guide for some project of your own
that involves text processing, this project will be directly useful because it
performs a style check to make sure that your capitalization conforms to the
de-facto Java style. It opens each 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file in the current directory and extracts all the class names and identifiers,
then shows you if any of them don&#8217;t meet the Java style.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">For
the program to operate correctly, you must first build a class name repository
to hold all the class names in the standard Java library. You do this by moving
into all the source code subdirectories for the standard Java library and
running 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ClassScanner</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in each subdirectory. Provide as arguments the name of the repository file
(using the same path and name each time) and the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>-a</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
command-line option to indicate that the class names should be added to the
repository.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
use the program to check your code, run it and hand it the path and name of the
repository to use. It will check all the classes and identifiers in the current
directory and tell you which ones don&#8217;t follow the typical Java
capitalization style.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
should be aware that the program isn&#8217;t perfect; there a few times when it
will point out what it thinks is a problem but on looking at the code
you&#8217;ll see that nothing needs to be changed. This is a little annoying,
but it&#8217;s still much easier than trying to find all these cases by staring
at your code.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
explanation immediately follows the listing:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: ClassScanner.java</font>
<font color="#009900">// Scans all files in directory for classes</font>
<font color="#009900">// and identifiers, to check capitalization.</font>
<font color="#009900">// Assumes properly compiling code listings.</font>
<font color="#009900">// Doesn't do everything right, but is a very</font>
<font color="#009900">// useful aid.</font>
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">import</font> java.util.*;

<font color="#0000ff">class</font> MultiStringMap <font color="#0000ff">extends</font> Hashtable {
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> add(String key, String value) {
    <font color="#0000ff">if</font>(!containsKey(key))
      put(key, <font color="#0000ff">new</font> Vector());
    ((Vector)get(key)).addElement(value);
  }
  <font color="#0000ff">public</font> Vector getVector(String key) {
    <font color="#0000ff">if</font>(!containsKey(key)) {
      System.err.println(
        "ERROR: can't find key: " + key);
      System.exit(1);
    }
    <font color="#0000ff">return</font> (Vector)get(key);
  }
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> printValues(PrintStream p) {
    Enumeration k = keys();
    <font color="#0000ff">while</font>(k.hasMoreElements()) {
      String oneKey = (String)k.nextElement();
      Vector val = getVector(oneKey);
      <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; val.size(); i++)
        p.println((String)val.elementAt(i));
    }
  }
}

<font color="#0000ff">public</font> <font color="#0000ff">class</font> ClassScanner {
  <font color="#0000ff">private</font> File path;
  <font color="#0000ff">private</font> String[] fileList;
  <font color="#0000ff">private</font> Properties classes = <font color="#0000ff">new</font> Properties();
  <font color="#0000ff">private</font> MultiStringMap 
    classMap = <font color="#0000ff">new</font> MultiStringMap(),
    identMap = <font color="#0000ff">new</font> MultiStringMap();
  <font color="#0000ff">private</font> StreamTokenizer in;
  <font color="#0000ff">public</font> ClassScanner() {
    path = <font color="#0000ff">new</font> File(".");
    fileList = path.list(<font color="#0000ff">new</font> JavaFilter());
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; fileList.length; i++) {
      System.out.println(fileList[i]);
      scanListing(fileList[i]);
    }
  }
  <font color="#0000ff">void</font> scanListing(String fname) {
    <font color="#0000ff">try</font> {
      in = <font color="#0000ff">new</font> StreamTokenizer(
          <font color="#0000ff">new</font> BufferedReader(
            <font color="#0000ff">new</font> FileReader(fname)));
      <font color="#009900">// Doesn't seem to work:</font>
      <font color="#009900">// in.slashStarComments(true);</font>
      <font color="#009900">// in.slashSlashComments(true);</font>
      in.ordinaryChar('/');
      in.ordinaryChar('.');
      in.wordChars('_', '_');
      in.eolIsSignificant(<font color="#0000ff">true</font>);
      <font color="#0000ff">while</font>(in.nextToken() != 
            StreamTokenizer.TT_EOF) {
        <font color="#0000ff">if</font>(in.ttype == '/')
          eatComments();
        <font color="#0000ff">else</font> <font color="#0000ff">if</font>(in.ttype == 
                StreamTokenizer.TT_WORD) {
          <font color="#0000ff">if</font>(in.sval.equals("<font color="#0000ff">class</font>") || 
             in.sval.equals("<font color="#0000ff">interface</font>")) {
            <font color="#009900">// Get class name:</font>
               <font color="#0000ff">while</font>(in.nextToken() != 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -