public class RandomAccessFile extends Object implements DataOutput, DataInput, Closeable
getFilePointer方法读取和设置的
seek方法。
它通常是在这个班级的阅读程序真实,如果文件结束之前的字节所需数量一直读到,一个EOFException(这是一种IOException)被。如果任何一个字节不能读取的文件比其他任何原因,除了EOFException抛出一个IOException。特别是,一个IOException可能如果流已关闭扔。
| Constructor and Description |
|---|
RandomAccessFile(File file, String mode)
创建一个随机访问文件流读,随意写来,由
File参数指定的文件。
|
RandomAccessFile(String name, String mode)
创建一个随机访问文件流,并可选择地写入到具有指定名称的文件中。
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
关闭此随机访问文件流,并释放与流关联的任何系统资源。
|
FileChannel |
getChannel()
返回与该文件相关的独特的
FileChannel对象。
|
FileDescriptor |
getFD()
返回与此流关联的不透明文件描述符对象。
|
long |
getFilePointer()
返回此文件中的当前偏移量。
|
long |
length()
返回此文件的长度。
|
int |
read()
从这个文件读取一个字节的数据。
|
int |
read(byte[] b)
读到
b.length从该文件为一个字节数组数据字节。
|
int |
read(byte[] b, int off, int len)
读了这个文件
len字节到字节数组数据。
|
boolean |
readBoolean()
读取这个文件
boolean。
|
byte |
readByte()
从该文件读取一个签名的八位值。
|
char |
readChar()
从这个文件读取一个字符。
|
double |
readDouble()
读取这个文件
double。
|
float |
readFloat()
读取这个文件
float。
|
void |
readFully(byte[] b)
b.length字节读取这个文件的字节数组,从当前文件指针。
|
void |
readFully(byte[] b, int off, int len)
正是从这个文件读取
len字节到字节数组,从当前文件指针。
|
int |
readInt()
从这个文件读取一个有符号的32位整数。
|
String |
readLine()
从这个文件读取下一行文本。
|
long |
readLong()
从这个文件中读取一个签名的64位整数。
|
short |
readShort()
从这个文件读取一个签名的16位数字。
|
int |
readUnsignedByte()
从该文件读取一个无符号的八位数字。
|
int |
readUnsignedShort()
从该文件读取一个无符号的16位数字。
|
String |
readUTF()
从这个文件中读取一个字符串。
|
void |
seek(long pos)
设置文件指针偏移量,从这个文件的开始,在下一个读或写发生。
|
void |
setLength(long newLength)
设置此文件的长度。
|
int |
skipBytes(int n)
试图跳过
n输入字节丢弃跳过的字节。
|
void |
write(byte[] b)
写
b.length字节指定字节数组中的这个文件,从当前文件指针。
|
void |
write(byte[] b, int off, int len)
写
len字节指定字节数组中的起始偏移量
off这个文件。
|
void |
write(int b)
将指定的字节写入该文件。
|
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)
将双参数到一个
long使用
doubleToLongBits方法在类
Double,然后写道,
long文件的值为八的字节数,高字节优先。
|
void |
writeFloat(float v)
将浮点型参数的
int使用
floatToIntBits方法在类
Float,然后写道,
int文件的值为一四的字节数,高字节优先。
|
void |
writeInt(int v)
写一个
int的文件为四字节,高字节优先。
|
void |
writeLong(long v)
写一个
long的文件为八字节,高字节优先。
|
void |
writeShort(int v)
写一个
short文件的两个字节,高字节优先。
|
void |
writeUTF(String str)
写一个字符串使用一个机器无关的方式
modified UTF-8编码文件。
|
public RandomAccessFile(String name, String mode) throws FileNotFoundException
FileDescriptor对象来表示连接文件。
的mode参数指定的文件被打开的访问模式。允许的值和它们的意义是为RandomAccessFile(File,String)构造函数中指定。
如果存在安全管理器,它的checkRead方法称为是否对文件的读访问其参数是允许的name论点。如果模式允许写作,保安部经理的checkWrite方法也被称为与name争论看文件的写访问其参数是允许的。
name -系统依赖的文件名
mode -访问
mode
IllegalArgumentException如果mode参数不等于一个
"r",
"rw",
"rws",或
"rwd"
FileNotFoundException如果模式是
"r"但给定字符串不是指现有的普通文件,或者如果这个模式开始
"rw"但给定字符串不是指现有的,写普通文件和一个新的名字不能创造规则的文件,或者一些其他的错误发生在打开或创建文件
SecurityException -如果存在一个安全管理及其
checkRead方法拒绝读取访问文件或模式是“刻录”和安全经理的
checkWrite方法拒绝写访问文件
SecurityException,
SecurityManager.checkRead(java.lang.String),
SecurityManager.checkWrite(java.lang.String)
public RandomAccessFile(File file, String mode) throws FileNotFoundException
File参数指定的文件。一个新的
FileDescriptor对象来表示这个文件连接。
的mode参数指定的文件被打开的访问模式。允许的值和它们的意思是:
| 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. |
FileChannel班
force(boolean)方法,通过
true和
false,参数分别,但他们总是适用于每一个I/O操作,因此往往更有效。如果该文件驻留在本地存储设备上,然后当这个类返回的方法的调用,它保证,由该调用的文件所做的所有更改将已写入该设备。这是有用的,以确保关键信息不会丢失在系统崩溃的事件。如果该文件不驻留在本地设备上,则没有这样的保证。
的"rwd"模式可以用来减少I/O操作。使用"rwd"只需要更新的文件的内容被写入存储;使用"rws"需要更新的文件的内容和元数据都是书面的,通常至少需要一个更底层的I/O操作。
如果存在安全管理器,它的checkRead方法调用的参数作为file路径是否对文件的读访问其参数是允许的。如果模式允许写作,保安部经理的checkWrite法又称为路径的争论看文件的写访问是允许的。
file -文件对象
mode的接入方式,如
above
IllegalArgumentException如果mode参数不等于一个
"r",
"rw",
"rws",或
"rwd"
FileNotFoundException如果模式是
"r"但给定的文件对象不表示现有的普通文件,或者如果这个模式开始
"rw"但给定的文件对象不表示一个存在,可写普通文件和一个新的名字不能创造规则的文件,或者一些其他的错误发生在打开或创建文件
SecurityException -如果存在一个安全管理及其
checkRead方法拒绝读取访问文件或模式是“刻录”和安全经理的
checkWrite方法拒绝写访问文件
SecurityManager.checkRead(java.lang.String),
SecurityManager.checkWrite(java.lang.String),
FileChannel.force(boolean)
public final FileDescriptor getFD() throws IOException
IOException如果I/O错误发生。
FileDescriptor
public final FileChannel getChannel()
FileChannel对象。
的返回信道的position总是等于该对象的文件指针偏移的getFilePointer方法返回。改变这个对象的文件指针偏移量,无论是明确的还是通过读或写字节,都会改变通道的位置,反之亦然。改变文件的长度,通过这个对象将改变通过文件通道看到的长度,反之亦然。
public int read()
throws IOException
0x00-0x0ff)。此方法块,如果没有输入可用。
虽然RandomAccessFile不是InputStream的子类,该方法具有完全相同的方式作为InputStream的InputStream.read()方法。
-1如果到达了文件的结尾。
IOException如果I/O错误发生。未达到文件结束时,未引发。
public int read(byte[] b,
int off,
int len)
throws IOException
len字节到字节数组数据。此方法块,直到至少一个字节的输入可用。
虽然RandomAccessFile不是InputStream的子类,该方法具有完全相同的方式作为InputStream的InputStream.read(byte[], int, int)方法。
b -缓冲区中读取数据。
off的起始偏移阵列
b在写入数据。
len -的最大字节数读。
-1如果没有更多的数据,因为已经到达文件末尾。
IOException -如果第一个字节不能读比其他文件的任何理由,或者随机访问的文件已被关闭,或者其他一些发生I/O错误。
null
b
NullPointerException。
IndexOutOfBoundsException -如果
off是负的,
len是负的,或
len大于
b.length - off
public int read(byte[] b)
throws IOException
b.length从该文件为一个字节数组数据字节。此方法块,直到至少一个字节的输入可用。
虽然RandomAccessFile不是InputStream的子类,该方法具有完全相同的方式作为InputStream的InputStream.read(byte[])方法。
b -缓冲区中读取数据。
-1如果没有更多的数据,因为这已经到达文件末尾。
IOException -如果第一个字节不能读比其他文件的任何理由,或者随机访问的文件已被关闭,或者其他一些发生I/O错误。
null
b
NullPointerException。
public final void readFully(byte[] b)
throws IOException
b.length字节读取这个文件的字节数组,从当前文件指针。此方法从文件中读取重复,直到请求的字节数为。该方法块,直到请求的字节数被读取,流的结束被检测到,或抛出一个异常。
readFully 接口
DataInput
b -缓冲区中读取数据。
EOFException -如果这个文件到达终点之前阅读所有字节。
IOException如果I/O错误发生。
public final void readFully(byte[] b,
int off,
int len)
throws IOException
len字节到字节数组,从当前文件指针。此方法从文件中读取重复,直到请求的字节数为。该方法块,直到请求的字节数被读取,流的结束被检测到,或抛出一个异常。
readFully 接口
DataInput
b -缓冲区中读取数据。
off的起始偏移量的数据。
len -字节数读。
EOFException -如果这个文件到达终点之前阅读所有字节。
IOException如果I/O错误发生。
public int skipBytes(int n)
throws IOException
n输入字节丢弃跳过的字节。
此方法可以跳过一些较小的字节数,可能为零。这可能会导致任何一个条件;在n字节已经跳过到达了文件的结尾只有一种可能性。这种方法从未抛出一个EOFException。跳过的字节数实际返回。如果n是负的,没有跳过的字节。
skipBytes 接口
DataInput
n -字节数被跳过。
IOException如果I/O错误发生。
public void write(int b)
throws IOException
write 接口
DataOutput
b -
byte要写。
IOException如果I/O错误发生。
public void write(byte[] b)
throws IOException
b.length字节指定字节数组中的这个文件,从当前文件指针。
write 接口
DataOutput
b -数据。
IOException如果I/O错误发生。
public void write(byte[] b,
int off,
int len)
throws IOException
len字节指定字节数组中的起始偏移量
off这个文件。
write 接口
DataOutput
b -数据。
off的起始偏移量的数据。
len -字节数写。
IOException如果I/O错误发生。
public long getFilePointer()
throws IOException
IOException如果I/O错误发生。
public void seek(long pos)
throws IOException
pos的偏移位置,测量的字节从文件的开始,在其中设置文件指针。
IOException -如果
pos小于
0或者I/O错误发生。
public long length()
throws IOException
IOException如果I/O错误发生。
public void setLength(long newLength)
throws IOException
如果当前的文件长度的length方法返回大于然后newLength参数文件将被截断。在这种情况下,如果文件偏移方法返回的getFilePointer大于newLength之后这个方法返回偏移量将等于newLength。
如果当前的文件长度的length方法返回小于然后newLength参数文件将扩展。在这种情况下,文件的扩展部分的内容没有被定义。
newLength -该文件所需的长度
IOException如果I/O错误发生
public void close()
throws IOException
如果这个文件有一个相关的通道,那么这个通道也关闭了。
close 接口
Closeable
close 接口
AutoCloseable
IOException如果I/O错误发生。
public final boolean readBoolean()
throws IOException
boolean。此方法从文件读取一个单字节,从当前的文件指针开始。一个价值
0代表
false。任何其他值表示
true。此方法块,直到读到字节,流的结束被检测到,或抛出一个异常。
readBoolean 接口
DataInput
boolean阅读的价值。
EOFException -如果该文件已结束。
IOException如果I/O错误发生。
public final byte readByte()
throws IOException
b,哪里
0 <= b <= 255,那么结果是:
(byte)(b)
此方法块,直到读到字节,流的结束被检测到,或抛出一个异常。
readByte 接口
DataInput
byte签署。
EOFException -如果该文件已结束。
IOException如果I/O错误发生。
public final int readUnsignedByte()
throws IOException
此方法块,直到读到字节,流的结束被检测到,或抛出一个异常。
readUnsignedByte 接口
DataInput
EOFException -如果该文件已结束。
IOException如果I/O错误发生。
public final short readShort()
throws IOException
b1和
b2,其中每台价值
0和
255,包容之间,那么结果等于:
(short)((b1 << 8) | b2)
此方法块,直到两个字节被读取,流的结束被检测到,或抛出一个异常。
readShort 接口
DataInput
EOFException -如果这个文件到达终点之前阅读两字节。
IOException如果I/O错误发生。
public final int readUnsignedShort()
throws IOException
b1和
b2,哪里
0 <= b1, b2 <= 255,那么结果等于:
(b1 << 8) | b2
此方法块,直到两个字节被读取,流的结束被检测到,或抛出一个异常。
readUnsignedShort 接口
DataInput
EOFException -如果这个文件到达终点之前阅读两字节。
IOException如果I/O错误发生。
public final char readChar()
throws IOException
b1和
b2,哪里
0 <= b1, b2 <= 255,那么结果等于:
(char)((b1 << 8) | b2)
此方法块,直到两个字节被读取,流的结束被检测到,或抛出一个异常。
readChar 接口
DataInput
char。
EOFException -如果这个文件到达终点之前阅读两字节。
IOException如果I/O错误发生。
public final int readInt()
throws IOException
b1,
b2,
b3,和
b4,哪里
0 <= b1, b2, b3, b4 <= 255,那么结果等于:
(b1 << 24) | (b2 << 16) + (b3 << 8) + b4
该方法块,直到读到四个字节,流的结束被检测到,或抛出一个异常。
readInt 接口
DataInput
int。
EOFException -如果这个文件到达终点之前读四个字节。
IOException如果I/O错误发生。
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
该方法块,直到读到八个字节,流的结束被检测到,或抛出一个异常。
readLong 接口
DataInput
long。
EOFException -如果这个文件到达终点之前读八个字节。
IOException如果I/O错误发生。
public final float readFloat()
throws IOException
float。此方法读取一个
int值,从当前文件指针,仿佛由
readInt方法然后将
int到
float使用
intBitsToFloat方法
Float
该方法块,直到读到四个字节,流的结束被检测到,或抛出一个异常。
类。readFloat 接口
DataInput
float。
EOFException -如果这个文件到达终点之前读四个字节。
IOException如果I/O错误发生。
readInt(),
Float.intBitsToFloat(int)
public final double readDouble()
throws IOException
double。此方法读取一个
long值,从当前文件指针,仿佛由
readLong方法然后将
long到
double使用
longBitsToDouble方法在类
Double。
该方法块,直到读到八个字节,流的结束被检测到,或抛出一个异常。
readDouble 接口
DataInput
double。
EOFException -如果这个文件到达终点之前读八个字节。
IOException如果I/O错误发生。
readLong(),
Double.longBitsToDouble(long)
public final String readLine() throws IOException
一行文字是由回车符终止('\r')、换行符('\n'),一个回车符紧跟一个换行符,或文件的结尾。行终止字符被丢弃,不包括作为返回字符串的一部分。
这种方法阻塞直到一个换行符读,回车后的字节读(看它是否是一个换行符),在到达文件末尾,或者抛出一个异常。
readLine 接口
DataInput
IOException如果I/O错误发生。
public final String readUTF() throws IOException
第一个字节读,从当前文件指针,如readUnsignedShort。此值给出了在编码字符串中的下列字节数,而不是字符串的长度。下面的字节,然后解释为字节编码字符的UTF-8格式的修改和转换成字符。
该方法块,直到所有的字节都被读取,流的结束被检测到,或抛出一个异常。
readUTF 接口
DataInput
EOFException -如果这个文件到达终点之前阅读所有字节。
IOException如果I/O错误发生。
UTFDataFormatException如果字节不代表有效改进UTF-8 Unicode字符串编码。
readUnsignedShort()
public final void writeBoolean(boolean v)
throws IOException
boolean的文件为一一字节的值。
true写出来的价值
(byte)1价值;价值
false写出来的价值
(byte)0。写入在文件指针的当前位置处开始。
writeBoolean 接口
DataOutput
v -
boolean值被写入。
IOException如果I/O错误发生。
public final void writeByte(int v)
throws IOException
byte的文件为一一字节的值。写入在文件指针的当前位置处开始。
writeByte 接口
DataOutput
v -
byte值被写入。
IOException如果I/O错误发生。
public final void writeShort(int v)
throws IOException
short文件的两个字节,高字节优先。写入在文件指针的当前位置处开始。
writeShort 接口
DataOutput
v -
short要写。
IOException如果I/O错误发生。
public final void writeChar(int v)
throws IOException
char的文件为一二字节的值,高字节优先。写入在文件指针的当前位置处开始。
writeChar 接口
DataOutput
v -
char值被写入。
IOException如果I/O错误发生。
public final void writeInt(int v)
throws IOException
int的文件为四字节,高字节优先。写入在文件指针的当前位置处开始。
writeInt 接口
DataOutput
v -
int要写。
IOException如果I/O错误发生。
public final void writeLong(long v)
throws IOException
long的文件为八字节,高字节优先。写入在文件指针的当前位置处开始。
writeLong 接口
DataOutput
v -
long要写。
IOException如果I/O错误发生。
public final void writeFloat(float v)
throws IOException
int使用
floatToIntBits方法在类
Float,然后写道,
int文件的值为一四的字节数,高字节优先。写入在文件指针的当前位置处开始。
writeFloat 接口
DataOutput
v -
float值被写入。
IOException如果I/O错误发生。
Float.floatToIntBits(float)
public final void writeDouble(double v)
throws IOException
long使用
doubleToLongBits方法在类
Double,然后写道,
long文件的值为八的字节数,高字节优先。写入在文件指针的当前位置处开始。
writeDouble 接口
DataOutput
v -
double值被写入。
IOException如果I/O错误发生。
Double.doubleToLongBits(double)
public final void writeBytes(String s) throws IOException
writeBytes 接口
DataOutput
s -一个字符串的字节被写入。
IOException如果I/O错误发生。
public final void writeChars(String s) throws IOException
writeChar。写入在文件指针的当前位置处开始。
writeChars 接口
DataOutput
s -
String值被写入。
IOException如果I/O错误发生。
writeChar(int)
public final void writeUTF(String str) throws IOException
首先,两个字节写入文件,从当前文件指针,仿佛由writeShort方法给出的字节数跟随。此值是实际写出来的字节数,而不是字符串的长度。以下的长度,字符串的每个字符输出序列,使用UTF-8字符编码改为每。
writeUTF 接口
DataOutput
str -字符串被写入。
IOException如果I/O错误发生。
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.