📄 chap11.htm
字号:
</FONT><BR></P></DIV>
<A NAME="Heading356"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Anonymous inner classes</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This example is ideal for rewriting using
an
<A NAME="Index1143"></A><A NAME="Index1144"></A><A NAME="Index1145"></A>anonymous
inner class (described in Chapter 8). As a first cut, a method <B>filter( )
</B>is created that returns a reference to a
<B>FilenameFilter</B>:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c11:DirList2.java</font>
<font color=#009900>// Uses anonymous inner classes.</font>
<font color=#0000ff>import</font> java.io.*;
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>import</font> com.bruceeckel.util.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> DirList2 {
<font color=#0000ff>public</font> <font color=#0000ff>static</font> FilenameFilter
filter(<font color=#0000ff>final</font> String afn) {
<font color=#009900>// Creation of anonymous inner class:</font>
<font color=#0000ff>return</font> <font color=#0000ff>new</font> FilenameFilter() {
String fn = afn;
<font color=#0000ff>public</font> <font color=#0000ff>boolean</font> accept(File dir, String n) {
<font color=#009900>// Strip path information:</font>
String f = <font color=#0000ff>new</font> File(n).getName();
<font color=#0000ff>return</font> f.indexOf(fn) != -1;
}
}; <font color=#009900>// End of anonymous inner class</font>
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
File path = <font color=#0000ff>new</font> File(<font color=#004488>"."</font>);
String[] list;
<font color=#0000ff>if</font>(args.length == 0)
list = path.list();
<font color=#0000ff>else</font>
list = path.list(filter(args[0]));
Arrays.sort(list,
<font color=#0000ff>new</font> AlphabeticComparator());
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < list.length; i++)
System.out.println(list[i]);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Note that the argument to
<B>filter( )</B> must be
<A NAME="Index1146"></A><A NAME="Index1147"></A><B>final</B>. This is required
by the anonymous inner class so that it can use an object from outside its
scope.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I12'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I13>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This design is an improvement because the
<B>FilenameFilter</B> class is now tightly bound to <B>DirList2</B>. However,
you can take this approach one step further and define the anonymous inner class
as an argument to <B>list( )</B>, in which case it’s even
smaller:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c11:DirList3.java</font>
<font color=#009900>// Building the anonymous inner class "in-place."</font>
<font color=#0000ff>import</font> java.io.*;
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>import</font> com.bruceeckel.util.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> DirList3 {
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(<font color=#0000ff>final</font> String[] args) {
File path = <font color=#0000ff>new</font> File(<font color=#004488>"."</font>);
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> FilenameFilter() {
<font color=#0000ff>public</font> <font color=#0000ff>boolean</font>
accept(File dir, String n) {
String f = <font color=#0000ff>new</font> File(n).getName();
<font color=#0000ff>return</font> f.indexOf(args[0]) != -1;
}
});
Arrays.sort(list,
<font color=#0000ff>new</font> AlphabeticComparator());
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < list.length; i++)
System.out.println(list[i]);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The argument to <B>main( )</B> is
now <B>final</B>, since the anonymous inner class uses <B>args[0]</B> directly.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I13'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I14>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This shows you how anonymous inner
classes allow the creation of quick-and-dirty classes to solve problems. Since
everything in Java revolves around classes, this can be a useful coding
technique. One benefit is that it keeps the code that solves a particular
problem isolated together in one spot. On the other hand, it is not always as
easy to read, so you must use it judiciously.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I14'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I15>
</FONT><A NAME="_Toc375545392"></A><A NAME="_Toc481064737"></A><BR></P></DIV>
<A NAME="Heading357"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Checking for and creating directories</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>File</B> class is more than just a
representation for an existing file or directory. You can also use a <B>File</B>
object to create a new <A NAME="Index1148"></A><A NAME="Index1149"></A>directory
or an entire directory path if it doesn’t exist. You can also look at the
<A NAME="Index1150"></A><A NAME="Index1151"></A>characteristics of files (size,
last modification date, read/write), see whether a <B>File</B> object represents
a file or a directory, and delete a file. This program shows some of the other
methods available with the <B>File</B> class (see the HTML documentation from
<I>java.sun.com </I>for the full set):</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c11:MakeDirectories.java</font>
<font color=#009900>// Demonstrates the use of the File class to</font>
<font color=#009900>// create directories and manipulate files.</font>
<font color=#0000ff>import</font> java.io.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> MakeDirectories {
<font color=#0000ff>private</font> <font color=#0000ff>final</font> <font color=#0000ff>static</font> String usage =
<font color=#004488>"Usage:MakeDirectories path1 ...\n"</font> +
<font color=#004488>"Creates each path\n"</font> +
<font color=#004488>"Usage:MakeDirectories -d path1 ...\n"</font> +
<font color=#004488>"Deletes each path\n"</font> +
<font color=#004488>"Usage:MakeDirectories -r path1 path2\n"</font> +
<font color=#004488>"Renames from path1 to path2\n"</font>;
<font color=#0000ff>private</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> usage() {
System.err.println(usage);
System.exit(1);
}
<font color=#0000ff>private</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> fileData(File f) {
System.out.println(
<font color=#004488>"Absolute path: "</font> + f.getAbsolutePath() +
<font color=#004488>"\n Can read: "</font> + f.canRead() +
<font color=#004488>"\n Can write: "</font> + f.canWrite() +
<font color=#004488>"\n getName: "</font> + f.getName() +
<font color=#004488>"\n getParent: "</font> + f.getParent() +
<font color=#004488>"\n getPath: "</font> + f.getPath() +
<font color=#004488>"\n length: "</font> + f.length() +
<font color=#004488>"\n lastModified: "</font> + f.lastModified());
<font color=#0000ff>if</font>(f.isFile())
System.out.println(<font color=#004488>"it's a file"</font>);
<font color=#0000ff>else</font> <font color=#0000ff>if</font>(f.isDirectory())
System.out.println(<font color=#004488>"it's a directory"</font>);
}
<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 < 1) usage();
<font color=#0000ff>if</font>(args[0].equals(<font color=#004488>"-r"</font>)) {
<font color=#0000ff>if</font>(args.length != 3) usage();
File
old = <font color=#0000ff>new</font> File(args[1]),
rname = <font color=#0000ff>new</font> File(args[2]);
old.renameTo(rname);
fileData(old);
fileData(rname);
<font color=#0000ff>return</font>; <font color=#009900>// Exit main</font>
}
<font color=#0000ff>int</font> count = 0;
<font color=#0000ff>boolean</font> del = <font color=#0000ff>false</font>;
<font color=#0000ff>if</font>(args[0].equals(<font color=#004488>"-d"</font>)) {
count++;
del = <font color=#0000ff>true</font>;
}
<font color=#0000ff>for</font>( ; count < args.length; count++) {
File f = <font color=#0000ff>new</font> File(args[count]);
<font color=#0000ff>if</font>(f.exists()) {
System.out.println(f + <font color=#004488>" exists"</font>);
<font color=#0000ff>if</font>(del) {
System.out.println(<font color=#004488>"deleting..."</font> + f);
f.delete();
}
}
<font color=#0000ff>else</font> { <font color=#009900>// Doesn't exist</font>
<font color=#0000ff>if</font>(!del) {
f.mkdirs();
System.out.println(<font color=#004488>"created "</font> + f);
}
}
fileData(f);
}
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In <B>fileData( )</B> you can see
various file investigation methods used to display information about the file or
directory path.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I15'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I16>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The first method that’s exercised
by <B>main( )</B> is
<A NAME="Index1152"></A><A NAME="Index1153"></A><B>renameTo( )</B>, which
allows you to rename (or move) a file to an entirely new path represented by the
argument, which is another <B>File</B> object. This also works with directories
of any length.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I16'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I17>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you experiment with the above program,
you’ll find that you can make a directory path of any complexity because
<A NAME="Index1154"></A><A NAME="Index1155"></A><B>mkdirs( )</B> will do
all the work for you.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I17'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I18>
</FONT><A NAME="_Toc481064738"></A><BR></P></DIV>
<A NAME="Heading358"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Input and
output<BR><A NAME="Index1156"></A><A NAME="Index1157"></A><A NAME="Index1158"></A><A NAME="Index1159"></A><A NAME="Index1160"></A><A NAME="Index1161"></A><A NAME="Index1162"></A><A NAME="Index1163"></A><A NAME="Index1164"></A><A NAME="Index1165"></A><A NAME="Index1166"></A><A NAME="Index1167"></A><A NAME="Index1168"></A><A NAME="Index1169"></A><A NAME="Index1170"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">I/O libraries often use the abstraction
of a <I>stream</I>, which represents any data source or sink as an object
capable of producing or receiving pieces of data. The stream hides the details
of what happens to the data inside the actual I/O device.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I18'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I19>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Java library classes for I/O are
divided by input and output, as you can see by looking at the online Java class
hierarchy with your Web browser. By inheritance, everything derived from the
<B>InputStream</B> or <B>Reader </B>classes have basic methods called
<B>read( ) </B>for reading a single byte or array of bytes. Likewise,
everything derived from <B>OutputStream </B>or <B>Writer </B>classes have basic
methods called <B>write( ) </B>for writing a single byte or array of bytes.
However, you won’t generally use these methods; they exist so that other
classes can use them—these other classes provide a more useful interface.
Thus, you’ll rarely create your stream object by using a single class, but
instead will layer multiple objects together to provide your desired
functionality. The fact that you create more than one object to create a single
resulting stream is the primary reason that Java’s stream library is
confusing.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I19'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I20>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It’s helpful to categorize the
classes by their functionality. In Java 1.0, the library designers started by
deciding that all classes that had anything to do with input would be inherited
from <B>InputStream</B> and all classes that were associated with output would
be inherited from <B>OutputStream</B>.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I20'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I21>
</FONT><A NAME="_Toc375545383"></A><A NAME="_Toc481064739"></A><BR></P></DIV>
<A NAME="Heading359"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Types of InputStream</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B>’s job is to
represent classes that produce input from different sources. These sources can
be: </FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">An array of
bytes.</FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">A
<B>String</B>
object.</FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">A
file.</FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">A
“pipe,” which works like a
physical</FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">
<A NAME="Index1171"></A><A NAME="Index1172"></A>pipe: you put things in one end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -