📄 tij314.htm
字号:
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Buffered-InputStream</b><br></p>
</td>
<td width="178.666622" colspan="1" rowspan="2" valign="top">
<p class="Table">Use this to prevent a physical read every time you want more data. You’re saying “Use a buffer.”<br></p>
</td>
<td width="174.666623" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>InputStream</b>, with optional buffer size.<br></p>
</td>
</tr>
<tr valign="top">
<td width="174.666623" colspan="1" rowspan="1" valign="top">
<p class="Table">This doesn’t provide an interface <i>per se</i>, just a requirement that a buffer be used. Attach an interface object.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>LineNumber-InputStream</b><br></p>
</td>
<td width="178.666622" colspan="1" rowspan="2" valign="top">
<p class="Table">Keeps track of line numbers in the input stream; you can call <b>getLineNumber( )</b> and <b>setLineNumber(</b><br><b>int)</b>.<br></p>
</td>
<td width="174.666623" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>InputStream</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="174.666623" colspan="1" rowspan="1" valign="top">
<p class="Table">This just adds line numbering, so you’ll probably attach an interface object.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Pushback-InputStream</b><br></p>
</td>
<td width="178.666622" colspan="1" rowspan="2" valign="top">
<p class="Table">Has a one byte push-back buffer so that you can push back the last character read.<br></p>
</td>
<td width="174.666623" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>InputStream</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="174.666623" colspan="1" rowspan="1" valign="top">
<p class="Table">Generally used in the scanner for a compiler and probably included because the Java compiler needed it. You probably won’t use this.<br></p>
</td>
</tr>
</table></div>
<h3>
<a name="_Toc375545388"></a><a name="_Toc24775792"></a><a name="Heading14188"></a>Writing
to an <b>OutputStream<i> </i></b><br>with<b> FilterOutputStream</b></h3>
<p>The complement to <b>DataInputStream</b> is <b>DataOutputStream</b>, which formats each of the primitive types and <b>String</b> objects onto a stream in such a way that any <b>DataInputStream</b>, on any machine, can read them. All the methods start with “write,” such as <b>writeByte( )</b>, <b>writeFloat( )</b>, etc. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1652" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The original intent of <b>PrintStream</b> was to print all of the primitive data types and <b>String</b> objects in a viewable format. This is different from <b>DataOutputStream</b>, whose goal is to put data elements on a stream in a way that <b>DataInputStream</b> can portably reconstruct them. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1653" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The two important methods in <b>PrintStream</b> are <b>print( )</b> and <b>println( )</b>, which are overloaded to print all the various types. The difference between <b>print( )</b> and <b>println( )</b> is that the latter adds a newline when it’s done. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1654" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><b>PrintStream</b> can be problematic because it traps all <b>IOException</b>s (You must explicitly test the error status with <b>checkError( )</b>, which returns <b>true</b> if an error has occurred). Also, <b>PrintStream</b> doesn’t internationalize properly and doesn’t handle line breaks in a platform-independent way (these problems are solved with <b>PrintWriter</b>, described later). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1655" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><b>BufferedOutputStream</b> is a modifier and tells the stream to use buffering so you don’t get a physical write every time you write to the stream. You’ll probably always want to use this when doing output. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1656" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><b>Table 12-4. Types of FilterOutputStream</b><br></p>
<div align="center" style="position:relative; left: 0"><table border="1">
<tr valign="top">
<th width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><a name="Index1242"></a><a name="Index1243"></a><a name="Index1244"></a><a name="Index1245"></a><a name="Index1246"></a><a name="Index1247"></a><b>Class</b><br></p>
</th>
<th width="173.333290" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Function</b><br></p>
</th>
<th width="161.333293" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Constructor Arguments </b><br></p>
</th>
</tr>
<tr valign="top">
<th width="161.333293" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>How to use it</b><br></p>
</th>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Data-OutputStream</b><br></p>
</td>
<td width="173.333290" colspan="1" rowspan="2" valign="top">
<p class="Table">Used in concert with <b>DataInputStream</b> so you can write primitives (int, char, long, etc.) to a stream in a portable fashion.<br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>OutputStream</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p class="Table">Contains full interface to allow you to write primitive types.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>PrintStream</b><br></p>
</td>
<td width="173.333290" colspan="1" rowspan="2" valign="top">
<p class="Table">For producing formatted output. While <b>DataOutputStream</b> handles the <i>storage</i> of data, <b>PrintStream</b> handles <i>display</i>.<br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>OutputStream</b>, with optional <b>boolean</b> indicating that the buffer is flushed with every newline.<br></p>
</td>
</tr>
<tr valign="top">
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p class="Table">Should be the “final” wrapping for your <b>OutputStream</b> object. You’ll probably use this a lot.<br></p>
</td>
</tr>
<tr valign="top">
<td width="131.999967" colspan="1" rowspan="2" valign="top">
<p class="Table"><b>Buffered-OutputStream</b><br></p>
</td>
<td width="173.333290" colspan="1" rowspan="2" valign="top">
<p class="Table">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.<br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>OutputStream</b>, with optional buffer size.<br></p>
</td>
</tr>
<tr valign="top">
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p class="Table">This doesn’t provide an interface <i>per se</i>, just a requirement that a buffer is used. Attach an interface object.<br></p>
</td>
</tr>
</table></div>
<h2>
<a name="_Toc375545389"></a><a name="_Toc24775793"></a><a name="Heading14219"></a><b>Reader</b>s
& <b>Writer</b>s<br></h2>
<p><a name="Index1248"></a>Java 1.1 made some significant modifications to the fundamental I/O stream library. When you see the <a name="Index1249"></a><a name="Index1250"></a><b>Reader</b> and <a name="Index1251"></a><a name="Index1252"></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, whereas the <b>Reader </b>and <b>Writer </b>classes provide Unicode-compliant, character-based I/O. In addition: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1657" title="Send BackTalk Comment">Feedback</a></font><br></p>
<ol>
<li><a name="Index1253"></a><a name="Index1254"></a><a name="Index1255"></a><a name="Index1256"></a>Java
1.1 added new classes into the <b>InputStream </b>and <b>OutputStream
</b>hierarchy, so it’s obvious those hierarchies weren’t being
replaced. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1658" title="Send BackTalk
Comment">Feedback</a></font></li>
<li>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 “adapter” 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>.
<font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1659"
title="Send BackTalk
Comment">Feedback</a></font></li></ol><p>The most important reason for the <b>Reader</b> and <b>Writer</b> hierarchies is for 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 <a name="Index1257"></a><a name="Index1258"></a><b>char </b>is 16-bit Unicode), the <a name="Index1259"></a><a name="Index1260"></a><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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1660" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>As is the practice in this book, I will attempt to provide an overview of the classes, but assume that you will use the JDK documentation to determine all the details, such as the exhaustive list of methods. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1661" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775794"></a><a name="Heading14225"></a>Sources and sinks of
data</h3>
<p>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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap11_1662" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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. <br></p>
<div align="center" style="position:relative; left: 0"><table border="1">
<tr valign="top">
<td width="215.999946" colspan="1" rowspan="1" valign="top">
<p class="Table"><a name="Index1261"></a><a name="Index1262"></a><a name="Index1263"></a><a name="Index1264"></a><a name="Index1265"></a><a name="Index1266"></a><a name="Index1267"></a><a name="Index1268"></a><a name="Index1269"></a><a name="Index1270"></a><a name="Index1271"></a><a name="Index1272"></a><a name="Index1273"></a><a name="Index1274"></a><a name="Index1275"></a><a name="Index1276"></a><a name="Index1277"></a><a name="Index1278"></a><a name="Index1279"></a><a name="Index1280"></a><a name="Index1281"></a><a name="Index1282"></a><a name="Index1283"></a><a name="Index1284"></a><b>Sources & Sinks:</b><br><b>Java 1.0 class</b><br></p>
</td>
<td width="239.999940" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Corresponding Java 1.1 class</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="215.999946" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>InputStream</b><br></p>
</td>
<td width="239.999940" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Reader </b><br>adapter:<br><b>InputStreamReader</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="215.999946" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>OutputStream</b><br></p>
</td>
<td width="239.999940" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Writer </b><br>adapter:<b> </b><br><b>OutputStreamWriter</b><br></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -