java.io.RandomAccessFile
java.lang.Object
None
java.io.DataInput, java.io.DataOutput
JDK 1.0 or later
The RandomAccessFile class reads data from and writes data to a file. The file is specified using a File object or a String that represents a pathname. Both constructors take a mode parameter that specifies whether the file is being opened solely for reading, or for reading and writing. Each of the constructors can throw a SecurityException if the application does not have permission to access the specified file using the given mode.
Unlike FileInputStream and FileOutputStream, RandomAccessFile supports random access to the data in the file; the seek() method allows you to alter the current position of the file pointer to any location in the file. RandomAccessFile implements both the DataInput and DataOutput interfaces, so it supports reading and writing of all the primitive data types.
public class java.io.RandomAccessFile extends java.lang.Object implements java.io.DataInput, java.io.DataOutput { // Constructors public RandomAccessFile(File file, String mode); public RandomAccessFile(String name, String mode); // Instance Methods public native void close(); public final FileDescriptor getFD(); public native long getFilePointer(); public native long length(); public native int read(); public int read(byte[] b); public int read(byte[] b, int off, int len); public final boolean readBoolean(); public final byte readByte(); public final char readChar(); public final double readDouble(); public final float readFloat(); public final void readFully(byte[] b); public final void readFully(byte[] b, int off, int len); public final int readInt(); public final String readLine(); public final long readLong(); public final short readShort(); public final String readUTF(); public final int readUnsignedByte(); public final int readUnsignedShort(); public native void seek(long pos); public int skipBytes(int n); public native void write(int b); public void write(byte[] b); public void write(byte[] b, int off, int len); public final void writeBoolean(boolean v); public final void writeByte(int v); public final void writeBytes(String s); public final void writeChar(int v); public final void writeChars(String s); public final void writeDouble(double v); public final void writeFloat(float v); public final void writeInt(int v); public final void writeLong(long v); public final void writeShort(int v); public final void writeUTF(String str); }
The file to be accessed.
The mode of access to the file: either "r" for read access or "rw" for read/write access.
If any kind of I/O error occurs.
If mode is not "r" or "rw".
If the application does not have permission to read the named file, or if mode is "rw" and the application does not have permission to write to the named file.
This constructor creates a RandomAccessFile to access the specified File in the specified mode.
A String that contains the pathname of the file to be accessed. The path must conform to the requirements of the native operating system.
The mode of access to the file: either "r" for read access or "rw" for read/write access.
If any kind of I/O error occurs.
If mode is not "r" or "rw".
If the application does not have permission to read the named file, or if mode is "rw" and the application does not have permission to write to the named file.
This constructor creates a RandomAccessFile to access the file with the specified name in the specified mode.
If any kind of I/O error occurs.
This method closes the file and releases the system resources that are associated with it.
The file descriptor for the file that supplies data for this object.
If there is no FileDescriptor associated with this object.
This method returns the file descriptor associated with this RandomAccessFile.
The current position in the file.
If any kind of I/O error occurs.
This method returns the current position in the file. The position is the offset, in bytes, from the beginning of the file where the next read or write operation occurs.
The length of the file.
If any kind of I/O error occurs.
This method returns the length of the file in bytes.
The next byte or -1 if the end of file is encountered.
If any kind of I/O error occurs.
This method reads the next byte from the file. The method blocks until the byte is read, the end of the file is encountered, or an exception is thrown.
An array of bytes to be filled from the stream.
The number of bytes read or -1 if the end of file is encountered immediately.
If any kind of I/O error occurs.
This method reads bytes from the file into the given array. The method reads up to b.length bytes of data from the stream. The method blocks until there is some data available.
An array of bytes to be filled.
An offset into the array.
The number of bytes to read.
The number of bytes read or -1 if the end of file is encountered immediately.
If any kind of I/O error occurs.
This method reads up to len bytes from the file into the given array, starting at index off. The method blocks until there is some input available.
The boolean value read from the file.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readBoolean()
This method reads a byte as a boolean value from the file. A byte that contains a zero is read as false. A byte that contains any other value is read as true. The method blocks until the byte is read, the end of the file is encountered, or an exception is thrown.
The byte value read from the file.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readByte()
This method reads a signed 8-bit value, a byte, from the file. The method blocks until the byte is read, the end of the file is encountered, or an exception is thrown.
The char value read from the file.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readChar()
This method reads a 16-bit Unicode character from the file. The method reads two bytes from the file and then creates a char value using the first byte read as the most significant byte. The method blocks until the two bytes are read, the end of the file is encountered, or an exception is thrown.
The double value read from the file.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readDouble()
This method reads a 64-bit double quantity from the file. The method reads a long value from the file as if using the readLong() method. The long value is then converted to a double using the longBitsToDouble() method in Double. The method blocks until the necessary eight bytes are read, the end of the file is encountered, or an exception is thrown.
The float value read from the file.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readFloat()
This method reads a 32-bit float quantity from the file. The method reads an int value from the file as if using the readInt() method. The int value is then converted to a float using the intBitsToFloat() method in Float. The method blocks until the necessary four bytes are read, the end of the file is encountered, or an exception is thrown.
The array to fill.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readFully(byte[])
This method reads bytes into the given array b until the array is full. The method reads repeatedly from the file to fill the array. The method blocks until all of the bytes are read, the end of the file is encountered, or an exception is thrown.
The array to fill.
An offset into the array.
The number of bytes to read.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readFully(byte[], int, int)
This method reads len bytes into the given array, starting at offset off. The method reads repeatedly from the file to fill the array. The method blocks until all of the bytes are read, the end of the file is encountered, or an exception is thrown.
The int value read from the stream.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readInt()
This method reads a signed 32-bit int quantity from the file. The method reads four bytes from the file and then creates an int quantity, using the first byte read as the most significant byte. The method blocks until the four bytes are read, the end of the file is encountered, or an exception is thrown.
A String that contains the line read from the stream.
If the end of the file is encountered.
If any other I/O error occurs.
DataInput.readLine()
This method reads the next line of text from the file. The method reads bytes of data from the file until it encounters a line terminator. A line terminator is a carriage return ('\r'), a newline character ('\n'), a carriage return immediately followed by a newline character, or the end of the file. The method blocks until a line terminator is read, the end of the file is encountered, or an exception is thrown.
The method does not convert bytes to characters correctly.
The long value read from the stream.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readLong()
This method reads a signed 64-bit long quantity from the file. The method reads eight bytes from the file and then creates a long quantity, using the first byte read as the most significant byte. The method blocks until the eight bytes are read, the end of the file is encountered, or an exception is thrown.
The short value read from the stream.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readShort()
This method reads a signed 16-bit short quantity from the file. The method reads two bytes from the file and then creates a short quantity, using the first byte read as the most significant byte. The method blocks until the two bytes are read, the end of the file is encountered, or an exception is thrown.
The unsigned byte value read from the stream.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readUnsignedByte()
This method reads an unsigned 8-bit quantity from the file. The method reads a byte from the file and returns that byte. The method blocks until the byte is read, the end of the file is encountered, or an exception is thrown.
The unsigned short value read from the stream.
If the end of the file is encountered.
If any other kind of I/O error occurs.
DataInput.readUnsignedShort()
This method reads an unsigned 16-bit quantity from the file. The method reads two bytes from the file and creates an unsigned short quantity using the first byte read as the most significant byte. The method blocks until the two bytes are read, the end of the file is encountered, or an exception is thrown.
The String read from the stream.
If the end of the file is encountered.
If any other kind of I/O error occurs.
If the bytes do not represent a valid UTF-8 encoding.
DataInput.readUTF()
This method reads a UTF-8 encoded string from the file. The method reads the first two bytes from the file as unsigned short values, to get the number of bytes in the encoded string. Then the following bytes are read and interpreted UTF-8 encoded bytes; these bytes are converted into characters for the resulting String. This method blocks until all of the bytes in the encoded string have been read, the end of the file is encountered, or an exception is thrown. See Appendix B, The UTF-8 Encoding for information about the UTF-8 encoding.
The new position in the file.
If any kind of I/O error occurs.
This method sets the current file position to the specified position. The position is the offset, in bytes, from the beginning of the file where the next read or write operation occurs.
The number of bytes to skip.
The actual number of skipped bytes.
If EOF is encountered.
If any I/O error occurs.
DataInput.skipBytes()
This method skips over n bytes.
The value to write.
If any kind of I/O error occurs.
DataOutput.write(int)
This method writes the low-order eight bits of b to the file as a byte.
An array of bytes to write.
If any kind of I/O error occurs.
DataOutput.write(byte[])
This method writes the bytes in the given array to the file.
An array of bytes to write.
An offset into the byte array.
The number of bytes to write.
If any kind of I/O error occurs.
DataOutput.write(byte[], int, int)
This method writes len bytes from the given array, starting off elements from the beginning of the array, to the file.
The boolean value to write.
If any kind of I/O error occurs.
DataOutput.writeBoolean()
If v is true, this method writes a byte that contains the value 1 to the file. If v is false, the method writes a byte that contains the value 0.
The value to write.
If any kind of I/O error occurs.
DataOutput.writeByte()
This method writes an 8-bit byte to the file, using the low-order eight bits of the given integer v.
The String to write.
If any kind of I/O error occurs.
DataOutput.writeBytes()
This method writes the characters in the given String to the file as a sequence of 8-bit bytes. The high-order bytes of the characters in the string are ignored.
The value to write.
If any kind of I/O error occurs.
DataOutput.writeChar()
This method writes a 16-bit char to the file, using the low-order 16 bits of the given integer v.
The String to write.
If any kind of I/O error occurs.
DataOutput.writeChars()
This method writes the characters in the given String object to the file as a sequence of 16-bit characters.
The double value to write.
If any kind of I/O error occurs.
DataOutput.writeDouble()
This method writes a 64-bit double to the file. The double value is converted to a long using doubleToLongBits() of Double; the long value is then written to the file as eight bytes with the high-order byte first.
The float value to write.
If any kind of I/O error occurs.
DataOutput.writeFloat()
This method writes a 32-bit float to the file. The float value is converted to a int using floatToIntBits() of Float; the int value is then written to the file as four bytes with the high-order byte first.
The int value to write.
If any kind of I/O error occurs.
DataOutput.writeInt()
This method writes a 32-bit int to the file. The value is written as four bytes with the high-order byte first.
The long value to write.
If any kind of I/O error occurs.
DataOutput.writeLong()
This method writes a 64-bit long to the file. The value is written as eight bytes with the high-order byte first.
The value to write.
If any kind of I/O error occurs.
DataOutput.writeShort()
This method writes a 16-bit short to the file, using the low-order 16 bits of the given integer v.
The String to write.
If any kind of I/O error occurs.
DataOutput.writeUTF()
This method writes the given String to the file using the UTF-8 encoding. See Appendix B, The UTF-8 Encoding for information about the UTF-8 encoding.
Method |
Inherited From |
Method |
Inherited From |
---|---|---|---|
clone() |
Object |
equals(Object) |
Object |
finalize() |
Object |
getClass() |
Object |
hashCode() |
Object |
notify() |
Object |
notifyAll() |
Object |
toString() |
Object |
wait() |
Object |
wait(long) |
Object |
wait(long, int) |
Object |
DataInput, DataOutput, File, FileInputStream, FileOutputStream, Double, Float, Integer, IllegalArgumentException, IOException, Long
This HTML Help has been published using the chm2web software. |