随机文件的访问
RandomAccessFile
- 存在包:
package java.io; - 继承关系:
public class RandomAccessFile implements DataOutput, DataInput, Closeable
RandomAccessFile 类继承于Object,并实现接口DataInput和DataOutput中定义的读取/输出基本类型值和字符串的方法,并实现Closeable可关闭的接口;
- 简述:
随机访问文件使用文件指针进行定位:读取操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针;写入操作从文件指针开始写入字节,并随着对字节的写入而前移此文件指针。通过getFilePointer()方法可获取文件指针,通过seek()方法可设置其指向的位置。
读取随机访问文件时,如果读取所需数量的字节之前已到达文件末尾,则抛出FOFException。如果由于某些原因无法读取任何字节(例如流已被关闭,磁盘故障等。),而不是在读取所需数量的字节之前已达到文件末尾,则抛出IOException。 - 构造方法:
//根据文件名构造
public RandomAccessFile(String name, String mode)
throws FileNotFoundException
{
this(name != null ? new File(name) : null, mode);
}
//根据文件构造
public RandomAccessFile(File file, String mode)
throws FileNotFoundException
{
String name = (file != null ? file.getPath() : null);
int imode = -1;
if (mode.equals("r"))
imode = O_RDONLY;
else if (mode.startsWith("rw")) {
imode = O_RDWR;
rw = true;
if (mode.length() > 2) {
if (mode.equals("rws"))
imode |= O_SYNC;
else if (mode.equals("rwd"))
imode |= O_DSYNC;
else
imode = -1;
}
}
if (imode < 0)
throw new IllegalArgumentException("Illegal mode \"" + mode
+ "\" must be one of "
+ "\"r\", \"rw\", \"rws\","
+ " or \"rwd\"");
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
if (rw) {
security.checkWrite(name);
}
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
fd = new FileDescriptor();
fd.incrementAndGetUseCount();
this.path = name;
open(name, imode);
}
RandomAccessFile(String name, String mode)
RandomAccessFile(File file, String mode)
其中,mode 是指定打开文件的访问模式,有以下4中:
(1)“r”:当前文件支持只读操作。任何写入操作将导致抛出IOException。
(2)“rw”:当前文件支持读写操作。如果该文件不存在,则尝试创建该文件。
(3)“rws”:支持读写操作,并且将文件内容和元数据同步到存储介质。
(4)“rwd”:支持读写操作,并且将文件内容同步到底层存储介质。
- 方法介绍:
native void open(String name, int mode):打开文件操作,以特定方式打开
FileChannel getChannel(): 返回与此文件关联的唯一 FileChannel 对象。
int read(): 从此文件中读取一个数据字节。
int read(byte b[], int off, int len): 将最多 len 个数据字节从此文件读入 byte 数组。
int read(byte b[]):将最多 b.length 个数据字节从此文件读入 byte 数组。
void readFully(byte b[]): 将 b.length 个字节从此文件读入 byte 数组,并从当前文件指针开始。
void readFully(byte b[], int off, int len) :将正好 len 个字节从此文件读入 byte 数组,并从当前文件指针开始。
int skipBytes(int n):尝试跳过输入的 n 个字节以丢弃跳过的字节。
void write(int b): 向此文件写入指定的字节。
void writeBytes(byte b[], int off, int len):按字节序列将该字符串写入该文件。
void write(byte b[]): 将 b.length 个字节从指定 byte 数组写入到此文件,并从当前文件指针开始。
void write(byte b[], int off, int len):将 len 个字节从指定 byte 数组写入到此文件,并从偏移量 off 处开始。
native long getFilePointer():返回此文件中的当前偏移量。
native void seek(long pos): 设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。
native void setLength(long newLength): 设置此文件的长度。
void close()
boolean readBoolean(): 从此文件读取一个 boolean。
byte readByte(): 从此文件读取一个有符号的八位值。
char readChar():从此文件读取一个字符。
double readDouble():从此文件读取一个 double。
float readFloat():从此文件读取一个 float。
int readInt():从此文件读取一个有符号的 32 位整数。
String readLine():从此文件读取文本的下一行。
void writeBoolean(boolean v) :按单字节值将 boolean 写入该文件。
void writeByte(int v):按单字节值将 byte 写入该文件。
void writeBytes(String s): 按字节序列将该字符串写入该文件。
void writeChar(int v): 按双字节值将 char 写入该文件,先写高字节。
void writeChars(String s): 按字符序列将一个字符串写入该文件。
void writeDouble(double v): 使用 Double 类中的 doubleToLongBits 方法将双精度参数转换为一个 long,然后按八字节数量将该 long 值写入该文件,先定高字节。
void writeFloat(float v):使用 Float 类中的 floatToIntBits 方法将浮点参数转换为一个 int,然后按四字节数量将该 int 值写入该文件,先写高字节。
void writeInt(int v):按四个字节将 int 写入该文件,先写高字节。
void writeLong(long v):按八个字节将 long 写入该文件,先写高字节。
void writeShort(int v):按两个字节将 short 写入该文件,先写高字节。
void writeUTF(String str):使用 modified UTF-8 编码以与机器无关的方式将一个字符串写入该文件。
特有方法:
native long getFilePointer():返回此文件中的当前偏移量。
native void seek(long pos): 设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。
native long length():返回此文件的长度。