RandomAccessFile
This is a primitive class that implements the DataInput and DataOutput interfaces and serves to read and write data.
The construction requires a string that contains a valid path to a file or a File file.
There is a second mandatory parameter called mode. The mode is a string that can contain an r (read), w (write), or both, rw. The FileNotFound exception must be captured when the constructor runs.
File f=new File("prueba.txt");
RandomAccessFile archivo = new RandomAccessFile( f, "rw");
The fundamental methods are:
- > seek(long pos)
It allows to be placed in a specific position, counted in bytes, in the file.
What is placed is the access pointer that is the signal that marks the position to read or write.
- > long getFilePointer().Posición
current of the access pointer?
- > long length()
. Returns the file size.
- > readBoolean
, readByte
, readChar
, readInt
, readDouble
, readFloat
, readUTF
, readLine
.
Reading functions.
They read a data of the indicated type. In the case of readUTFlee a string in Unicode format.
? writeBoolean
, writeByte
, writeBytes
, writeChar
, writeChars
writeInt
, writeDouble
, writeFloat
, writeUTF
, writeLine
.
Writing functions.
All receive as a parameter, the data to write.
Write above what has already been written.
To write at the end you have to place the access pointer at the end of the file.