📄 javaio.doc.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.io </title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javautil.doc12.html">Prev</a> | <a href="javaio.doc1.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="46185"></a>
<p><strong>
CHAPTER 22 </strong></p>
<a name="46187"></a>
<h1>The Package java.io </h1>
<hr><p>
<a name="27524"></a>
Input and output in Java is organized around the concept of streams. A stream
is a sequence of items, usually 8-bit bytes, read or written over the course of time.
<p><a name="27530"></a>
In the <code>java.io</code> package, all input is done through subclasses of the abstract class <code>InputStream</code>, and all output is done through subclasses of the abstract class <code>OutputStream</code>. The one exception to this rule is the class <code>RandomAccessFile</code>, which handles files that allow random access and perhaps intermixed reading and writing of the file.<p>
<a name="27534"></a>
For an input stream, the source of data might be a file, a <code>String</code>, an array of bytes, or bytes written to an output stream (typically by another thread). There are also "filter input streams" that take data from another input stream and transform or augment the data before delivering it as input. For example, a <code>LineNumberInputStream</code>  passes bytes through verbatim but counts line terminators as they are read.<p>
<a name="27537"></a>
For an output stream, the sink of data might be a file, an array of bytes, or a buffer to be read as an input stream (typically by another thread). There are also "filter output streams" that transform or augment data before writing it to some other output stream.<p>
<a name="27564"></a>
An instance of class <code>File</code> represents a path name (a string) that might identify a particular file within a file system. Certain operations on the file system, such as renaming and deleting files, are done by this class rather than through streams.<p>
<a name="27687"></a>
An instance of class <code>FileDescriptor</code> represents an abstract indication of a particular file within a file system; such file descriptors are created internally by the Java I/O system.<p>
<a name="27586"></a>
There are two interfaces, <code>DataInput</code> and <code>DataOutput</code>, that support the transfer of data other than bytes or characters, such as long integers, floating-point numbers and strings. The class <code>DataInputStream</code> implements <code>DataInput</code>; the class <code>DataOutputStream</code> implements <code>DataOutput</code>; and <code>RandomAccessFile</code> implements both <code>DataInput</code> and <code>DataOutput</code>.<p>
<a name="27588"></a>
The class <code>StreamTokenizer</code> provides some simple support for parsing bytes or characters from an input stream into tokens such as identifiers, numbers, and strings, optionally ignoring comments and optionally recognizing or ignoring line terminators.<p>
<a name="27733"></a>
The hierarchy of classes defined in package <code>java.io</code> is as follows. (Classes whose names are shown here in <code><b>boldface</b></code> are in package <code>java.io</code>; the others are in package <code>java.lang</code> and are shown here to clarify subclass relationships.)<p>
<pre><a name="27572"></a>Object <code><a href="javalang.doc1.html#46442">§20.1</a></code>
<a name="32844"></a> interface<code><b> DataInput <a href="javaio.doc.html#28762">§22.1</a>
</b></code><a name="32849"></a> interface<code><b> DataOutput <a href="javaio.doc.html#42209">§22.2</a>
</b></code><a name="32854"></a> <code><b>InputStream <a href="javaio.doc1.html#28126">§22.3</a>
</b></code><a name="27604"></a> <code><b>FileInputStream <a href="javaio.doc2.html#28230">§22.4</a>
</b></code><a name="27615"></a> <code><b>PipedInputStream <a href="javaio.doc3.html#28307">§22.5</a>
</b></code><a name="27577"></a> <code><b>ByteArrayInputStream <a href="javaio.doc4.html#28355">§22.6</a>
</b></code><a name="27578"></a> <code><b>StringBufferInputStream <a href="javaio.doc5.html#28427">§22.7</a>
</b></code><a name="27580"></a> <code><b>SequenceInputStream <a href="javaio.doc6.html#28491">§22.8</a>
</b></code><a name="27582"></a> <code><b>FilterInputStream <a href="javaio.doc7.html#28534">§22.9</a>
</b></code><a name="27630"></a> <code><b>BufferedInputStream <a href="javaio.doc8.html#28120">§22.10</a>
</b></code><a name="27599"></a> <code><b> DataInputStream <a href="javaio.doc9.html#28623">§22.11</a>
</b></code><a name="27535"></a> <code><b> LineNumberInputStream <a href="javaio.doc10.html#32925">§22.12</a>
</b></code><a name="27681"></a> <code><b>PushBackInputStream <a href="javaio.doc21.html#27769">§22.13</a>
</b></code><a name="30359"></a> <code><b>StreamTokenizer <a href="javaio.doc12.html#29287">§22.14</a>
</b></code><a name="30360"></a> <code><b>OutputStream <a href="javaio.doc13.html#29399">§22.15</a>
</b></code><a name="30361"></a> <code><b>FileOutputStream <a href="javaio.doc14.html#29496">§22.16</a>
</b></code><a name="27663"></a> <code><b>PipedOutputStream <a href="javaio.doc15.html#29449">§22.17</a>
</b></code><a name="27664"></a> <code><b>ByteArrayOutputStream <a href="javaio.doc16.html#29558">§22.18</a>
</b></code><a name="27667"></a> <code><b>FilterOutputStream <a href="javaio.doc21.html#27982">§22.19</a>
</b></code><a name="27668"></a> <code><b>BufferedOutputStream <a href="javaio.doc18.html#9059">§22.20</a>
</b></code><a name="27669"></a> <code><b>DataOutputStream <a href="javaio.doc19.html#9231">§22.21</a>
</b></code><a name="27670"></a> <code><b>PrintStream <a href="javaio.doc20.html#29906">§22.22</a>
</b></code><a name="27646"></a> <code><b>RandomAccessFile <a href="javaio.doc21.html#27738">§22.23</a>
</b></code><a name="27652"></a><code><b> File <a href="javaio.doc22.html#29705">§22.24</a>
</b></code><a name="32863"></a> interface<code><b> FileNameFilter <a href="javaio.doc22.html#30147">§22.25</a>
</b></code><a name="32868"></a> <code><b>FileDescriptor <a href="javaio.doc23.html#29890">§22.26</a>
</b></code><a name="30355"></a> Throwable <a href="javalang.doc20.html#46198">§20.22</a>
<a name="30356"></a> Exception <a href="javalang.doc20.html#46198">§20.22</a>
<a name="27598"></a> <code><b>IOException <a href="javaio.doc24.html#30171">§22.27</a>
</b></code><a name="30143"></a> <code><b>EOFException <a href="javaio.doc25.html#32995">§22.28</a>
</b></code><a name="27658"></a> <code><b>FileNotFoundException <a href="javaio.doc26.html#30115">§22.29</a>
</b></code><a name="27656"></a> <code><b>InterruptedIOException <a href="javaio.doc27.html#30205">§22.30</a>
</b></code><a name="28119"></a> <code><b>UTFDataFormatException <a href="javaio.doc28.html#10137">§22.31</a>
</b></code></pre><a name="28762"></a>
<h1>22.1 The Interface <code>java.io.DataInput</code></h1>
<a name="28763"></a>
The <code>DataInput</code> interface provides for reading bytes from a binary stream and
reconstructing from them data in any of the Java primitive types. There is also a
facility for reconstructing a <code>String</code> from data in Java modified UTF-8 format.
<p><a name="28767"></a>
The <code>DataOutput</code> interface <a href="javaio.doc.html#42209">(§22.2)</a> supports the creation of binary output data suitable for reading back in through the <code>DataInput</code> interface.<p>
<a name="28768"></a>
The <code>DataInput</code> interface is implemented by classes <code>DataInputStream</code> <a href="javaio.doc9.html#28623">(§22.11)</a> and <code>RandomAccessFile</code> <a href="javaio.doc21.html#27738">(§22.23)</a>.<p>
<pre><a name="28775"></a>public interface <code><b>DataInput</b></code> {
<a name="28776"></a> public void <code><b>readFully</b></code>(byte[] b)
<a name="32257"></a> throws IOException, NullPointerException;
<a name="28777"></a> public void <code><b>readFully</b></code>(byte[] b, int off, int len)
<a name="28778"></a> throws IOException, NullPointerException,
<a name="32260"></a> IndexOutOfBoundsException;
<a name="28779"></a> public int <code><b>skipBytes</b></code>(int n) throws IOException;
<a name="28780"></a> public boolean <code><b>readBoolean</b></code>() throws IOException;
<a name="28781"></a> public byte <code><b>readByte</b></code>() throws IOException;
<a name="28782"></a> public int <code><b>readUnsignedByte</b></code>() throws IOException;
<a name="28783"></a> public short <code><b>readShort</b></code>() throws IOException;
<a name="28784"></a> public int <code><b>readUnsignedShort</b></code>() throws IOException;
<a name="28785"></a> public char <code><b>readChar</b></code>() throws IOException;
<a name="28786"></a> public int <code><b>readInt</b></code>() throws IOException;
<a name="28787"></a> public long <code><b>readLong</b></code>() throws IOException;
<a name="28788"></a> public float <code><b>readFloat</b></code>() throws IOException;
<a name="28789"></a> public double <code><b>readDouble</b></code>() throws IOException;
<a name="28790"></a> public String <code><b>readLine</b></code>() throws IOException;
<a name="28791"></a> public String <code><b>readUTF</b></code>() throws IOException;
<a name="28794"></a>}
</pre><a name="30226"></a>
It is generally true of all the reading routines in this interface that if end of file is reached before the desired number of bytes has been read, an <code>EOFException</code> (which is a kind of <code>IOException</code>) is thrown. If any byte cannot be read for any reason other than end of file, an <code>IOException</code> other than <code>EOFException</code> is thrown. In particular, an <code>IOException</code> may be thrown if the input stream has been closed <a href="javaio.doc1.html#28187">(§22.3.6)</a>.<p>
<a name="28801"></a>
<p><font size=+1><strong>22.1.1 </strong> <code>public void <code><b>readFully</b></code>(byte[] b)<br>throws IOException, <code>NullPointerException</code>;</code></font>
<p>
<a name="28802"></a>
The general contract of <code>readFully(b)</code> is that it reads some bytes from an input
stream and stores them into the buffer array <code>b</code>. The number of bytes read is equal
to the length of <code>b</code>.
<p><a name="31275"></a>
This method blocks until one of the following conditions occurs:<p>
<ul><a name="31276"></a>
<li><code>b.length</code> bytes of input data are available, in which case a normal return is made.
<a name="31277"></a>
<li>End of file is detected, in which case an <code>EOFException</code> is thrown.
<a name="31278"></a>
<li>An I/O error occurs, in which case an <code>IOException</code> other than <code>EOFException</code> is thrown.
</ul><a name="28804"></a>
If <code>b</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.<p>
<a name="28805"></a>
If <code>b.length</code> is zero, then no bytes are read. Otherwise, the first byte read is stored into element <code>b[0]</code>, the next one into <code>b[1]</code>, and so on.<p>
<a name="31283"></a>
If an exception is thrown from this method, then it may be that some but not all bytes of <code>b</code> have been updated with data from the input stream.<p>
<a name="28807"></a>
<p><font size=+1><strong>22.1.2 </strong> <code>public void <code><b>readFully</b></code>(byte[] b, int off, int len)<br>throws IOException, <code>NullPointerException</code>,<br>      IndexOutOfBoundsException</code></font>
<p>
<a name="28808"></a>
The general contract of <code>readFully(b, off, len)</code> is that it reads <code>len</code> bytes from
an input stream.
<p><a name="28809"></a>
This method blocks until one of the following conditions occurs:<p>
<ul><a name="31270"></a>
<li><code>len</code> bytes of input data are available, in which case a normal return is made.
<a name="31271"></a>
<li>End of file is detected, in which case an <code>EOFException</code> is thrown.
<a name="31273"></a>
<li>An I/O error occurs, in which case an <code>IOException</code> other than <code>EOFException</code> is thrown.
</ul><a name="28810"></a>
If <code>b</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.<p>
<a name="28811"></a>
If <code>off</code> is negative, or <code>len</code> is negative, or <code>off+len</code> is greater than the length of the array <code>b</code>, then an <code>IndexOutOfBoundsException</code> is thrown.<p>
<a name="28812"></a>
If <code>len</code> is zero, then no bytes are read. Otherwise, the first byte read is stored into element <code>b[off]</code>, the next one into <code>b[off+1]</code>, and so on. The number of bytes read is, at most, equal to <code>len</code>.<p>
<a name="31291"></a>
If an exception is thrown from this method, then it may be that some but not all bytes of <code>b</code> in positions <code>off</code> through <code>off+len-1</code> have been updated with data from the input stream.<p>
<a name="28814"></a>
<p><font size=+1><strong>22.1.3 </strong> <code>public int <code><b>skipBytes</b></code>(int n) throws IOException</code></font>
<p>
<a name="28815"></a>
The general contract of <code>skipBytes</code> is that it makes an attempt to skip over <code>n</code> bytes
of data from the input stream, discarding the skipped bytes. However, it may skip
over some smaller number of bytes, possibly zero. This may result from any of a
number of conditions; reaching end of file before <code>n</code> bytes have been skipped is
only one possibility. This method never throws an <code>EOFException</code>. The actual
number of bytes skipped is returned.
<p><a name="28817"></a>
<p><font size=+1><strong>22.1.4 </strong> <code>public boolean <code><b>readBoolean</b></code>() throws IOException;</code></font>
<p>
<a name="28818"></a>
The general contract of <code>readBoolean</code> is that it reads one input byte and returns
<code>true</code> if that byte is nonzero, <code>false</code> if that byte is zero.
<p><a name="28819"></a>
This method is suitable for reading the byte written by the <code>writeBoolean</code> method of interface <code>DataOutput</code> <a href="javaio.doc.html#28974">(§22.2.4)</a>.<p>
<a name="28824"></a>
<p><font size=+1><strong>22.1.5 </strong> <code>public byte <code><b>readByte</b></code>() throws IOException</code></font>
<p>
<a name="28825"></a>
The general contract of <code>readByte</code> is that it reads and returns one input byte. The
byte is treated as a signed value in the range <code>-128</code> through <code>127</code>, inclusive.
<p><a name="28826"></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>.<p>
<a name="28831"></a>
<p><font size=+1><strong>22.1.6 </strong> <code>public int <code><b>readUnsignedByte</b></code>() throws IOException</code></font>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -