java.lang.Object | +----java.io.RandomAccessFile
这个类的实例支持对随机存储文件的读和写操作。一个应用程序可以修改文件中的下一个读写位置。这个类通过提供指定的只读或只写访问模式,可以支持对文件的安全访问。
File 参数指定的文件。
b.length 个字节数据读到一个字节数组中。
len 个字节数据读入一个字节数组中。
boolean 值。
double 值。
float 值。
b.length 个字节到给定数组。
len 个字节到给定数组。
n 字节输入。
off 开始的
b.length 字节写入当前文件。
off 开始的
len个字节写入当前文件。
boolean 作为一字节值,写入该文件。
byte 作为一字节值,写入该文件。
char 作为 2 字节值,写入该文件。
Double 中 doubleToLongBits 方法,将给定的双精度浮点数转换为 long 值,然后将它当作一个 8 字节数写入该文件,高字节优先。
Float 中 floatToIntBits 方法,将给定的单精度浮点数转换为 int 值,然后将它当作一个 4 字节数写入该文件,高字节优先。
int 作为 4 字节数写入该文件,高字节优先。
long 作为 8 字节数写入该文件,高字节优先。
short 作为 2 字节数写入该文件,高字节优先。
public RandomAccessFile(String name,
String mode) throws IOException
参数 mode 必须等于"r" 或
"rw", 表示打开的文件可读或可读写。
"r" 或"rw"。
checkRead 方法,查看是否允许应用程序读该文件。如果参数 mode 等于 "rw", 则还将用参数给定的文件名调用
checkWrite 方法,查看是否允许此应用程序写该文件。这两种情况都可能导致一个安全性异常。
public RandomAccessFile(File file,
String mode) throws IOException
File 参数指定的文件。
参数 mode 必须等于 "r" 或
"rw", 表示相应打开的文件可读或可读写。
"r" 或 "rw"。
File 给定的路径名调用
checkRead 方法,查看是否允许此应用程序读该文件。 如果参数 mode 等于 "rw", 则还将用路径名调用
checkWrite 方法,查看是否允许此应用程序写该文件。
public final FileDescriptor getFD() throws IOException
public native int read() throws IOException
-1 。
public int read(byte b[],
int off,
int len) throws IOException
len 个字节数据读入一个字节数组中。
此方法将阻塞,直到至少有一个字节输入。
-1。 public int read(byte b[]) throws IOException
b.length 个字节数据读到一个字节数组中。
此方法将阻塞,直到至少有一字节输入。
-1。
public final void readFully(byte b[]) throws IOException
b.length 个字节到给定数组。 这个方法将重复读,直到读入所有的字节。该方法将一直阻塞,直到所有的字节数据被读入,或检测到了数据流尾或抛出异常。
public final void readFully(byte b[],
int off,
int len) throws IOException
len 个字节到给定数组。 这个方法将重复读,直到读入所有的字节。该方法将一直阻塞,直到所有的字节数据被读入,或检测到了数据流尾或抛出异常。
public int skipBytes(int n) throws IOException
n 字节输入。
该方法将一直阻塞,直到所有的字节数据被跳过,或检测到了数据流尾或抛出异常。
n。
public native void write(int b) throws IOException
字节。
public void write(byte b[]) throws IOException
off 开始的
b.length 字节写入当前文件。
public void write(byte b[],
int off,
int len) throws IOException
off 开始的
len 个字节写入当前文件。
public native long getFilePointer() throws IOException
public native void seek(long pos) throws IOException
public native long length() throws IOException
public native void close() throws IOException
public final boolean readBoolean() throws IOException
boolean 值。 此方法从该文件中读入一个单一字节。
0 表示 false。任一其它值表示 true
。该方法将一直阻塞,直到此字节被读入,或检测到了数据流尾或抛出异常。
boolean 值。
public final byte readByte() throws IOException
b, 且
0 <= b <= 255,
那么结果是:
(byte)(b)
该方法将一直阻塞,直到此字节被读入,或检测到了数据流尾或抛出异常。
byte 的下一个字节。
public final int readUnsignedByte() throws IOException
该方法将一直阻塞,直到此字节被读入,或检测到了数据流尾或抛出异常。
public final short readShort() throws IOException
b1 和 b2, 均在
0 和 255 之间, 那么结果等于:
(short)((b1 << 8) | b2)
该方法将一直阻塞,直到此两个字节数据被读入,或检测到了数据流尾或抛出异常。
public final int readUnsignedShort() throws IOException
b1 和 b2, 满足
0 <= b1, b2 <= 255,
那么结果等于:
(b1 << 8) | b2
该方法将一直阻塞,直到此两个字节数据被读入,或检测到了数据流尾或抛出异常。
public final char readChar() throws IOException
b1 和 b2,
满足 0 <= b1, b2 <= 255,
那么结果等于:
(char)((b1 << 8) | b2)
该方法将一直阻塞,直到此两个字节数据被读入,或检测到了数据流尾或抛出异常。
public final int readInt() throws IOException
b1,
b2,b3 和 b4, 满足
0 <= b1, b2,b3,b4 <= 255,
那么结果等于:
(b1 << 24) | (b2 << 16) + (b3 << 8) + b4
该方法将一直阻塞,直到此四个字节数据被读入,或检测到了数据流尾或抛出异常。
public final long readLong() throws IOException
b1, b2, b3,
b4, b5, b6,
b7, 和 b8,当:
0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255,
相应的结果等于:
((long)b1 << 56) + ((long)b2 << 48)
+ ((long)b3 << 40) + ((long)b4 << 32)
+ ((long)b5 << 24) + ((long)b6 << 16)
+ ((long)b7 << 8) + b8
该方法将一直阻塞,直到此八个字节数据被读入,或检测到了数据流尾或抛出异常。
public final float readFloat() throws IOException
float 值。 这个方法与
readInt 方法一样读取一个 int 值,然后使用类
Float 中 intBitsToFloat 方法将这个
int 转换为一个 float 值。
该方法将一直阻塞,直到此四个字节数据被读入,或检测到了数据流尾或抛出异常。
public final double readDouble() throws IOException
double 值。 这个方法同
readLong 方法一样读取一个
long 值,然后使用类
Double 中的 longBitsToDouble 方法将这个 long 转换为一个 double。
该方法将一直阻塞,直到此八个字节数据被读入,或检测到了数据流尾或抛出异常。
public final String readLine() throws IOException
一个文本行结束于一个回车符('\r'), 一个换行符
('\n'), 一个回车符紧跟一个换行符或输入流的末尾。如果有行结束符,则它将作为返回串的一部分。
该方法将一直阻塞,直到一个换行符、或一个回车符、或紧跟回车符的换行符被读入,或检测到了数据流尾或抛出异常。
public final String readUTF() throws IOException
前两个字节以同 readUnsignedShort 方法一样的机制被读入。这个值给出了编码字符串中下面的字节数,不是结果串的长度。然后下面的字节被解释为用 UTF-8 格式编码字符的字节,且转换为字符。
该方法将一直阻塞,直到所有的字节数据被读入,或检测到了数据流尾或抛出异常。
public final void writeBoolean(boolean v) throws IOException
boolean 作为一字节值,写入该文件。 值
true 被输出为值 (byte)1;值
false 被输出为值 (byte)0。
boolean 值。
public final void writeByte(int v) throws IOException
byte作为一字节值,写入该文件。
byte值。
public final void writeShort(int v) throws IOException
short 作为 2 字节数写入该文件,高字节优先。
short。
public final void writeChar(int v) throws IOException
char 作为 2 字节值,写入该文件。
char 值。
public final void writeInt(int v) throws IOException
int 作为 4 字节数写入该文件,高字节优先。
int 值。
public final void writeLong(long v) throws IOException
long 作为 8 字节数写入该文件,高字节优先。
long。
public final void writeFloat(float v) throws IOException
Float 中 floatToIntBits 方法,将给定的单精度浮点数转换为 int 值,然后将它当作一个 4 字节数写入该文件,高字节优先。
float值。
public final void writeDouble(double v) throws IOException
Double 中 doubleToLongBits 方法,将给定的双精度浮点数转换为 long 值,然后将它当作一个 8 字节数写入该文件,高字节优先。
double 值。
public final void writeBytes(String s) throws IOException
public final void writeChars(String s) throws IOException
writeChar 方法一样把每个字符写到数据输出流。
String 值。
public final void writeUTF(String str) throws IOException
开始输出到文件中的两个字节同 writeShort 方法一样,给出下面的字节数。这个值是实际输出的字节数。这个值之后,依次输出的每个字符使用
UTF-8 编码。