GeezerVison is showing docs.oracle.com — RandomAccessFile (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.io
Class RandomAccessFile
-
-
Constructor Summary
Constructors
| Constructor |
Description |
RandomAccessFile(File file,
String mode) |
Creates a random access file stream to read from, and optionally to
write to, the file specified by the File argument.
|
RandomAccessFile(String name,
String mode) |
Creates a random access file stream to read from, and optionally
to write to, a file with the specified name.
|
-
Method Summary
All Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
void |
close() |
Closes this random access file stream and releases any system
resources associated with the stream.
|
FileChannel |
getChannel() |
Returns the unique FileChannel
object associated with this file.
|
FileDescriptor |
getFD() |
Returns the opaque file descriptor object associated with this
stream.
|
long |
getFilePointer() |
Returns the current offset in this file.
|
long |
length() |
Returns the length of this file.
|
int |
read() |
Reads a byte of data from this file.
|
int |
read(byte[] b) |
Reads up to b.length bytes of data from this file
into an array of bytes.
|
int |
read(byte[] b,
int off,
int len) |
Reads up to len bytes of data from this file into an
array of bytes.
|
boolean |
readBoolean() |
Reads a boolean from this file.
|
byte |
readByte() |
Reads a signed eight-bit value from this file.
|
char |
readChar() |
Reads a character from this file.
|
double |
readDouble() |
Reads a double from this file.
|
float |
readFloat() |
Reads a float from this file.
|
void |
readFully(byte[] b) |
Reads b.length bytes from this file into the byte
array, starting at the current file pointer.
|
void |
readFully(byte[] b,
int off,
int len) |
Reads exactly len bytes from this file into the byte
array, starting at the current file pointer.
|
int |
readInt() |
Reads a signed 32-bit integer from this file.
|
String |
readLine() |
Reads the next line of text from this file.
|
long |
readLong() |
Reads a signed 64-bit integer from this file.
|
short |
readShort() |
Reads a signed 16-bit number from this file.
|
int |
readUnsignedByte() |
Reads an unsigned eight-bit number from this file.
|
int |
readUnsignedShort() |
Reads an unsigned 16-bit number from this file.
|
String |
readUTF() |
Reads in a string from this file.
|
void |
seek(long pos) |
Sets the file-pointer offset, measured from the beginning of this
file, at which the next read or write occurs.
|
void |
setLength(long newLength) |
Sets the length of this file.
|
int |
skipBytes(int n) |
Attempts to skip over n bytes of input discarding the
skipped bytes.
|
void |
write(byte[] b) |
Writes b.length bytes from the specified byte array
to this file, starting at the current file pointer.
|
void |
write(byte[] b,
int off,
int len) |
Writes len bytes from the specified byte array
starting at offset off to this file.
|
void |
write(int b) |
Writes the specified byte to this file.
|
void |
writeBoolean(boolean v) |
Writes a boolean to the file as a one-byte value.
|
void |
writeByte(int v) |
Writes a byte to the file as a one-byte value.
|
void |
writeBytes(String s) |
Writes the string to the file as a sequence of bytes.
|
void |
writeChar(int v) |
Writes a char to the file as a two-byte value, high
byte first.
|
void |
writeChars(String s) |
Writes a string to the file as a sequence of characters.
|
void |
writeDouble(double v) |
Converts the double argument to a long using the
doubleToLongBits method in class Double,
and then writes that long value to the file as an
eight-byte quantity, high byte first.
|
void |
writeFloat(float v) |
Converts the float argument to an int using the
floatToIntBits method in class Float,
and then writes that int value to the file as a
four-byte quantity, high byte first.
|
void |
writeInt(int v) |
Writes an int to the file as four bytes, high byte first.
|
void |
writeLong(long v) |
Writes a long to the file as eight bytes, high byte first.
|
void |
writeShort(int v) |
Writes a short to the file as two bytes, high byte first.
|
void |
writeUTF(String str) |
Writes a string to the file using
modified UTF-8
encoding in a machine-independent manner.
|
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
readFully
public final void readFully(byte[] b)
throws IOException
Reads b.length bytes from this file into the byte
array, starting at the current file pointer. This method reads
repeatedly from the file until the requested number of bytes are
read. This method blocks until the requested number of bytes are
read, the end of the stream is detected, or an exception is thrown.
- Specified by:
readFully in interface DataInput
- Parameters:
b - the buffer into which the data is read.
- Throws:
EOFException - if this file reaches the end before reading
all the bytes.
IOException - if an I/O error occurs.
-
readFully
public final void readFully(byte[] b,
int off,
int len)
throws IOException
Reads exactly len bytes from this file into the byte
array, starting at the current file pointer. This method reads
repeatedly from the file until the requested number of bytes are
read. This method blocks until the requested number of bytes are
read, the end of the stream is detected, or an exception is thrown.
- Specified by:
readFully in interface DataInput
- Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the number of bytes to read.
- Throws:
EOFException - if this file reaches the end before reading
all the bytes.
IOException - if an I/O error occurs.
-
write
public void write(byte[] b)
throws IOException
Writes b.length bytes from the specified byte array
to this file, starting at the current file pointer.
- Specified by:
write in interface DataOutput
- Parameters:
b - the data.
- Throws:
IOException - if an I/O error occurs.
-
write
public void write(byte[] b,
int off,
int len)
throws IOException
Writes len bytes from the specified byte array
starting at offset off to this file.
- Specified by:
write in interface DataOutput
- Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
- Throws:
IOException - if an I/O error occurs.
-
seek
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.
- Parameters:
pos - the offset position, measured in bytes from the
beginning of the file, at which to set the file
pointer.
- Throws:
IOException - if pos is less than
0 or if an I/O error occurs.
-
readBoolean
public final boolean readBoolean()
throws IOException
Reads a boolean from this file. This method reads a
single byte from the file, starting at the current file pointer.
A value of 0 represents
false. Any other value represents true.
This method blocks until the byte is read, the end of the stream
is detected, or an exception is thrown.
- Specified by:
readBoolean in interface DataInput
- Returns:
- the
boolean value read.
- Throws:
EOFException - if this file has reached the end.
IOException - if an I/O error occurs.
-
writeBoolean
public final void writeBoolean(boolean v)
throws IOException
Writes a boolean to the file as a one-byte value. The
value true is written out as the value
(byte)1; the value false is written out
as the value (byte)0. The write starts at
the current position of the file pointer.
- Specified by:
writeBoolean in interface DataOutput
- Parameters:
v - a boolean value to be written.
- Throws:
IOException - if an I/O error occurs.
-
writeShort
public final void writeShort(int v)
throws IOException
Writes a short to the file as two bytes, high byte first.
The write starts at the current position of the file pointer.
- Specified by:
writeShort in interface DataOutput
- Parameters:
v - a short to be written.
- Throws:
IOException - if an I/O error occurs.
-
writeChar
public final void writeChar(int v)
throws IOException
Writes a char to the file as a two-byte value, high
byte first. The write starts at the current position of the
file pointer.
- Specified by:
writeChar in interface DataOutput
- Parameters:
v - a char value to be written.
- Throws:
IOException - if an I/O error occurs.
-
writeInt
public final void writeInt(int v)
throws IOException
Writes an int to the file as four bytes, high byte first.
The write starts at the current position of the file pointer.
- Specified by:
writeInt in interface DataOutput
- Parameters:
v - an int to be written.
- Throws:
IOException - if an I/O error occurs.
-
writeLong
public final void writeLong(long v)
throws IOException
Writes a long to the file as eight bytes, high byte first.
The write starts at the current position of the file pointer.
- Specified by:
writeLong in interface DataOutput
- Parameters:
v - a long to be written.
- Throws:
IOException - if an I/O error occurs.
-
writeFloat
public final void writeFloat(float v)
throws IOException
Converts the float argument to an int using the
floatToIntBits method in class Float,
and then writes that int value to the file as a
four-byte quantity, high byte first. The write starts at the
current position of the file pointer.
- Specified by:
writeFloat in interface DataOutput
- Parameters:
v - a float value to be written.
- Throws:
IOException - if an I/O error occurs.
- See Also:
Float.floatToIntBits(float)
-
writeDouble
public final void writeDouble(double v)
throws IOException
Converts the double argument to a long using the
doubleToLongBits method in class Double,
and then writes that long value to the file as an
eight-byte quantity, high byte first. The write starts at the current
position of the file pointer.
- Specified by:
writeDouble in interface DataOutput
- Parameters:
v - a double value to be written.
- Throws:
IOException - if an I/O error occurs.
- See Also:
Double.doubleToLongBits(double)
-
writeBytes
public final void writeBytes(String s)
throws IOException
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.
- Specified by:
writeBytes in interface DataOutput
- Parameters:
s - a string of bytes to be written.
- Throws:
IOException - if an I/O error occurs.
-
writeChars
public final void writeChars(String s)
throws IOException
Writes a string to the file as a sequence of characters. Each
character is written to the data output stream as if by the
writeChar method. The write starts at the current
position of the file pointer.
- Specified by:
writeChars in interface DataOutput
- Parameters:
s - a String value to be written.
- Throws:
IOException - if an I/O error occurs.
- See Also:
writeChar(int)
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|