java.io
Class RandomAccessFile

java.lang.Object
  |
  +--java.io.RandomAccessFile
All Implemented Interfaces:
DataInput, DataOutput

public class RandomAccessFile
extends Object
implements DataOutput, DataInput

Random access files can be constructed from file descriptors, file names, or file objects. This class provides a sense of security by offering methods that allow specified mode accesses of read-only or read-write to files.


Constructor Summary
RandomAccessFile(File file, String mode)
          Creates a RandomAccessFile from a specified File object and mode ("r" or "rw").
RandomAccessFile(String name, String mode)
          Creates a RandomAccessFile with the specified system dependent file name and the specified mode.
 
Method Summary
 void close()
          Closes the file.
 FileDescriptor getFD()
          Returns the opaque file descriptor object.
 long getFilePointer()
          Returns the current location of the file pointer.
 long length()
          Returns the length of the file.
 int read()
          Reads a byte of data.
 int read(byte[] b)
          Reads data into an array of bytes.
 int read(byte[] b, int off, int len)
          Reads a sub array as a sequence of bytes.
 boolean readBoolean()
          Reads a boolean.
 byte readByte()
          Reads a byte.
 char readChar()
          Reads a 16 bit char.
 double readDouble()
          Reads a 64 bit double.
 float readFloat()
          Reads a 32 bit float.
 void readFully(byte[] b)
          Reads bytes, blocking until all bytes are read.
 void readFully(byte[] b, int off, int len)
          Reads bytes, blocking until all bytes are read.
 int readInt()
          Reads a 32 bit int.
 String readLine()
          Reads a line terminated by a '\n' or EOF.
 long readLong()
          Reads a 64 bit long.
 short readShort()
          Reads 16 bit short.
 int readUnsignedByte()
          Reads an unsigned 8 bit byte.
 int readUnsignedShort()
          Reads 16 bit short.
 String readUTF()
          Reads a UTF formatted String.
 void seek(long pos)
          Sets the file pointer to the specified absolute position.
 int skipBytes(int n)
          Skips bytes, block until all bytes are skipped.
 void write(byte[] b)
          Writes an array of bytes.
 void write(byte[] b, int off, int len)
          Wrotes a sub array of bytes.
 void write(int b)
          Writes a byte of data.
 void writeBoolean(boolean v)
          Writes a boolean.
 void writeByte(int v)
          Writes a byte.
 void writeBytes(String s)
          Writes a String as a sequence of bytes.
 void writeChar(int v)
          Writes a character.
 void writeChars(String s)
          Writes a String as a sequence of chars.
 void writeDouble(double v)
          Writes a 64 bit double.
 void writeFloat(float v)
          Writes a 32 bit float.
 void writeInt(int v)
          Writes an integer.
 void writeLong(long v)
          Writes a long.
 void writeShort(int v)
          Writes a short.
 void writeUTF(String str)
          Writes a String in UTF format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomAccessFile

public RandomAccessFile(String name,
                        String mode)
                 throws IOException
Creates a RandomAccessFile with the specified system dependent file name and the specified mode. Mode "r" is for read-only and mode "rw" is for read+write.

Parameters:
name - the system dependent file name
mode - the access mode
Throws:
IOException - If an I/O error has occurred.

RandomAccessFile

public RandomAccessFile(File file,
                        String mode)
                 throws IOException
Creates a RandomAccessFile from a specified File object and mode ("r" or "rw").

Parameters:
file - the file object
mode - the access mode
Method Detail

getFD

public final FileDescriptor getFD()
                           throws IOException
Returns the opaque file descriptor object.

Returns:
the file descriptor.
IOException

read

public int read()
         throws IOException
Reads a byte of data. This method will block if no input is available.

Returns:
the byte read, or -1 if the end of the stream is reached.
Throws:
IOException - If an I/O error has occurred.

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Reads a sub array as a sequence of bytes.

Parameters:
b - the data to be written
off - the start offset in the data
len - the number of bytes that are written
Throws:
IOException - If an I/O error has occurred.

read

public int read(byte[] b)
         throws IOException
Reads data into an array of bytes. This method blocks until some input is available.

Returns:
the actual number of bytes read, -1 is returned when the end of the stream is reached.
Throws:
IOException - If an I/O error has occurred.

readFully

public final void readFully(byte[] b)
                     throws IOException
Reads bytes, blocking until all bytes are read.

Specified by:
readFully in interface DataInput
Parameters:
b - the buffer into which the data is read
Returns:
the actual number of bytes read, -1 is returned when the end of the stream is reached.
Throws:
IOException - If an I/O error has occurred.

readFully

public final void readFully(byte[] b,
                            int off,
                            int len)
                     throws IOException
Reads bytes, blocking until all bytes are read.

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 maximum number of bytes read
Returns:
the actual number of bytes read, -1 is returned when the end of the stream is reached.
Throws:
IOException - If an I/O error has occurred.

skipBytes

public int skipBytes(int n)
              throws IOException
Description copied from interface: DataInput
Skips bytes, block until all bytes are skipped.

Specified by:
skipBytes in interface DataInput
Parameters:
n - the number of bytes to be skipped
Returns:
the actual number of bytes skipped.
Throws:
IOException - If other I/O error has occurred.

write

public void write(int b)
           throws IOException
Writes a byte of data. This method will block until the byte is actually written.

Specified by:
write in interface DataOutput
Parameters:
b - the byte to be written
Throws:
IOException - If an I/O error has occurred.

write

public void write(byte[] b)
           throws IOException
Writes an array of bytes. Will block until the bytes are actually written.

Specified by:
write in interface DataOutput
Parameters:
b - the data to be written
Throws:
IOException - If an I/O error has occurred.

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Wrotes a sub array of bytes.

Specified by:
write in interface DataOutput
Parameters:
b - the data to be written
off - the start offset in the data
len - the number of bytes that are written
Throws:
IOException - If an I/O error has occurred.

getFilePointer

public long getFilePointer()
                    throws IOException
Returns the current location of the file pointer.

IOException

seek

public void seek(long pos)
          throws IOException
Sets the file pointer to the specified absolute position.

Parameters:
pos - the absolute position
IOException

length

public long length()
            throws IOException
Returns the length of the file.

IOException

close

public void close()
           throws IOException
Closes the file.

Throws:
IOException - If an I/O error has occurred.

readBoolean

public final boolean readBoolean()
                          throws IOException
Reads a boolean.

Specified by:
readBoolean in interface DataInput
Returns:
the boolean read.
Throws:
IOException - If other I/O error has occurred.

readByte

public final byte readByte()
                    throws IOException
Reads a byte.

Specified by:
readByte in interface DataInput
Returns:
the 8 bit byte read.
Throws:
IOException - If other I/O error has occurred.

readUnsignedByte

public final int readUnsignedByte()
                           throws IOException
Reads an unsigned 8 bit byte.

Specified by:
readUnsignedByte in interface DataInput
Returns:
the 8 bit byte read.
Throws:
IOException - If other I/O error has occurred.

readShort

public final short readShort()
                      throws IOException
Reads 16 bit short.

Specified by:
readShort in interface DataInput
Returns:
the read 16 bit short.
Throws:
IOException - If other I/O error has occurred.

readUnsignedShort

public final int readUnsignedShort()
                            throws IOException
Reads 16 bit short.

Specified by:
readUnsignedShort in interface DataInput
Returns:
the read 16 bit short.
Throws:
IOException - If other I/O error has occurred.

readChar

public final char readChar()
                    throws IOException
Reads a 16 bit char.

Specified by:
readChar in interface DataInput
Returns:
the read 16 bit char.
Throws:
IOException - If other I/O error has occurred.

readInt

public final int readInt()
                  throws IOException
Reads a 32 bit int.

Specified by:
readInt in interface DataInput
Returns:
the read 32 bit integer.
Throws:
IOException - If other I/O error has occurred.

readLong

public final long readLong()
                    throws IOException
Reads a 64 bit long.

Specified by:
readLong in interface DataInput
Returns:
the read 64 bit long.
Throws:
IOException - If other I/O error has occurred.

readFloat

public final float readFloat()
                      throws IOException
Reads a 32 bit float.

Specified by:
readFloat in interface DataInput
Returns:
the read 32 bit float.
Throws:
IOException - If other I/O error has occurred.

readDouble

public final double readDouble()
                        throws IOException
Reads a 64 bit double.

Specified by:
readDouble in interface DataInput
Returns:
the read 64 bit double.
Throws:
IOException - If other I/O error has occurred.

readLine

public final String readLine()
                      throws IOException
Reads a line terminated by a '\n' or EOF.

Specified by:
readLine in interface DataInput
IOException

readUTF

public final String readUTF()
                     throws IOException
Reads a UTF formatted String.

Specified by:
readUTF in interface DataInput
IOException

writeBoolean

public final void writeBoolean(boolean v)
                        throws IOException
Writes a boolean.

Specified by:
writeBoolean in interface DataOutput
Parameters:
v - the boolean value
IOException

writeByte

public final void writeByte(int v)
                     throws IOException
Writes a byte.

Specified by:
writeByte in interface DataOutput
Parameters:
v - the byte
IOException

writeShort

public final void writeShort(int v)
                      throws IOException
Writes a short.

Specified by:
writeShort in interface DataOutput
Parameters:
v - the short
IOException

writeChar

public final void writeChar(int v)
                     throws IOException
Writes a character.

Specified by:
writeChar in interface DataOutput
Parameters:
v - the char
IOException

writeInt

public final void writeInt(int v)
                    throws IOException
Writes an integer.

Specified by:
writeInt in interface DataOutput
Parameters:
v - the integer
IOException

writeLong

public final void writeLong(long v)
                     throws IOException
Writes a long.

Specified by:
writeLong in interface DataOutput
Parameters:
v - the long
IOException

writeFloat

public final void writeFloat(float v)
                      throws IOException
Description copied from interface: DataOutput
Writes a 32 bit float.

Specified by:
writeFloat in interface DataOutput
Parameters:
v - the float value to be written
IOException

writeDouble

public final void writeDouble(double v)
                       throws IOException
Description copied from interface: DataOutput
Writes a 64 bit double.

Specified by:
writeDouble in interface DataOutput
Parameters:
v - the double value to be written
IOException

writeBytes

public final void writeBytes(String s)
                      throws IOException
Writes a String as a sequence of bytes.

Specified by:
writeBytes in interface DataOutput
Parameters:
s - the String
IOException

writeChars

public final void writeChars(String s)
                      throws IOException
Writes a String as a sequence of chars.

Specified by:
writeChars in interface DataOutput
Parameters:
s - the String
IOException

writeUTF

public final void writeUTF(String str)
                    throws IOException
Writes a String in UTF format.

Specified by:
writeUTF in interface DataOutput
Parameters:
str - the String
IOException