📄 tij0114.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="tij0113.html">Prev</a> | <a href="tij0115.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
Java
1.1 IO streams
<P><A NAME="Index1181"></A></H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">At
this point you might be scratching your head, wondering if there is another
design for IO streams that could require
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>more</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
typing. Could someone have come up with an odder design?” Prepare
yourself: Java 1.1 makes some significant modifications to the IO stream
library. When you see the <A NAME="Index1182"></A><A NAME="Index1183"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Reader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and <A NAME="Index1184"></A><A NAME="Index1185"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Writer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
classes your first thought (like mine) might be that these were meant to
replace the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>OutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
classes. But that’s not the case. Although some aspects of the original
streams library are deprecated (if you use them you will receive a warning from
the compiler), the old streams have been left in for backwards compatibility and:
</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> New
classes have been put into the old hierarchy, so it’s obvious that Sun is
not abandoning the old streams.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> There
are times when you’re supposed to use classes in the old hierarchy
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>in
combination
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with classes in the new hierarchy and to accomplish this there are
“bridge” classes: <A NAME="Index1186"></A><A NAME="Index1187"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStreamReader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
converts an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStream
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">to
a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Reader
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
<A NAME="Index1188"></A><A NAME="Index1189"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>OutputStreamWriter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
converts an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>OutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Writer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
a result there are situations in which you have
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>more</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
layers of wrapping with the new IO stream library than with the old. Again,
this is a drawback of the decorator pattern – the price you pay for added
flexibility.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
most important reason for adding the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Reader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Writer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
hierarchies in Java 1.1<A NAME="Index1190"></A>
is for <A NAME="Index1191"></A><A NAME="Index1192"></A>internationalization.
The old IO stream hierarchy supports only 8-bit byte streams and doesn’t
handle the 16-bit Unicode characters well. Since Unicode is used for
internationalization (and Java’s native
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>char
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
16-bit <A NAME="Index1193"></A><A NAME="Index1194"></A>Unicode),
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Reader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Writer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
hierarchies were added to support Unicode in all IO operations. In addition,
the new libraries are designed for faster operations than the old.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
is the practice in this book, I will attempt to provide an overview of the
classes but assume that you will use online documentation to determine all the
details, such as the exhaustive list of methods.
</FONT><a name="_Toc408018629"></a><P></DIV>
<A NAME="Heading334"></A><H3 ALIGN=LEFT>
Sources
and sinks of data
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Almost
all of the Java 1.0<A NAME="Index1195"></A>
IO stream classes have corresponding Java 1.1<A NAME="Index1196"></A>
classes to provide native Unicode manipulation. It would be easiest to say
“Always use the new classes, never use the old ones,” but things
are not that simple. Sometimes you are forced into using the Java 1.0 IO stream
classes because of the library design; in particular, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.util.zip</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
libraries are new additions to the old stream library and they rely on old
stream components. So the most sensible approach to take is to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>try</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to use the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Reader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Writer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
classes whenever you can, and you’ll discover the situations when you
have to drop back into the old libraries because your code won’t compile.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here
is a table that shows the correspondence between the sources and sinks of
information (that is, where the data physically comes from or goes to) in the
old and new libraries.
</FONT><P></DIV>
<DIV ALIGN=LEFT><TABLE BORDER>
<COLGROUP>
<COL width="144">
<COL width="175">
</COLGROUP>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Sources
& Sinks:
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
1.0<A NAME="Index1197"></A>
class
</FONT><P></DIV>
</TD>
<TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Corresponding
Java 1.1<A NAME="Index1198"></A>
class
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStream</B></FONT><P></DIV>
</TD>
<A NAME="Index1199"></A><A NAME="Index1200"></A><TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Reader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">converter:
<A NAME="Index1201"></A><A NAME="Index1202"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStreamReader</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>OutputStream</B></FONT><P></DIV>
</TD>
<A NAME="Index1203"></A><A NAME="Index1204"></A><TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Writer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">converter:
<A NAME="Index1205"></A><A NAME="Index1206"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>OutputStreamWriter</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileInputStream</B></FONT><P></DIV>
</TD>
<A NAME="Index1207"></A><A NAME="Index1208"></A><TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileReader</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileOutputStream</B></FONT><P></DIV>
</TD>
<A NAME="Index1209"></A><A NAME="Index1210"></A><TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FileWriter</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringBufferInputStream</B></FONT><P></DIV>
</TD>
<A NAME="Index1211"></A><A NAME="Index1212"></A><TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringReader</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">(no
corresponding class)
</FONT><P></DIV>
</TD>
<A NAME="Index1213"></A><A NAME="Index1214"></A><TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringWriter</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ByteArrayInputStream</B></FONT><P></DIV>
</TD>
<A NAME="Index1215"></A><A NAME="Index1216"></A><TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CharArrayReader</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ByteArrayOutputStream</B></FONT><P></DIV>
</TD>
<A NAME="Index1217"></A><A NAME="Index1218"></A><TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CharArrayWriter</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PipedInputStream</B></FONT><P></DIV>
</TD>
<A NAME="Index1219"></A><A NAME="Index1220"></A><TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PipedReader</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=144 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PipedOutputStream</B></FONT><P></DIV>
</TD>
<A NAME="Index1221"></A><A NAME="Index1222"></A><TD WIDTH=175 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PipedWriter</B></FONT><P></DIV>
</TD>
</TR>
</TABLE></DIV>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
general, you’ll find that the interfaces in the old library components
and the new ones are similar if not identical.
</FONT><a name="_Toc408018630"></a><P></DIV>
<A NAME="Heading335"></A><H3 ALIGN=LEFT>
Modifying
stream behavior
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
Java 1.0<A NAME="Index1223"></A>,
streams were adapted for particular needs using “decorator”
subclasses of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilterInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilterOutputStream.</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
Java 1.1<A NAME="Index1224"></A>
IO streams continues the use of this idea, but the model of deriving all of the
decorators from the same “filter” base class is not followed. This
can make it a bit confusing if you’re trying to understand it by looking
at the class hierarchy.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
the following table, the correspondence is a rougher approximation than in the
previous table. The difference is because of the class organization: while
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BufferedOutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is a subclass of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilterOutputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BufferedWriter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>not</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
a subclass of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilterWriter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(which, even though it is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>abstract</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
has no subclasses and so appears to have been put in either as a placeholder or
simply so you wouldn’t wonder where it was). However, the interfaces to
the classes are quite a close match and it’s apparent that you’re
supposed to use the new versions instead of the old whenever possible (that is,
except in cases where you’re forced to produce a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
instead of a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Reader</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -