Advanced   Java   Services RandomAccessFile (wahlweiser Dateizugriff) Back Next Up Home


RandomAccessFile

Objekte dieser Klasse haben einen wahlfreien Zugriff auf den Inhalt einer Datei. Dagegen stellen die übrigen Dateizugriffe eine Art Einbahnstraße dar, egal ob zum Lesen oder Schreiben, man kann die Datei nur in einer Richtung durchlaufen, vom Dateianfang zum Dateiende. Mit RandomAccessFile kann man sich in einer datei beliebig vorwärts oder eben auch rückwärts bewegen. Dies geschieht mit einem Dateipositionszeiger, der frei verschiebbar ist. Mit der Methode seek(long pos) kann man den Dateipositionszeiger setzen. Der Zugriff kann lesend oder schreibend sein oder beides zugleich. Die Art des Zugriffs wird über den zweiten Parameter des Konstruktors gesteuert. Die Methoden von RandomAccessFile sind zum Teil identisch mit den Methoden aus ObjectInputStream und ObjectOutputStream, da RandomAccessFile die Interfaces und DataInput und DataOutput implementiert.


Mögliche Zugriffsmodi (zweiter Parameter des Konstruktors)




Value Meaning
"r" Open for reading only. Invoking any of the write methods of the resulting object will cause an IOException to be thrown.
"rw" Open for reading and writing. If the file does not already exist then an attempt will be made to create it.
"rws" Open for reading and writing, as with "rw", and also require that every update to the file's content or metadata be written synchronously to the underlying storage device.
"rwd" Open for reading and writing, as with "rw", and also require that every update to the file's content be written synchronously to the underlying storage device.
"other" RuntimeException in thread "main" java.lang.IllegalArgumentException: mode must be r, rw, rws or rwd

Auszug aus der API








Konstruktoren
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.
Wichtige Methoden
ReturntypName der Methode
voidclose()
Closes this random access file stream and releases any system resources associated with the stream.
longgetFilePointer()
Returns the current offset in this file.
longlength()
Returns the length of this file.
intread()
Reads a byte of data.
intread(byte[] buf, int off, int len)
Reads into an array of bytes.
booleanreadBoolean()
Reads in a boolean.
bytereadByte()
Reads an 8 bit byte.
charreadChar()
Reads a 16 bit char.
doublereadDouble()
Reads a 64 bit double.
floatreadFloat()
Reads a 32 bit float.
voidreadFully(byte[] buf)
Reads bytes, blocking until all bytes are read.
intreadInt()
Reads a 32 bit int.
longreadLong()
Reads a 64 bit long.
shortreadShort()
Reads a 16 bit short.
intreadUnsignedByte()
Reads an unsigned 8 bit byte.
intreadUnsignedShort()
Reads an unsigned 16 bit short.
StringreadUTF()
Reads a String in modified UTF-8 format.
voidseek(long pos)
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
StringsetLength(long newLength)
Sets the length of this file.
intskipBytes(int n)
Attempts to skip over n bytes of input discarding the skipped bytes.
voidwrite(byte[] buf)
Writes an array of bytes.
voidwrite(int val)
Writes a byte.
voidwriteBoolean(boolean val)
Writes a boolean.
voidwriteByte(int val)
Writes an 8 bit byte.
voidwriteBytes(String str)
Writes a String as a sequence of bytes.
voidwriteChar(int val)
Writes a 16 bit char.
voidwriteChars(String str)
Writes a String as a sequence of chars.
voidwriteDouble(double val)
Writes a 64 bit double.
voidwriteFloat(float val)
Writes a 32 bit float.
voidwriteInt(int val)
Writes a 32 bit int.
voidwriteLong(long val)
Writes a 64 bit long.
voidwriteShort(int val)
Writes a 16 bit short.
voidwriteUTF(String str)
Primitive data write of this String in modified UTF-8 format.

Valid XHTML 1.0 Strict top Back Next Up Home