📄 tij0111.html
字号:
<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 – you might think it refers to a file, but it
doesn’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’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( )</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, “FilePath” 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’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( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with no arguments, you’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 “directory filter,” 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’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 < 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 “implements” 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’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( )</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( )</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( )</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( )</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( )</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( )</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( )</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’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( )</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( )</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( )</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 – 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( )</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’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 + -