Java的RandomAccessFile提供对文件的读写功能,与普通的输入输出流不一样的是RamdomAccessFile可以任意的访问文件的任何位置。RandomAccessFile的对象包含一个记录指针,用于标识当前流的读写位置,这个位置可以向前移动,也可以向后移动。
RandomAccessFile类最大的特点在于其具有可读与可写的功能并且可以任意访问文件任何位置,不像其他IO流通常只具备其中一种方法。从其继承结构就可以知道其拥有了很多读取和写入各种数据类型的方法。
Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor,or index into the implied array, called the file pointer;input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available;output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the getFilePointer method and set by the seek
method.
RandomAccessFile类核心方法
public class RandomAccessFile implements DataOutput, DataInput, Closeable {
/**
* Creates a random access file stream to read from, and opt