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

📄 tij0111.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0110.html">Prev</a> | <a href="tij0112.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
The
File class
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index1057"></A><A NAME="Index1058"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">class
has a deceiving name &#8211; you might think it refers to a file, but it
doesn&#8217;t. It can represent either the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>name</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
of a particular file or the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>names</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
of a set of files in a directory. If it&#8217;s a set of files, you can ask for
the set with the <A NAME="Index1059"></A><A NAME="Index1060"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method, and this returns an array of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
It makes sense to return an array rather than one of the flexible collection
classes because the number of elements is fixed, and if you want a different
directory listing you just create a different 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. In fact, &#8220;FilePath&#8221; would have been a better name. This
section shows a complete example of the use of this class, including the
associated <A NAME="Index1061"></A><A NAME="Index1062"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilenameFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc375545391"></a><a name="_Toc408018618"></a><P></DIV>
<A NAME="Heading312"></A><H3 ALIGN=LEFT>
A
directory lister
<P><A NAME="Index1063"></A><A NAME="Index1064"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Suppose
you&#8217;d like to see a directory listing. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object can be listed in two ways. If you call 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with no arguments, you&#8217;ll get the full list that the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object contains. However, if you want a restricted list, for example, all of
the files with an extension of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
then you use a &#8220;directory filter,&#8221; which is a class that tells how
to select the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects for display.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here&#8217;s
the code for the example: (See page 
<A HREF=" PAGE#Running_programs">97</A>
if you have trouble executing this program.)
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: DirList.java</font>
<font color="#009900">// Displays directory listing</font>
<font color="#0000ff">package</font> c10;
<font color="#0000ff">import</font> java.io.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> DirList {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    <font color="#0000ff">try</font> {
      File path = <font color="#0000ff">new</font> File(".");
      String[] list;
      <font color="#0000ff">if</font>(args.length == 0)
        list = path.list();
      <font color="#0000ff">else</font> 
        list = path.list(<font color="#0000ff">new</font> DirFilter(args[0]));
      <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; list.length; i++)
        System.out.println(list[i]);
    } <font color="#0000ff">catch</font>(Exception e) {
      e.printStackTrace();
    }
  }
}

<font color="#0000ff">class</font> DirFilter <font color="#0000ff">implements</font> FilenameFilter {
  String afn;
  DirFilter(String afn) { <font color="#0000ff">this</font>.afn = afn; }
  <font color="#0000ff">public</font> <font color="#0000ff">boolean</font> accept(File dir, String name) {
    <font color="#009900">// Strip path information:</font>
    String f = <font color="#0000ff">new</font> File(name).getName();
    <font color="#0000ff">return</font> f.indexOf(afn) != -1;
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DirFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class &#8220;implements&#8221; the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilenameFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
(Interfaces were covered in Chapter 7.) It&#8217;s useful to see how simple the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilenameFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#0000ff">public</font> <font color="#0000ff">interface</font> FilenameFilter {
  <font color="#0000ff">boolean</font> accept(File dir, String name);
}</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It
says that all that this type of object does is provide a method called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>accept(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The whole reason behind the creation of this class is to provide the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>accept(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method to the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method so that 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>call
back
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>accept(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to determine which file names should be included in the list. Thus, this
technique is often referred to as a <A NAME="Index1065"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>callback</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or sometimes a <A NAME="Index1066"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>functor</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(that is, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DirFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is a functor because its only job is to hold a method). Because 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
takes a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilenameFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object as its argument, it means that you can pass an object of any class that
implements 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilenameFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to choose (even at run-time) how the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method will behave. The purpose of a callback is to provide flexibility in the
behavior of code.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DirFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
shows that just because an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
contains only a set of methods, you&#8217;re not restricted to writing only
those methods. (You must at least provide definitions for all the methods in an
interface, however.) In this case, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DirFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
constructor is also created.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>accept(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method must accept a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object representing the directory that a particular file is found in, and a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
containing the name of that file. You might choose to use or ignore either of
these arguments, but you will probably at least use the file name. Remember
that the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method is calling 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>accept(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for each of the file names in the directory object to see which one should be
included &#8211; this is indicated by the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
result returned by 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>accept(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
make sure that what you&#8217;re working with is only the name and contains no
path information, all you have to do is take the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object and create a 

⌨️ 快捷键说明

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