📄 chap11.htm
字号:
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Data-OutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Used in concert with
<B>DataInputStream</B> so you can write primitives (int, char, long, etc.) to a
stream in a portable fashion.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>OutputStream</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Contains full interface to allow you to
write primitive types.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>PrintStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For producing formatted output. While
<B>DataOutputStream</B> handles the <I>storage</I> of data, <B>PrintStream</B>
handles <I>display</I>.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>OutputStream</B>, with optional
<B>boolean</B> indicating that the buffer is flushed with every
newline.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Should be the “final”
wrapping for your <B>OutputStream</B> object. You’ll probably use this a
lot.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Buffered-OutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Use this to prevent a physical write
every time you send a piece of data. You’re saying “Use a
buffer.” You can call <B>flush( )</B> to flush the
buffer.</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>OutputStream</B>, with optional buffer
size.</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This doesn’t provide an interface
<I>per se</I>, just a requirement that a buffer is used. Attach an interface
object.</FONT><BR></P></DIV>
</TD>
</TR>
<A NAME="_Toc375545389"></A><A NAME="_Toc481064744"></A></TABLE></P></DIV>
<A NAME="Heading364"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Readers & Writers<BR><A NAME="Index1219"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java 1.1 made some significant
modifications to the fundamental I/O stream library (Java 2, however, did not
make fundamental modifications). When you see the
<A NAME="Index1220"></A><A NAME="Index1221"></A><B>Reader</B> and
<A NAME="Index1222"></A><A NAME="Index1223"></A><B>Writer</B> classes your first
thought (like mine) might be that these were meant to replace the
<B>InputStream</B> and <B>OutputStream</B> 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
<B>InputStream</B> and <B>OutputStream</B> classes still provide valuable
functionality in the form of <B>byte</B>-oriented I/O, while the <B>Reader
</B>and <B>Writer </B>classes provide Unicode-compliant, character-based I/O. In
addition:
<A NAME="Index1224"></A><A NAME="Index1225"></A><A NAME="Index1226"></A><A NAME="Index1227"></A>
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I36'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I37>
</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Java 1.1 added new classes
into the <B>InputStream </B>and <B>OutputStream </B>hierarchy, so it’s
obvious those classes weren’t being replaced.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I37'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I38>
</FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">There
are times when you must use classes from the “byte” hierarchy <I>in
combination</I> with classes in the “character” hierarchy. To
accomplish this there are “bridge” classes: <B>InputStreamReader</B>
converts an <B>InputStream </B>to a <B>Reader </B>and <B>OutputStreamWriter</B>
converts an <B>OutputStream</B> to a <B>Writer</B>.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I38'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I39>
</FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The
most important reason for the <B>Reader</B> and <B>Writer</B> hierarchies is for
<A NAME="Index1228"></A><A NAME="Index1229"></A>internationalization. The old
I/O 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 <B>char </B>is 16-bit
<A NAME="Index1230"></A><A NAME="Index1231"></A>Unicode), the <B>Reader</B> and
<B>Writer</B> hierarchies were added to support Unicode in all I/O operations.
In addition, the new libraries are designed for faster operations than the old.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I39'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I40>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I40'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I41>
</FONT><A NAME="_Toc481064745"></A><BR></P></DIV>
<A NAME="Heading365"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Sources and sinks of data</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Almost all of the original Java I/O
stream classes have corresponding <B>Reader</B> and <B>Writer</B> classes to
provide native Unicode manipulation. However, there are some places where the
<B>byte</B>-oriented <B>InputStream</B>s and <B>OutputStream</B>s are the
correct solution;<B> </B>in particular, the <B>java.util.zip</B> libraries are
<B>byte</B>-oriented rather than <B>char</B>-oriented. So the most sensible
approach to take is to <I>try</I> to use the <B>Reader</B> and <B>Writer</B>
classes whenever you can, and you’ll discover the situations when you have
to use the <B>byte</B>-oriented libraries because your code won’t compile.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I41'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I42>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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 two hierarchies.
<A NAME="Index1232"></A><A NAME="Index1233"></A><A NAME="Index1234"></A><A NAME="Index1235"></A><A NAME="Index1236"></A><A NAME="Index1237"></A><A NAME="Index1238"></A><A NAME="Index1239"></A><A NAME="Index1240"></A><A NAME="Index1241"></A><A NAME="Index1242"></A><A NAME="Index1243"></A><A NAME="Index1244"></A><A NAME="Index1245"></A><A NAME="Index1246"></A><A NAME="Index1247"></A><A NAME="Index1248"></A><A NAME="Index1249"></A><A NAME="Index1250"></A><A NAME="Index1251"></A><A NAME="Index1252"></A><A NAME="Index1253"></A><A NAME="Index1254"></A><A NAME="Index1255"></A></FONT><BR></P></DIV>
<DIV ALIGN="CENTER"><TABLE BORDER>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Sources &
Sinks:</B></FONT><BR><FONT FACE="Georgia"><B>Java 1.0 class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Corresponding Java 1.1
class</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>InputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Reader
</B></FONT><BR><FONT FACE="Georgia">converter:<B>
InputStreamReader</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>OutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Writer
</B></FONT><BR><FONT FACE="Georgia">converter:<B>
OutputStreamWriter</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>FileInputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>FileReader</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>FileOutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>FileWriter</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>StringBufferInputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>StringReader</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">(no corresponding class)</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>StringWriter</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>ByteArrayInputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>CharArrayReader</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>ByteArrayOutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>CharArrayWriter</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>PipedInputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>PipedReader</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>PipedOutputStream</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>PipedWriter</B></FONT><BR></P></DIV>
</TD>
</TR>
</TABLE></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In general, you’ll find that the
interfaces for the two different hierarchies are similar if not
identical.</FONT><A NAME="_Toc481064746"></A><BR></P></DIV>
<A NAME="Heading366"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Modifying stream behavior</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For <B>InputStream</B>s and
<B>OutputStream</B>s, streams were adapted for particular needs using
“decorator” subclasses of <B>FilterInputStream</B> and
<B>FilterOutputStream.</B> The <B>Reader</B> and <B>Writer</B> class hierarchies
continue the use of this idea—but not exactly.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER11_I42'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER11_I43>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In the following table, the
correspondence is a rougher approximation than in the previous table. The
difference is because of the class organization: while
<B>BufferedOutputStream</B> is a subclass of <B>FilterOutputStream</B>,
<B>BufferedWriter</B> is <I>not</I> a subclass of <B>FilterWriter</B> (which,
even though it is <B>abstract</B>, 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.
<A NAME="Index1256"></A><A NAME="Index1257"></A><A NAME="In
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -