📄 javaio.doc21.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="javaio.doc20.html">Prev</a> | <a href="javaio.doc22.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="27738"></a>
<center><h1>22.23 The Class <code>java.io.RandomAccessFile</code></h1></center>
<a name="27739"></a>
A random access file behaves like a large array of bytes stored in the file system.
There is a kind of cursor, or index into the implied array, called the <i>file pointer</i>;
input operations read bytes starting at the file pointer and advance the file pointer
past the bytes read. If the random access file is created in read/write mode, then
output operations are also available; output operations write bytes starting at the
file pointer and advance the file pointer past the bytes written. Output operations
that write past the current end of the implied array cause the array to be extended.
The file pointer can be read by the <code>getFilePointer</code> method and set by the <code>seek</code>
method.
<p><pre><a name="27740"></a>public class <code><b>RandomAccessFile</b></code> implements DataOutput, DataInput {
<a name="27741"></a> public <code><b>RandomAccessFile</b></code>(String path, String mode)
<a name="27742"></a> throws SecurityException, IOException,
<a name="30735"></a> IllegalArgumentException;
<a name="27743"></a> public <code><b>RandomAccessFile</b></code>(File file, String mode)
<a name="27744"></a> throws SecurityException, IOException,
<a name="30736"></a> IllegalArgumentException;
<a name="27745"></a> public final FileDescriptor <code><b>getFD</b></code>() throws IOException;
<a name="27746"></a> public native long <code><b>getFilePointer</b></code>() throws IOException;
<a name="27747"></a> public native void <code><b>seek</b></code>(long pos) throws IOException;
<a name="27748"></a> public native long <code><b>length</b></code>() throws IOException;
<a name="27749"></a> public native void <code><b>close</b></code>() throws IOException;
<a name="27750"></a> public native int <code><b>read</b></code>() throws IOException;
<a name="27751"></a> public int <code><b>read</b></code>(byte[] b)
<a name="32511"></a> throws IOException, NullPointerException;
<a name="27752"></a> public int <code><b>read</b></code>(byte[] b, int off, int len)
<a name="27753"></a> throws IOException, NullPointerException,
<a name="32506"></a> IndexOutOfBoundsException;
<a name="27754"></a> // The methods that implement interface DataInput:
<a name="27755"></a> public final void <code><b>readFully</b></code>(byte[] b)
<a name="32519"></a> throws IOException, NullPointerException;
<a name="27756"></a> public final void <code><b>readFully</b></code>(byte[] b, int off, int len)
<a name="27757"></a> throws IOException, NullPointerException,
<a name="32514"></a> IndexOutOfBoundsException;
<a name="27758"></a> public int <code><b>skipBytes</b></code>(int n) throws IOException;
<a name="27759"></a> public final boolean <code><b>readBoolean</b></code>() throws IOException;
<a name="27760"></a> public final byte <code><b>readByte</b></code>() throws IOException;
<a name="27761"></a> public final int <code><b>readUnsignedByte</b></code>() throws IOException;
<a name="27762"></a> public final short <code><b>readShort</b></code>() throws IOException;
<a name="27763"></a> public final int <code><b>readUnsignedShort</b></code>() throws IOException;
<a name="27764"></a> public final char <code><b>readChar</b></code>() throws IOException;
<a name="27765"></a> public final int <code><b>readInt</b></code>() throws IOException;
<a name="27766"></a> public final long <code><b>readLong</b></code>() throws IOException;
<a name="27767"></a> public final float <code><b>readFloat</b></code>() throws IOException;
<a name="27768"></a> public final double <code><b>readDouble</b></code>() throws IOException;
<a name="27769"></a> public final String <code><b>readLine</b></code>() throws IOException;
<a name="27770"></a> public final String <code><b>readUTF</b></code>() throws IOException;
<a name="27771"></a> // The methods that implement interface DataOutput:
<a name="27772"></a> public native void <code><b>write</b></code>(int b) throws IOException;
<a name="27773"></a> public void <code><b>write</b></code>(byte[] b)
<a name="32527"></a> throws IOException, NullPointerException;
<a name="27774"></a> public void <code><b>write</b></code>(byte[] b, int off, int len)
<a name="27775"></a> throws IOException, NullPointerException,
<a name="32522"></a> IndexOutOfBoundsException;
<a name="27776"></a> public final void <code><b>writeBoolean</b></code>(boolean v) throws IOException;
<a name="27777"></a> public final void <code><b>writeByte</b></code>(int v) throws IOException;
<a name="27778"></a> public final void <code><b>writeShort</b></code>(int v) throws IOException;
<a name="27779"></a> public final void <code><b>writeChar</b></code>(int v) throws IOException;
<a name="27780"></a> public final void <code><b>writeInt</b></code>(int v) throws IOException;
<a name="27781"></a> public final void <code><b>writeLong</b></code>(long v) throws IOException;
<a name="27782"></a> public final void <code><b>writeFloat</b></code>(float v) throws IOException;
<a name="27783"></a> public final void <code><b>writeDouble</b></code>(double v) throws IOException;
<a name="27784"></a> public final void <code><b>writeBytes</b></code>(String s) throws IOException;
<a name="27785"></a> public final void <code><b>writeChars</b></code>(String s) throws IOException;
<a name="27786"></a> public final void <code><b>writeUTF</b></code>(String str) throws IOException;
<a name="27787"></a>}
</pre><a name="31735"></a>
It is generally true of all the reading routines in this class 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 stream has been closed <a href="javaio.doc21.html#27822">(§22.23.7)</a>.<p>
<a name="27788"></a>
<p><font size=+1><strong>22.23.1 </strong> <code>public <code><b>RandomAccessFile</b></code>(String path, String mode)<br>throws SecurityException, IOException,      IllegalArgumentException</code></font>
<p>
<a name="27789"></a>
This constructor initializes a newly created <code>RandomAccessFile</code> by opening a
connection to an actual file, the file named by the path name <code>path</code> in the file system.
A new <code>FileDescriptor</code> object is created to represent this file connection.
<p><a name="27793"></a>
First, if there is a security manager, its <code>checkRead</code> method <a href="javalang.doc16.html#14115">(§20.17.19)</a> is called with the <code>path</code> argument as its argument.<p>
<a name="27794"></a>
Next, if <code>mode</code> is <code>"rw"</code> and there is a security manager, its <code>checkWrite</code> method <a href="javalang.doc16.html#14117">(§20.17.21)</a> is called with the <code>path</code> argument as its argument.<p>
<a name="27798"></a>
If <code>mode</code> is <code>"rw"</code>, then the file may be both read and written. If <code>mode</code> is <code>"r"</code>, then the file may be read but may not be written (every write method for this object will simply throw an <code>IOException</code>). If <code>mode</code> is not <code>"r"</code> or <code>"rw"</code>, then this constructor throws an <code>IllegalArgumentException</code>.<p>
<a name="27799"></a>
<p><font size=+1><strong>22.23.2 </strong> <code>public <code><b>RandomAccessFile</b></code>(File file, String mode)<br>throws SecurityException, IOException,      IllegalArgumentException</code></font>
<p>
<a name="27800"></a>
This constructor initializes a newly created <code>RandomAccessFile</code> by opening a
connection to an actual file, the file named by <code>file</code> in the file system. A new
<code>FileDescriptor</code> object is created to represent this file connection.
<p><a name="27804"></a>
First, if there is a security manager, its <code>checkRead</code> method <a href="javalang.doc16.html#14115">(§20.17.19)</a> is called with the path represented by the <code>file</code> argument as its argument.<p>
<a name="27805"></a>
Next, if <code>mode</code> is <code>"rw"</code> and there is a security manager, its <code>checkWrite</code> method <a href="javalang.doc16.html#14117">(§20.17.21)</a> is called with the path represented by the <code>file</code> argument as its argument.<p>
<a name="27809"></a>
If <code>mode</code> is <code>"rw"</code>, then the file may be both read and written. If <code>mode</code> is <code>"r"</code>, then the file may be read but may not be written (every write method for this object will simply throw an <code>IOException</code>). If <code>mode</code> is not <code>"r"</code> or <code>"rw"</code>, then this constructor throws an <code>IllegalArgumentException</code>.<p>
<a name="27810"></a>
<p><font size=+1><strong>22.23.3 </strong> <code>public final FileDescriptor <code><b>getFD</b></code>() throws IOException</code></font>
<p>
<a name="30417"></a>
This method returns the <code>FileDescriptor</code> object <a href="javaio.doc23.html#29890">(§22.26)</a> that represents the connection
to the actual file in the file system being used by this <code>RandomAccessFile</code>.
<p><a name="27815"></a>
<p><font size=+1><strong>22.23.4 </strong> <code>public long <code><b>getFilePointer</b></code>() throws IOException</code></font>
<p>
<a name="27816"></a>
The current file pointer for this random access file is returned. An <code>IOException</code> is
thrown if the file pointer cannot be read for any reason.
<p><a name="27817"></a>
<p><font size=+1><strong>22.23.5 </strong> <code>public void <code><b>seek</b></code>(long pos) throws IOException</code></font>
<p>
<a name="27818"></a>
The file pointer for this random access file is set to <code>pos</code>, which is a position within
the file, measured in bytes. Position <code>0</code> is the start of the file. An <code>IOException</code> is
thrown if <code>pos</code> is less than zero or greater than the length of the file, or if the file
pointer cannot be set for any other reason.
<p><a name="27819"></a>
<p><font size=+1><strong>22.23.6 </strong> <code>public long <code><b>length</b></code>() throws IOException</code></font>
<p>
<a name="27820"></a>
The length of this random access file, measured in bytes, is returned.
<p><a name="31749"></a>
An <code>IOException</code> is thrown if the length cannot be read for any reason.<p>
<a name="27822"></a>
<p><font size=+1><strong>22.23.7 </strong> <code>public void <code><b>close</b></code>() throws IOException</code></font>
<p>
<a name="27823"></a>
This random access file is closed. A closed random access file cannot perform
input or output operations and cannot be reopened.
<p><a name="27824"></a>
<p><font size=+1><strong>22.23.8 </strong> <code>public int <code><b>read</b></code>() throws IOException</code></font>
<p>
<a name="27825"></a>
This method reads one byte from the random access file. The byte is returned as
an integer in the range 0 to 255 (<code>0x00</code>-<code>0xff</code>). If no byte is available because the
file pointer is at end of file, the value <code>-1</code> is returned.
<p><a name="27826"></a>
If the byte cannot be read for any reason other than end of file, an <code>IOException</code> is thrown. In particular, an <code>IOException</code> is thrown if the input stream has been closed <a href="javaio.doc21.html#27822">(§22.23.7)</a>.<p>
<a name="27830"></a>
Although <code>RandomAccessFile</code> is not a subclass of <code>InputStream</code>, this method behaves in exactly the same way as the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28142">(§22.3.1)</a>.<p>
<a name="27834"></a>
<p><font size=+1><strong>22.23.9 </strong> <code>public int <code><b>read</b></code>(byte[] b)<br>throws IOException, NullPointerException</code></font>
<p>
<a name="27835"></a>
Although <code>RandomAccessFile</code> is not a subclass of <code>InputStream</code>, this method
behaves in exactly the same way as the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28150">(§22.3.2)</a>.
<p><a name="27839"></a>
<p><font size=+1><strong>22.23.10 </strong> <code>public int <code><b>read</b></code>(byte[] b, int off, int len)<br>throws IOException, NullPointerException,      IndexOutOfBoundsException</code></font>
<p>
<a name="27840"></a>
Although <code>RandomAccessFile</code> is not a subclass of <code>InputStream</code>, this method
behaves in exactly the same way as the <code>read</code> method of <code>InputStream</code> <a href="javaio.doc1.html#28164">(§22.3.3)</a>.
<p><a name="27844"></a>
<p><font size=+1><strong>22.23.11 </strong> <code>public final void <code><b>readFully</b></code>(byte[] b)<br>throws IOException, NullPointerException</code></font>
<p>
<a name="27848"></a>
See the general contract of the <code>readFully</code> method of <code>DataInput</code> <a href="javaio.doc.html#28801">(§22.1.1)</a>.
<p><a name="27849"></a>
Bytes for this operation are read from the random access file, starting at the current file pointer.<p>
<a name="27850"></a>
<p><font size=+1><strong>22.23.12 </strong> <code>public final void <code><b>readFully</b></code>(byte[] b, int off, int len) throws IOException, NullPointerException,      IndexOutOfBoundsException</code></font>
<p>
<a name="27854"></a>
See the general contract of the <code>readFully</code> method of <code>DataInput</code> <a href="javaio.doc.html#28807">(§22.1.2)</a>.
<p><a name="27855"></a>
Bytes for this operation are read from the random access file, starting at the current file pointer.<p>
<a name="27856"></a>
<p><font size=+1><strong>22.23.13 </strong> <code>public int <code><b>skipBytes</b></code>(int n) throws IOException</code></font>
<p>
<a name="27860"></a>
See the general contract of the <code>skipBytes</code> method of <code>DataInput</code> <a href="javaio.doc.html#28814">(§22.1.3)</a>.
<p><a name="27861"></a>
Bytes for this operation are read from the random access file, starting at the current file pointer.<p>
<a name="27862"></a>
<p><font size=+1><strong>22.23.14 </strong> <code>public final boolean <code><b>readBoolean</b></code>() throws IOException</code></font>
<p>
<a name="27866"></a>
See the general contract of the <code>readBoolean</code> method of <code>DataInput</code> <a href="javaio.doc.html#28817">(§22.1.4)</a>.
<p><a name="27867"></a>
The byte for this operation is read from the random access file, starting at the current file pointer.<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -