getFilePointer method and set by the seek
method.
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 EOFException (which is a kind of
IOException) is thrown. If any byte cannot be read for
any reason other than end-of-file, an IOException other
than EOFException is thrown. In particular, an
IOException may be thrown if the stream has been closed.
| Modifier and Type | Field and Description |
|---|---|
| private final byte[] | buffer
A local buffer that allows reading and writing of longer primitive parameters (e.g. long) to be performed using bulk operations rather than on a per-byte basis. |
| private volatile FileChannel | |
| private volatile boolean | |
| private final Object | |
| private final FileDescriptor | |
| private static boolean | jfrTracing
Flag set by jdk.internal.event.JFRTracing to indicate if file reads and writes should be traced by JFR. |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private final String | path
The path of the referenced file (null if the stream is created with a file descriptor) |
| private final boolean | |
| private final boolean |
| Access | Constructor and Description |
|---|---|
| public | RandomAccessFile(String
the system-dependent pathname string pathname, String the access mode mode)Creates a random access file stream to read from, and optionally to write to, a file with the specified pathname. |
| public | RandomAccessFile(File
the file object file, String the access mode, as described
above mode)Creates a random access file stream to read from, and optionally
to write to, the file specified by the |
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Implements java. Closes this random access file stream and releases any system resources associated with the stream. |
| public final FileChannel | Returns: the file channel associated with this fileReturns the unique |
| public final FileDescriptor | Returns: the file descriptor object associated with this stream.Returns the opaque file descriptor object associated with this stream. |
| public native long | Returns: the offset from the beginning of the file, in bytes, at which the next read or write occurs.Returns the current offset in this file. |
| private final String | |
| private void | |
| private void | |
| private static native void | |
| public long | |
| private native long | |
| private void | |
| private native void | |
| public int | Returns: the next byte of data, or-1 if the end of the
file has been reached.Reads a byte of data from this file. |
| public int | Returns: the total number of bytes read into the buffer, or-1 if there is no more data because the end of
the file has been reached.the buffer into which the data is read. b, int the start offset in array off, int b
at which the data is written.the maximum number of bytes read. len)Reads up to |
| public int | Returns: the total number of bytes read into the buffer, or-1 if there is no more data because the end of
this file has been reached.the buffer into which the data is read. b)Reads up to |
| private native int | |
| public final boolean | Returns: theboolean value read.Implements java. Reads a |
| public final byte | Returns: the next byte of this file as a signed eight-bitbyte.Implements java. Reads a signed eight-bit value from this file. |
| private int | readBytes(byte[]
the buffer into which the data is read. b, int the start offset of the data. off, int the number of bytes to read. len)Reads a sub array as a sequence of bytes. |
| private native int | |
| public final char | Returns: the next two bytes of this file, interpreted as achar.Implements java. Reads a character from this file. |
| public final double | Returns: the next eight bytes of this file, interpreted as adouble.Implements java. Reads a |
| public final float | Returns: the next four bytes of this file, interpreted as afloat.Implements java. Reads a |
| public final void | readFully(byte[]
the buffer into which the data is read. b)Implements java. Reads |
| public final void | readFully(byte[]
the buffer into which the data is read. b, int the start offset into the data array off, int b.the number of bytes to read. len)Implements java. Reads exactly |
| public final int | Returns: the next four bytes of this file, interpreted as anint.Implements java. Reads a signed 32-bit integer from this file. |
| public final String | Returns: the next line of text from this file, or null if end of file is encountered before even one byte is read.Implements java. Reads the next line of text from this file. |
| public final long | Returns: the next eight bytes of this file, interpreted as along.Implements java. Reads a signed 64-bit integer from this file. |
| public final short | Returns: the next two bytes of this file, interpreted as a signed 16-bit number.Implements java. Reads a signed 16-bit number from this file. |
| public final int | Returns: the next byte of this file, interpreted as an unsigned eight-bit number.Implements java. Reads an unsigned eight-bit number from this file. |
| public final int | Returns: the next two bytes of this file, interpreted as an unsigned 16-bit integer.Implements java. Reads an unsigned 16-bit number from this file. |
| public final String | Returns: a Unicode string.Implements java. Reads in a string from this file. |
| public void | seek(long
the offset position, measured in bytes from the
beginning of the file, at which to set the file
pointer. pos)Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. |
| private native void | |
| public void | |
| private native void | |
| public int | Returns: the actual number of bytes skipped.the number of bytes to be skipped. n)Implements java. Attempts to skip over |
| private void | |
| private void | |
| private int | |
| private int | |
| public void | write(int
the b)byte to be written.Implements java. Writes the specified byte to this file. |
| public void | write(byte[]
the data. b)Implements java. Writes |
| public void | write(byte[]
the data. b, int the start offset in the data. off, int the number of bytes to write. len)Implements java. Writes |
| private native void | |
| public final void | writeBoolean(boolean
a v)boolean value to be written.Implements java. Writes a |
| public final void | writeByte(int
a v)byte value to be written.Implements java. Writes a |
| private void | writeBytes(byte[]
the data to be written b, int the start offset in the data off, int the number of bytes that are written len)Writes a sub array as a sequence of bytes. |
| public final void | writeBytes(String
a string of bytes to be written. s)Implements java. Writes the string to the file as a sequence of bytes. |
| private native void | |
| public final void | writeChar(int
a v)char value to be written.Implements java. Writes a |
| public final void | writeChars(String
a s)String value to be written.Implements java. Writes a string to the file as a sequence of characters. |
| public final void | writeDouble(double
a v)double value to be written.Implements java. Converts the double argument to a |
| public final void | writeFloat(float
a v)float value to be written.Implements java. Converts the float argument to an |
| public final void | writeInt(int
an v)int to be written.Implements java. Writes an |
| public final void | writeLong(long
a v)long to be written.Implements java. Writes a |
| public final void | writeShort(int
a v)short to be written.Implements java. Writes a |
| public final void | writeUTF(String
a string to be written. str)Implements java. Writes a string to the file using modified UTF-8 encoding in a machine-independent manner. |
| buffer | back to summary |
|---|---|
| private final byte[] buffer A local buffer that allows reading and writing of longer primitive parameters (e.g. long) to be performed using bulk operations rather than on a per-byte basis. | |
| channel | back to summary |
|---|---|
| private volatile FileChannel channel | |
| closed | back to summary |
|---|---|
| private volatile boolean closed | |
| closeLock | back to summary |
|---|---|
| private final Object closeLock | |
| fd | back to summary |
|---|---|
| private final FileDescriptor fd | |
| jfrTracing | back to summary |
|---|---|
| private static boolean jfrTracing Flag set by jdk.internal.event.JFRTracing to indicate if file reads and writes should be traced by JFR. | |
| O_DSYNC | back to summary |
|---|---|
| private static final int O_DSYNC | |
| O_RDONLY | back to summary |
|---|---|
| private static final int O_RDONLY | |
| O_RDWR | back to summary |
|---|---|
| private static final int O_RDWR | |
| O_SYNC | back to summary |
|---|---|
| private static final int O_SYNC | |
| O_TEMPORARY | back to summary |
|---|---|
| private static final int O_TEMPORARY | |
| path | back to summary |
|---|---|
| private final String path The path of the referenced file (null if the stream is created with a file descriptor) | |
| rw | back to summary |
|---|---|
| private final boolean rw | |
| sync | back to summary |
|---|---|
| private final boolean sync | |
| RandomAccessFile | back to summary |
|---|---|
| public RandomAccessFile(String pathname, String mode) throws FileNotFoundException Creates a random access file stream to read from, and optionally
to write to, a file with the specified pathname. If the file exists
it is opened; if it does not exist and write mode is specified, a
new file is created.
Symbolic links
are automatically redirected to the target of the link.
A new The
| |
| RandomAccessFile | back to summary | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| public RandomAccessFile(File file, String mode) throws FileNotFoundException Creates a random access file stream to read from, and optionally
to write to, the file specified by the The
"rws" and "rwd" modes work much like the force(boolean) method of
the java. class, passing arguments of
true and false, respectively, except that they always
apply to every I/O operation and are therefore often more efficient. If
the file resides on a local storage device then when an invocation of a
method of this class returns it is guaranteed that all changes made to
the file by that invocation will have been written to that device. This
is useful for ensuring that critical information is not lost in the
event of a system crash. If the file does not reside on a local device
then no such guarantee is made.
The
| |||||||||||
| RandomAccessFile | back to summary |
|---|---|
| private RandomAccessFile(File file, String mode, boolean openAndDelete) throws FileNotFoundException | |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements java. Closes this random access file stream and releases any system resources associated with the stream. A closed random access file cannot perform input or output operations and cannot be reopened. If this file has an associated channel then the channel is closed as well. API Note If this stream has an associated channel then this method will close the channel, which in turn will close this stream. Subclasses that override this method should be prepared to handle possible reentrant invocation.
| |
| getChannel | back to summary |
|---|---|
| public final FileChannel getChannel() Returns the unique The
| |
| getFD | back to summary |
|---|---|
| public final FileDescriptor getFD() throws IOException Returns the opaque file descriptor object associated with this stream.
| |
| getFilePointer | back to summary |
|---|---|
| public native long getFilePointer() throws IOException Returns the current offset in this file.
| |
| implReadLine | back to summary |
|---|---|
| private final String implReadLine() throws IOException | |
| implWrite | back to summary |
|---|---|
| private void implWrite(int b) throws IOException | |
| implWriteBytes | back to summary |
|---|---|
| private void implWriteBytes(byte[] b, int off, int len) throws IOException | |
| initIDs | back to summary |
|---|---|
| private static native void initIDs() | |
| length | back to summary |
|---|---|
| public long length() throws IOException Returns the length of this file.
| |
| length0 | back to summary |
|---|---|
| private native long length0() throws IOException | |
| open | back to summary |
|---|---|
| private void open(String name, int mode) throws FileNotFoundException Opens a file and returns the file descriptor. The file is
opened in read-write mode if the O_RDWR bit in
| |
| open0 | back to summary |
|---|---|
| private native void open0(String name, int mode) throws FileNotFoundException Opens a file and returns the file descriptor. The file is
opened in read-write mode if the O_RDWR bit in
| |
| read | back to summary |
|---|---|
| public int read() throws IOException Reads a byte of data from this file. The byte is returned as an
integer in the range 0 to 255 (
Although
| |
| read | back to summary |
|---|---|
| public int read(byte[] b, int off, int len) throws IOException Reads up to
Although
| |
| read | back to summary |
|---|---|
| public int read(byte[] b) throws IOException Reads up to
Although
| |
| read0 | back to summary |
|---|---|
| private native int read0() throws IOException | |
| readBoolean | back to summary |
|---|---|
| public final boolean readBoolean() throws IOException Implements java. Reads a
| |
| readByte | back to summary |
|---|---|
| public final byte readByte() throws IOException Implements java. Reads a signed eight-bit value from this file. This method reads a
byte from the file, starting from the current file pointer.
If the byte read is (byte)(b) This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.
| |
| readBytes | back to summary |
|---|---|
| private int readBytes(byte[] b, int off, int len) throws IOException Reads a sub array as a sequence of bytes.
| |
| readBytes0 | back to summary |
|---|---|
| private native int readBytes0(byte[] b, int off, int len) throws IOException | |
| readChar | back to summary |
|---|---|
| public final char readChar() throws IOException Implements java. Reads a character from this file. This method reads two
bytes from the file, starting at the current file pointer.
If the bytes read, in order, are
(char)((b1 << 8) | b2) This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.
| |
| readDouble | back to summary |
|---|---|
| public final double readDouble() throws IOException Implements java. Reads a This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.
| |
| readFloat | back to summary |
|---|---|
| public final float readFloat() throws IOException Implements java. Reads a This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.
| |
| readFully | back to summary |
|---|---|
| public final void readFully(byte[] b) throws IOException Implements java. Reads
| |
| readFully | back to summary |
|---|---|
| public final void readFully(byte[] b, int off, int len) throws IOException Implements java. Reads exactly
| |
| readInt | back to summary |
|---|---|
| public final int readInt() throws IOException Implements java. Reads a signed 32-bit integer from this file. This method reads 4
bytes from the file, starting at the current file pointer.
If the bytes read, in order, are (b1 << 24) | (b2 << 16) + (b3 << 8) + b4 This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.
| |
| readLine | back to summary |
|---|---|
| public final String readLine() throws IOException Implements java. Reads the next line of text from this file. This method successively reads bytes from the file, starting at the current file pointer, until it reaches a line terminator or the end of the file. Each byte is converted into a character by taking the byte's value for the lower eight bits of the character and setting the high eight bits of the character to zero. This method does not, therefore, support the full Unicode character set. A line of text is terminated by a carriage-return character
( This method blocks until a newline character is read, a carriage return and the byte following it are read (to see if it is a newline), the end of the file is reached, or an exception is thrown.
| |
| readLong | back to summary |
|---|---|
| public final long readLong() throws IOException Implements java. Reads a signed 64-bit integer from this file. This method reads eight
bytes from the file, starting at the current file pointer.
If the bytes read, in order, are
0 <= b1, b2, b3, b4, b5, b6, b7, b8 <= 255
then the result is equal to: ((long)b1 << 56) + ((long)b2 << 48) + ((long)b3 << 40) + ((long)b4 << 32) + ((long)b5 << 24) + ((long)b6 << 16) + ((long)b7 << 8) + b8 This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.
| |
| readShort | back to summary |
|---|---|
| public final short readShort() throws IOException Implements java. Reads a signed 16-bit number from this file. The method reads two
bytes from this file, starting at the current file pointer.
If the two bytes read, in order, are
(short)((b1 << 8) | b2) This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.
| |
| readUnsignedByte | back to summary |
|---|---|
| public final int readUnsignedByte() throws IOException Implements java. Reads an unsigned eight-bit number from this file. This method reads a byte from this file, starting at the current file pointer, and returns that byte. This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.
| |
| readUnsignedShort | back to summary |
|---|---|
| public final int readUnsignedShort() throws IOException Implements java. Reads an unsigned 16-bit number from this file. This method reads
two bytes from the file, starting at the current file pointer.
If the bytes read, in order, are
(b1 << 8) | b2 This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.
| |
| readUTF | back to summary |
|---|---|
| public final String readUTF() throws IOException Implements java. Reads in a string from this file. The string has been encoded using a modified UTF-8 format.
The first two bytes are read, starting from the current file
pointer, as if by
This method blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown.
| |
| seek | back to summary |
|---|---|
| public void seek(long pos) throws IOException Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.
| |
| seek0 | back to summary |
|---|---|
| private native void seek0(long pos) throws IOException | |
| setLength | back to summary |
|---|---|
| public void setLength(long newLength) throws IOException Sets the length of this file. If the present length of the file as returned by the
length method is greater than the desired length
of the file specified by the If the present length of the file is smaller than the desired length, then the file will be extended. The contents of the extended portion of the file are not defined. If the present length of the file is equal to the desired length, then the file and its length will be unchanged. In all cases, after this method returns, the file offset as returned
by the getFilePointer method will equal the
minimum of the desired length and the file offset before this method was
called, even if the length is unchanged. In other words, this method
constrains the file offset to the closed interval
| |
| setLength0 | back to summary |
|---|---|
| private native void setLength0(long newLength) throws IOException | |
| skipBytes | back to summary |
|---|---|
| public int skipBytes(int n) throws IOException Implements java. Attempts to skip over
This method 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
| |
| traceImplWrite | back to summary |
|---|---|
| private void traceImplWrite(int b) throws IOException | |
| traceImplWriteBytes | back to summary |
|---|---|
| private void traceImplWriteBytes(byte[] b, int off, int len) throws IOException | |
| traceRead0 | back to summary |
|---|---|
| private int traceRead0() throws IOException | |
| traceReadBytes0 | back to summary |
|---|---|
| private int traceReadBytes0(byte[] b, int off, int len) throws IOException | |
| write | back to summary |
|---|---|
| public void write(int b) throws IOException Implements java. Writes the specified byte to this file. The write starts at the current file pointer.
| |
| write | back to summary |
|---|---|
| public void write(byte[] b) throws IOException Implements java. Writes
| |
| write | back to summary |
|---|---|
| public void write(byte[] b, int off, int len) throws IOException Implements java. Writes
| |
| write0 | back to summary |
|---|---|
| private native void write0(int b) throws IOException | |
| writeBoolean | back to summary |
|---|---|
| public final void writeBoolean(boolean v) throws IOException Implements java. Writes a
| |
| writeByte | back to summary |
|---|---|
| public final void writeByte(int v) throws IOException Implements java. Writes a
| |
| writeBytes | back to summary |
|---|---|
| private void writeBytes(byte[] b, int off, int len) throws IOException Writes a sub array as a sequence of bytes.
| |
| writeBytes | back to summary |
|---|---|
| public final void writeBytes(String s) throws IOException Implements java. Writes the string to the file as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits. The write starts at the current position of the file pointer.
| |
| writeBytes0 | back to summary |
|---|---|
| private native void writeBytes0(byte[] b, int off, int len) throws IOException | |
| writeChar | back to summary |
|---|---|
| public final void writeChar(int v) throws IOException Implements java. Writes a
| |
| writeChars | back to summary |
|---|---|
| public final void writeChars(String s) throws IOException Implements java. Writes a string to the file as a sequence of characters. Each
character is written to the data output stream as if by the
| |
| writeDouble | back to summary |
|---|---|
| public final void writeDouble(double v) throws IOException Implements java. Converts the double argument to a
| |
| writeFloat | back to summary |
|---|---|
| public final void writeFloat(float v) throws IOException Implements java. Converts the float argument to an
| |
| writeInt | back to summary |
|---|---|
| public final void writeInt(int v) throws IOException Implements java. Writes an
| |
| writeLong | back to summary |
|---|---|
| public final void writeLong(long v) throws IOException Implements java. Writes a
| |
| writeShort | back to summary |
|---|---|
| public final void writeShort(int v) throws IOException Implements java. Writes a
| |
| writeUTF | back to summary |
|---|---|
| public final void writeUTF(String str) throws IOException Implements java. Writes a string to the file using modified UTF-8 encoding in a machine-independent manner.
First, two bytes are written to the file, starting at the
current file pointer, as if by the
| |