📄 javaio.doc.html
字号:
<p>
<a name="28832"></a>
The general contract of <code>readUnsignedByte</code> is that it reads one input byte, zero-
extends it to type <code>int</code>, and returns the result, which is therefore in the range <code>0</code>
through <code>255</code>.
<p><a name="28833"></a>
This method is suitable for reading the byte written by the <code>writeByte</code> method of interface <code>DataOutput</code> <a href="javaio.doc.html#28981">(§22.2.5)</a> if the argument to <code>writeByte</code> was intended to be a value in the range <code>0</code> through <code>255</code>.<p>
<a name="28838"></a>
<p><font size=+1><strong>22.1.7 </strong> <code>public short <code><b>readShort</b></code>() throws IOException</code></font>
<p>
<a name="28839"></a>
The general contract of <code>readShort</code> is that it reads two input bytes and returns a
<code>short</code> value. Let <code>a</code> be the first byte read and <code>b</code> be the second byte. The value
returned is:
<p><pre><a name="31301"></a><code>(short)((a << 8) | (b & 0xff))
</code></pre><a name="28840"></a>
This method is suitable for reading the bytes written by the <code>writeShort</code> method of interface <code>DataOutput</code> <a href="javaio.doc.html#28988">(§22.2.6)</a>.<p>
<a name="28845"></a>
<p><font size=+1><strong>22.1.8 </strong> <code>public int <code><b>readUnsignedShort</b></code>() throws IOException</code></font>
<p>
<a name="28846"></a>
The general contract of <code>readUnsignedShort</code> is that it reads two input bytes and
returns an <code>int</code> value in the range <code>0</code> through <code>65535</code>. Let <code>a</code> be the first byte read and
<code>b</code> be the second byte. The value returned is:
<p><pre><a name="28847"></a><code>(((a & 0xff) << 8) | (b & 0xff))
</code></pre><a name="28848"></a>
This method is suitable for reading the bytes written by the <code>writeShort</code> method of interface <code>DataOutput</code> <a href="javaio.doc.html#28988">(§22.2.6)</a> if the argument to <code>writeShort</code> was intended to be a value in the range <code>0</code> through <code>65535</code>.<p>
<a name="28853"></a>
<p><font size=+1><strong>22.1.9 </strong> <code>public char <code><b>readChar</b></code>() throws IOException</code></font>
<p>
<a name="28854"></a>
The general contract of <code>readChar</code> is that it reads two input bytes and returns a
<code>char</code> value. Let <code>a</code> be the first byte read and <code>b</code> be the second byte. The value
returned is:
<p><pre><a name="31302"></a><code>(char)((a << 8) | (b & 0xff))
</code></pre><a name="28855"></a>
This method is suitable for reading bytes written by the <code>writeChar</code> method of interface <code>DataOutput</code> <a href="javaio.doc.html#28997">(§22.2.7)</a>.<p>
<a name="28860"></a>
<p><font size=+1><strong>22.1.10 </strong> <code>public int <code><b>readInt</b></code>() throws IOException</code></font>
<p>
<a name="28861"></a>
The general contract of <code>readInt</code> is that it reads four input bytes and returns an
<code>int</code> value. Let <code>a</code> be the first byte read, <code>b</code> be the second byte, <code>c</code> be the third byte,
and <code>d</code> be the fourth byte. The value returned is:
<p><pre><a name="28862"></a>
<code>(((a & 0xff) << 24) | ((b & 0xff) << 16) |
</code><a name="28864"></a><code>  ((c & 0xff) <<    8) | (d & 0xff))
</code></pre><a name="28866"></a>
This method is suitable for reading bytes written by the <code>writeInt</code> method of interface <code>DataOutput</code> <a href="javaio.doc16.html#29567">(§22.2.8)</a>.<p>
<a name="28871"></a>
<p><font size=+1><strong>22.1.11 </strong> <code>public long <code><b>readLong</b></code>() throws IOException</code></font>
<p>
<a name="28872"></a>
The general contract of <code>readLong</code> is that it reads eight input bytes and returns a
<code>long</code> value. Let <code>a</code> be the first byte read, <code>b</code> be the second byte, <code>c</code> be the third byte, <code>d</code>
be the fourth byte, <code>e</code> be the fifth byte, <code>f</code> be the sixth byte, <code>g</code> be the seventh byte,
and <code>h</code> be the eighth byte. The value returned is:
<p><pre><a name="28873"></a>
<code>(((long)(a & 0xff) << 56) |
</code><a name="28874"></a><code>  ((long)(b & 0xff) << 48) |
</code><a name="28875"></a><code>  ((long)(c & 0xff) <<  40) |
</code><a name="28876"></a><code>  ((long)(d & 0xff) << 32) |
</code><a name="28877"></a><code>  ((long)(e & 0xff) <<  24) |
</code><a name="28878"></a><code>  ((long)(f & 0xff) << 16) |
</code><a name="28879"></a><code>  ((long)(g & 0xff) <<    8) |
</code><a name="28880"></a><code>  ((long)(h & 0xff)))
</code></pre><a name="28881"></a>
This method is suitable for reading bytes written by the <code>writeLong</code> method of interface <code>DataOutput</code> <a href="javaio.doc.html#29017">(§22.2.9)</a>.<p>
<a name="28886"></a>
<p><font size=+1><strong>22.1.12 </strong> <code>public float <code><b>readFloat</b></code>() throws IOException</code></font>
<p>
<a name="28887"></a>
The general contract of <code>readFloat</code> is that it reads four input bytes and returns a
<code>float</code> value. It does this by first constructing an <code>int</code> value in exactly the manner
of the <code>readInt</code> method <a href="javaio.doc.html#28860">(§22.1.10)</a>, then converting this <code>int</code> value to a <code>float</code> in
exactly the manner of the method <code>Float.intBitsToFloat</code> <a href="javalang.doc8.html#5683">(§20.9.23)</a>.
<p><a name="28894"></a>
This method is suitable for reading bytes written by the <code>writeFloat</code> method of interface <code>DataOutput</code> <a href="javaio.doc.html#29032">(§22.2.10)</a>.<p>
<a name="28899"></a>
<p><font size=+1><strong>22.1.13 </strong> <code>public double <code><b>readDouble</b></code>() throws IOException</code></font>
<p>
<a name="28900"></a>
The general contract of <code>readDouble</code> is that it reads eight input bytes and returns a
<code>double</code> value. It does this by first constructing a <code>long</code> value in exactly the manner
of the <code>readlong</code> method <a href="javaio.doc.html#28871">(§22.1.11)</a>, then converting this <code>long</code> value to a <code>double</code>
in exactly the manner of the method <code>Double.longBitsToDouble</code> <a href="javalang.doc9.html#13864">(§20.10.22)</a>.
<p><a name="28907"></a>
This method is suitable for reading bytes written by the <code>writeDouble</code> method of interface <code>DataOutput</code> <a href="javaio.doc.html#29045">(§22.2.11)</a>.<p>
<a name="28912"></a>
<p><font size=+1><strong>22.1.14 </strong> <code>public String <code><b>readLine</b></code>() throws IOException</code></font>
<p>
<a name="28913"></a>
The general contract of <code>readLine</code> is that it reads successive bytes, converting
each byte separately into a character, until it encounters a line terminator or end of
file; the characters read are then returned as a <code>String</code>. Note that because this
method processes bytes, it does not support input of the full Unicode character set.
<p><a name="28914"></a>
If end of file is encountered before even one byte can be read, then <code>null</code> is returned. Otherwise, each byte that is read is converted to type <code>char</code> by zero-extension. If the character <code>'\n'</code> is encountered, it is discarded and reading ceases. If the character <code>'\r'</code> is encountered, it is discarded and, if the following byte converts  to the character <code>'\n'</code>, then that is discarded also; reading then ceases. If end of file is encountered before either of the characters <code>'\n'</code> and <code>'\r'</code> is encountered, reading ceases. Once reading has ceased, a <code>String</code> is returned that contains all the characters read and not discarded, taken in order. Note that every character in this string will have a value less than <code>\u0100</code>, that is, <code>(char)256</code>.<p>
<a name="28916"></a>
<p><font size=+1><strong>22.1.15 </strong> <code>public String <code><b>readUTF</b></code>() throws IOException</code></font>
<p>
<a name="28917"></a>
The general contract of <code>readUTF</code> is that it reads a representation of a Unicode
character string encoded in Java modified UTF-8 format; this string of characters
is then returned as a <code>String</code>.
<p><a name="28918"></a>
First, two bytes are read and used to construct an unsigned 16-bit integer in exactly the manner of the <code>readUnsignedShort</code> method <a href="javaio.doc20.html#29998">(§22.1.8)</a>. This integer value is called the <i>UTF length</i> and specifies the number of additional bytes to be read. These bytes are then converted to characters by considering them in groups. The length of each group is computed from the value of the first byte of the group. The byte following a group, if any, is the first byte of the next group.<p>
<a name="28922"></a>
If the first byte of a group matches the bit pattern <code>0xxxxxxx</code> (where <code>x</code> means "may be <code>0</code> or <code>1</code>"), then the group consists of just that byte. The byte is zero-extended to form a character.<p>
<a name="28923"></a>
If the first byte of a group matches the bit pattern <code>110xxxxx</code>, then the group consists of that byte <code>a</code> and a second byte <code>b</code>. If there is no byte <code>b</code> (because byte <code>a</code> was the last of the bytes to be read), or if byte <code>b</code> does not match the bit pattern <code>10xxxxxx</code>, then a <code>UTFDataFormatException</code> is thrown. Otherwise, the group is converted to the character:<p>
<pre><a name="28924"></a><code>(char)(((a & 0x1F) << 6) | (b & 0x3F))
</code></pre><a name="28925"></a>
If the first byte of a group matches the bit pattern <code>1110xxxx</code>, then the group consists of that byte <code>a</code> and two more bytes <code>b</code> and <code>c</code>. If there is no byte <code>c</code> (because byte <code>a</code> was one of the last two of the bytes to be read), or either byte <code>b</code> or byte <code>c</code> does not match the bit pattern <code>10xxxxxx</code>, then a <code>UTFDataFormatException</code> is thrown. Otherwise, the group is converted to the character:<p>
<pre><a name="28926"></a><code>(char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
</code></pre><a name="28927"></a>
If the first byte of a group matches the pattern <code>1111xxxx</code> or the pattern <code>10xxxxxx</code>, then a <code>UTFDataFormatException</code> is thrown.<p>
<a name="31354"></a>
If end of file is encountered at any time during this entire process, then an <code>EOFException</code> is thrown.<p>
<a name="28928"></a>
After every group has been converted to a character by this process, the characters are gathered, in the same order in which their corresponding groups were read from the input stream, to form a <code>String</code>, which is returned.<p>
<a name="42207"></a>
The <code>writeUTF</code> method of interface <code>DataOutput</code> <a href="javaio.doc.html#29074">(§22.2.14)</a> may be used to write data that is suitable for reading by this method.<p>
<a name="42209"></a>
<h1>22.2 The Interface <code>java.io.DataOutput</code></h1>
<a name="42210"></a>
The <code>DataOutput</code> interface provides for converting data from any of the Java primitive
types to a series of bytes and writing these bytes to a binary stream. There is
also a facility for converting a <code>String</code> into Java modified UTF-8 format and writing
the resulting series of bytes.
<p><a name="28936"></a>
The <code>DataInput</code> interface <a href="javaio.doc.html#28762">(§22.1)</a> can be used to read in and reconstruct Java data from the binary output data produced by the <code>DataOutput</code> interface.<p>
<a name="28937"></a>
The <code>DataOutput</code> interface is implemented by classes <code>DataOutputStream</code> <a href="javaio.doc19.html#9231">(§22.21)</a> and <code>RandomAccessFile</code> <a href="javaio.doc21.html#27738">(§22.23)</a>.<p>
<pre><a name="28944"></a>public interface <code><b>DataOutput</b></code> {
<a name="28945"></a> public void <code><b>write</b></code>(int b) throws IOException;
<a name="32269"></a> public void <code><b>write</b></code>(byte[] b)
<a name="32275"></a> throws IOException, NullPointerException;
<a name="28947"></a> public void <code><b>write</b></code>(byte[] b, int off, int len)
<a name="32266"></a> throws IOException, NullPointerException,
<a name="28948"></a> IndexOutOfBoundsException;
<a name="28949"></a> public void <code><b>writeBoolean</b></code>(boolean v) throws IOException;
<a name="28950"></a> public void <code><b>writeByte</b></code>(int v) throws IOException;
<a name="28951"></a> public void <code><b>writeShort</b></code>(int v) throws IOException;
<a name="28952"></a> public void <code><b>writeChar</b></code>(int v) throws IOException;
<a name="28953"></a> public void <code><b>writeInt</b></code>(int v) throws IOException;
<a name="28954"></a> public void <code><b>writeLong</b></code>(long v) throws IOException;
<a name="28955"></a> public void <code><b>writeFloat</b></code>(float v) throws IOException;
<a name="28956"></a> public void <code><b>writeDouble</b></code>(double v) throws IOException;
<a name="32277"></a> public void <code><b>writeBytes</b></code>(String s)
<a name="28957"></a> throws IOException, NullPointerException;
<a name="32280"></a> public void <code><b>writeChars</b></code>(String s)
<a name="28958"></a> throws IOException, NullPointerException;
<a name="32283"></a> public void <code><b>writeUTF</b></code>(String s)
<a name="28959"></a> throws IOException, NullPointerException;
<a name="28960"></a>}
</pre><a name="28961"></a>
For all the methods in this interface that write bytes, it is generally true that if a byte cannot be written for any reason, an <code>IOException</code> is thrown.<p>
<a name="28962"></a>
<p><font size=+1><strong>22.2.1 </strong> <code>public void <code><b>write</b></code>(int b) throws IOException</code></font>
<p>
<a name="28963"></a>
The general contract for <code>write</code> is that one byte is written to the output stream. The
byte to be written is the eight low-order bits of the argument <code>b</code>. The 24 high-order
bits of <code>b</code> are ignored.
<p><a name="28964"></a>
<p><font size=+1><strong>22.2.2 </strong> <code>public void <code><b>write</b></code>(byte[] b)<br>throws IOException, NullPointerException</code></font>
<p>
<a name="28965"></a>
The general contract for <code>write</code> is that all the bytes in array <code>b</code> are written, in order,
to the output stream.
<p><a name="28966"></a>
If <code>b</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -